Help with Array of lag value

Accepted Solution Solved
Reply
Super Contributor
Posts: 420
Accepted Solution

Help with Array of lag value

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;


Accepted Solutions
Solution
‎07-10-2014 01:43 PM
Super User
Posts: 11,343

Re: Help with Array of lag value

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


All Replies
Super User
Posts: 19,855

Re: Help with Array of lag value

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

Super Contributor
Posts: 420

Re: Help with Array of lag value

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

Super Contributor
Posts: 420

Re: Help with Array of lag value

I just check, mine is not SAS/ETS.

Solution
‎07-10-2014 01:43 PM
Super User
Posts: 11,343

Re: Help with Array of lag value

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 339 views
  • 0 likes
  • 3 in conversation