Solved
New Contributor
Posts: 2

# 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

 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!

Accepted Solutions
Solution
‎05-18-2016 05:09 PM
Super User
Posts: 8,069

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

All Replies
Super User
Posts: 5,876

## 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,

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: 13,498

## 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
Posts: 8,069

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

Posts: 5,519

## 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
Posts: 8,069

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