how to create 65 lags in SaS without copying and paste 65 times -
i got panel data. symbol
company names. date
, hour
represent time. want create upto 65 lagged return variable. can achieve target using following code. however, have copy , paste 65 times messy. can tell me how can improve code please.
data data (drop=i count); set data; symbol date hour; array x(*) rlag1-rlag65; rlag1=lag1(return); rlag2=lag2(return); rlag3=lag3(return); rlag4=lag4(return); rlag5=lag5(return); rlag6=lag6(return); rlag7=lag7(return); rlag8=lag8(return); rlag9=lag9(return); rlag10=lag10(return); /* reset count @ start of each new by-group */ if first.symbol count=1; i=count dim(x); x(i)=.; end; count + 1; run;
i want run following regression 65 times. can teach me how loop regression , auto change output file name please. essentially, want lag of independent variable should same last/last 2 digit of name of output file.
proc reg data=data noprint outest=lag1; model return = rlag1; date hour;;; run; quit;
you can use array achieve this.
first sample data , sort by
varaibles need:
proc sort data=sashelp.stocks out=have; stock date; run;
now in datastep, define variables going store values in. make sure retain
them keep values across observations. can achieve both of these things using retain
statement.
then define array based on variables defined in retain
. array allow refer variables lag1-lag10 without explicitly typing exact names.
in example, want lags 1 10. note i'm keeping variable called lag0
though - because simplify code. drop variable @ end.
so logic says, if it's first observation stock, intialise of values in array missing. assign value of current stock closing price lag0
.
if it's not first of stock, starting @ maximum lag (in case 10), assign value of next-oldest lag (in case 9). repeat way through array until have done lag1=lag0; finally, assign value of current stock closing price lag0
.
this shifts values down in array go on observations.
data want; set have; stock; retain lag0-lag10; array a[0:10] lag0-lag10; if first.stock do; cnt=0 10; a[cnt] = .; end; end; else do; cnt=10 1 -1; a[cnt] = a[cnt-1]; end; end; a[0] = close; drop lag0 cnt; run;
for regression, can loop on piece of sas code shown below. i'll leave exercise adapt code.
%macro reg; %do cnt=1 %to 10; data x&cnt; set want; value = lag&cnt; run; %end; %mend; %reg;
Comments
Post a Comment