SAS Programming

DATA Step, Macro, Functions and more
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;
run
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

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 3500 views
  • 3 likes
  • 4 in conversation