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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.