DATA Step, Macro, Functions and more

SAS loop through observations

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

SAS loop through observations

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

IDKN1KN2KN3KN4KN
1031349992
10555265
119999999262
17685643
2015489992
206999399924
2306749992

 

 

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!


Accepted Solutions
Solution
‎05-18-2016 05:09 PM
Super User
Super User
Posts: 6,498

Re: SAS loop through observations

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.

View solution in original post


All Replies
Super User
Posts: 5,255

Re: SAS loop through observations

A SAS data step loops through observations automatically.
For you rule use simple if-then-assignment statements.
I recommend that you start going through some initial training / read books and concepts parts of Base SAS user guides.
Data never sleeps
New Contributor
Posts: 2

Re: SAS loop through observations

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.

Super User
Posts: 10,483

Re: SAS loop through observations

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;

 

Solution
‎05-18-2016 05:09 PM
Super User
Super User
Posts: 6,498

Re: SAS loop through observations

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.

Respected Advisor
Posts: 4,641

Re: SAS loop through observations

Nice explanation @Tom. But with all due respect, why teach an undocumented feature of the data step language to a beginner?

PG
Super User
Super User
Posts: 6,498

Re: SAS loop through observations

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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 640 views
  • 4 likes
  • 5 in conversation