Hi folks,
I have some problem to write the following code by using a do loop. Say I have a column and its name is var1. Now I want to generate the second column and its name is var2, where var2=lag(var1). Similarly, var3=lag(var2) etc.
data want;
set column1;
var2=lag(var1);
var3=lag(var2);
var4=lag(var3);
run;
When I try to use a do loop:
data want;
set column1;
do i=2 to 4;
vari=lag(var(i-1));
run;
Obviously it's not working cuz the i is mixed with the string. Any idea on how to fix it? Thanks.
Use an array
data want;
set column1;
array vars{4} var1-var4;
do i=2 to 4;
vars{i}=lag(vars{i-1});
end;
run;
Edit: closed the do block.
Here's one of the missing pieces. You will need to set up an array:
array vars {4} var1-var4;
Then you can refer to elements in the array as vars{i}:
vars{i}=lag(vars(i-1));
What is the "MUST" part of the question? MUST use a DO loop? MUST get the right result? MUST use LAG?
You might be able to solve this without fancy tools, just knowledge of how the DATA step works:
data want;
var4=var3;
var3=var2;
var2=var1;
set have;
retain var1-var4;
run;
Use an array
data want;
set column1;
array vars{4} var1-var4;
do i=2 to 4;
vars{i}=lag(vars{i-1});
end;
run;
Edit: closed the do block.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.