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
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;
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;
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.
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;
Thank you for your resolution!
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!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.