cordelia
cordelia

Reputation: 133

SAS Loop through Columns

I am trying to write code in SAS which loops through the columns.

DesiredVariable is what I am trying to calculate using a Loop

where if the LengthValue for a Row is 5 Then the DesiredVariable value = Length5 and if the LengthValue for a Row is 3 Then the DesiredVariable value = Length3

(Image attached)

I would really appreciate any help writing this loop because I am relatively new to SAS.

Thanks in advance.

enter image description here

Upvotes: 1

Views: 2074

Answers (1)

momo1644
momo1644

Reputation: 1804

You don't need to loop the rows, you only need to create an Array for the columns length1-length5 and use LengthValue as the pointer.

data have;
input Name $ LengthValue Length1 Length2 Length3 Length4 Length5 DesiredVariable;
datalines;
Sally 5 0.29 0.21 0.73 0.46 0.43 .
Andrea 2 0.97 0.76 0.5 0.33 0.41 .
;
run;
data want;
set have;
Array length[*] Length1-Length5;
DesiredVariable=length[LengthValue];
run;

Output:

Name=Sally LengthValue=5 Length1=0.29 Length2=0.21 Length3=0.73 Length4=0.46 Length5=0.43 DesiredVariable=0.43
Name=Andrea LengthValue=2 Length1=0.97 Length2=0.76 Length3=0.5 Length4=0.33 Length5=0.41 DesiredVariable=0.76

Upvotes: 2

Related Questions