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

My purpose is to make a list of 60 lag variables, but there are always errors in logs which makes me suffer. I'll appreciate if someone could help me out with that!Attaching file is my code and sorry for the Chinese version of log error.

data stock_return3;
 set stock_return2;
 by permno date;
 Er = alpha + beta * vwretd;        /*Expected return*/
 AR = RET -Er;                      /*Abnormal retrun*/
 PER=1+RET;
 if first.permno then i=0;
 i+1;
 x=lag(1+ret);
 x1=lag2(1+ret);
 do i=2 to 59;
  x&i=lag(&i+1)(1+ret);
 end;
keep permno date ret vwretd month Er AR PER x x1-x59 i;
runerror.png
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

If you have SAS/IML ,that would be very easy.

Otherwise you need make a macro to generate the code like :

 

lag1=lag1(x);

lag2=lag2(x);

..............

 

 

proc iml;
use sashelp.class ;
read all var {age};
close;

lag=1:4;
name='age_lag1':'age_lag4';
x=lag(age,lag);

create lag from x[c=name];
append from x;
close;
quit;

data want;
 merge sashelp.class lag;
run;

proc print noobs;run;


View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Hi and welcome to the SAS communities 🙂

 

First of all: Why do you want 60 variables with lagged values? 

 

Secondly. In situations like this, I like to use PROC EXPAND. I would probably do something data driven like below

 

data have;
   set sashelp.stocks;
   keep stock date close;
run;

data _null_;
   length string $500;
   call execute("proc expand data=have out=want; by stock;");

   do lag=1 to 60;
      string=compbl(cat(
         "
         convert close = lag" , lag, "_close / transformout=(lag ", lag, ");
         "
         ));
      output;
      call execute(string);
   end;

   call execute("run;quit;");
run;
Kurt_Bremser
Super User

You should use an array of retained variables for this (I guess).

Please supply an example for stock_return2 (use a data step with datalines and post it with the "little running man" icon), and what you expect to get from it.

This

do i=2 to 59;
  x&i=lag(&i+1)(1+ret);
end;

can't work, as &i is a macro variable reference, and you do not have any macro code to define this macro variable.

 

Ksharp
Super User

If you have SAS/IML ,that would be very easy.

Otherwise you need make a macro to generate the code like :

 

lag1=lag1(x);

lag2=lag2(x);

..............

 

 

proc iml;
use sashelp.class ;
read all var {age};
close;

lag=1:4;
name='age_lag1':'age_lag4';
x=lag(age,lag);

create lag from x[c=name];
append from x;
close;
quit;

data want;
 merge sashelp.class lag;
run;

proc print noobs;run;


toby88131
Calcite | Level 5

Thank you for your resolution! Smiley Happy

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2737 views
  • 3 likes
  • 4 in conversation