what is the difference between SAS ARRAY and SAS IF-THEN

Eyal Marom

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. 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:

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 ;
Tom

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. 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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章