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

Hi Everyone,

I want to analyze the lag value of a variable.

Sure I can create lag1, lag2... lag100 variable and do the "array var(*) lag1-lag100;"

However, I wonder if there is any shorter way for the code such as

array var(*) lag1(x)-lag10(x);

Thank you for your help.

HHC

data have(drop=i);
do i=1 to 10000;
x=ranuni(-1);
output;
end;
run;


data want; set have;
array var(*) lag1(x)-lag10(x);
do i=1 to dim(lag);
if lag(i)>0 then t=1;
end;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

A macro may simplify the code;

%macro MakeLag;

%do i = 1 %to 100;

     Lag&i = Lag&i(x);

%end;

%mend;

Data want;

     set have;

     %MakeLag;
     array var lag1-lag100;

     <other code>

run;

BTW your example code

do i=1 to dim(lag);

if lag(i)>0 then t=1;

end;

is incorrect syntax;

do i = 1 to dim(var); /* use the array name which you called var*/

if var >0 then t=1; /* use the array with the index*/

end;

Which will only generate one value of t, depending on the value of the last lagged value as written.

View solution in original post

4 REPLIES 4
Reeza
Super User

Do you have a license for SAS/ETS (Econometrics and Time Series)?

hhchenfx
Barite | Level 11

I'm not sure but I should have it with my SAS

hhchenfx
Barite | Level 11

I just check, mine is not SAS/ETS.

ballardw
Super User

A macro may simplify the code;

%macro MakeLag;

%do i = 1 %to 100;

     Lag&i = Lag&i(x);

%end;

%mend;

Data want;

     set have;

     %MakeLag;
     array var lag1-lag100;

     <other code>

run;

BTW your example code

do i=1 to dim(lag);

if lag(i)>0 then t=1;

end;

is incorrect syntax;

do i = 1 to dim(var); /* use the array name which you called var*/

if var >0 then t=1; /* use the array with the index*/

end;

Which will only generate one value of t, depending on the value of the last lagged value as written.

sas-innovate-2024.png

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.

 

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
  • 4 replies
  • 2569 views
  • 0 likes
  • 3 in conversation