BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Roxy_Dong
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

6 REPLIES 6
LinusH
Tourmaline | Level 20
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
Roxy_Dong
Calcite | Level 5

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.

ballardw
Super User

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;

 

Tom
Super User Tom
Super User

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.

PGStats
Opal | Level 21

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

PG
Tom
Super User Tom
Super User

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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 12877 views
  • 6 likes
  • 5 in conversation