Hi all, I am a new user of SAS. I have a data and I want to write a loop throug observations, but I don't know how to do it in SAS.
Here is the data
ID | KN1 | KN2 | KN3 | KN4 | KN |
103 | 1 | 3 | 4 | 999 | 2 |
105 | 5 | 5 | 2 | 6 | 5 |
119 | 999 | 999 | 2 | 6 | 2 |
176 | 8 | 5 | 6 | 4 | 3 |
201 | 5 | 4 | 8 | 999 | 2 |
206 | 999 | 3 | 999 | 2 | 4 |
230 | 6 | 7 | 4 | 999 | 2 |
I want to write a loop that from the fisrt subject to the last subject, if this subject's kn1-4 equals to 999, then change the 999 to the value of this subject's kn.
Thank you very much!
Unless your problem is different than it sounds you do NOT want to loop through observations. SAS does that for you already. So let's just consider how to handle a single observation.
ID KN1 KN2 KN3 KN4 KN
103 1 3 4 999 2
If you wrote the code by hand you would do:
if KN1=999 then KN1=KN;
if KN2=999 then KN2=KN;
if KN3=999 then KN3=KN;
if KN4=999 then KN4=KN;
You can use an ARRAY to allow you to apply to same rule to all of the similar variables.
array _KN KN1-KN4 ;
do over _KN;
if _KN=999 then _KN=KN ;
end;
To do more variables just add them to the array definition.
Hi,
Thank you for replying me.
I know how to use if... then, but in my original data I have hundreds observations and more than 30 variables and each one's kn value is different. If I use kn1=999 then kn1= kn, it will be pretty fussy. I tried array but it treat data in column.
Here is a snippet of code you may find useful
data want;
set have;
array k Kn1-Kn4;
do i= 1 to dim(k);
if k[i]=999 then k[i]= kn;
end;
drop i;
run;
Unless your problem is different than it sounds you do NOT want to loop through observations. SAS does that for you already. So let's just consider how to handle a single observation.
ID KN1 KN2 KN3 KN4 KN
103 1 3 4 999 2
If you wrote the code by hand you would do:
if KN1=999 then KN1=KN;
if KN2=999 then KN2=KN;
if KN3=999 then KN3=KN;
if KN4=999 then KN4=KN;
You can use an ARRAY to allow you to apply to same rule to all of the similar variables.
array _KN KN1-KN4 ;
do over _KN;
if _KN=999 then _KN=KN ;
end;
To do more variables just add them to the array definition.
Nice explanation @Tom. But with all due respect, why teach an undocumented feature of the data step language to a beginner?
I am pretty sure that ARRAYs are covered in the documentation.
But perhaps you meant the DO OVER?
I find it much each to explain this type of situation by using a concept that matches the application. In this problem the "INDEX" that is required if you do not use the DO OVER construct has no meaning. The purpose of the DO loop is apply the same logic to EVERY member of the array. The relative order of the members of the array is meaningless.
Now if the variables represented something where an index makes sense then it is clearer to use index notation. So if the readings represent results for the months of the year it would make more sense to write:
array reading m1-m12 ;
do month=1 to 12 ;
if reading(month)= 999 then reading(month)=X;
end;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.