Thanks!
Here is the IML code . I believe it is going to be faster than John King(data _null_) 's code .
data have;
infile cards expandtabs truncover;
input X1 X2 X3 X4 X5;
cards;
1 11 21 31 41
2 12 22 32 42
3 13 23 33 43
4 14 24 34 44
5 15 25 35 45
6 16 26 36 46
7 17 27 37 47
8 18 28 38 48
9 19 29 39 49
;
run;
%let lag=5;
proc iml;
use have;
read all var _num_ into x[c=vnames];
close;
varname=j(ncol(x),&lag,blankstr(nleng(vnames)+8));
do i=1 to &lag;
varname[,i]=t(cats(vnames,'_',i));
end;
varname=vnames||rowvec(varname);
lag=j(nrow(x),&lag#ncol(x),.);
idx=do(1,&lag#ncol(x),&lag);
l=1:&lag;
do i=1 to ncol(idx);
lag[,idx[i]:idx[i]+&lag-1]=lag(x[,i],l);
end;
want=x||lag;
create want from want[c=varname];
append from want;
close;
quit;
Here is the new Data Step code , also faster than John King(data _null_)'s code and no need to consider about if the variable like var1-var5 . It is applied to any variable name .
data have;
infile cards expandtabs truncover;
input X1 X2 X3 X4 X5;
cards;
1 11 21 31 41
2 12 22 32 42
3 13 23 33 43
4 14 24 34 44
5 15 25 35 45
6 16 26 36 46
7 17 27 37 47
8 18 28 38 48
9 19 29 39 49
;
run;
%let lag=5;
data x;
do i=1 to &lag;
output;
end;
run;
proc transpose data=have(obs=0) out=temp;
var _all_;
run;
proc sql;
select cats(_name_,'_',i,'=lag',i,'(',_name_,')') into : lag separated by ';'
from temp,x ;
quit;
data want;
set have;
&lag ;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.