简体   繁体   English

SAS ARRAY 和 SAS IF-THEN 有什么区别

[英]what is the difference between SAS ARRAY and SAS IF-THEN

I have a table with students exams scores;我有一张学生考试成绩表; veriables: name, score1, score2, score3 and gender wherever there is a missing value in one of the scores, the score is set to 999. I want to transform all 999's to missing (.) values. veriables: name, score1, score2, score3 and gender 只要其中一个分数有缺失值,分数就设置为 999。我想将所有 999 转换为缺失 (.) 值。 I realized there are 2 main ways and I would like to know the MAIN difference between them.我意识到有两种主要方式,我想知道它们之间的主要区别。

As written above, both give the same output: first:如上所述,两者都给出相同的 output:首先:

data try ;
    set mis_999 ;
        if score1 = 999 then score1 = . ;
        if score2 = 999 then score2 = . ;
        if score3 = 999 then score3 = . ;
run ;

second (with array):第二个(带数组):

data array_try ;
    set mis_999 ;
    array try2{*} score1-score3 ;
    do i=1 to dim(try2) ;
    if try2(i) = 999 then try2(i) = . ;
    end ;
run ;

For that example the main difference is that the code using an array is easier to expand to more variables.对于该示例,主要区别在于使用数组的代码更容易扩展到更多变量。

In your first example you have what is referred to as wallpaper code, a lot of code that repeats the same pattern.在您的第一个示例中,您有所谓的墙纸代码,许多代码重复相同的模式。 If you have 500 variables instead of 3 you would need to write 500 statements.如果您有 500 个变量而不是 3 个,则需要编写 500 条语句。 But with the array method you would just need to change the list of variables in the array definition.但是使用数组方法,您只需要更改数组定义中的变量列表。 The DO loop would be the same. DO 循环将是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM