Create one what? This seems like it is missing something: " I want to create one until three periods lead variable of X called X_lead1 X_lead2 X_lead3, "
Define "period" as it is not at all obvious from the data.
Rules for creating the values? You say when, sort of, to create a value, but not how.
@Golf wrote:
Hello all, @Kurt_BremserSuppose I have 3 variables (X, Y, and Z)obs X Y Z1 10 11 212 2 12 223 40 13 234 4 14 245 80 15 25Now I want to create one until three periods lead variable of X called X_lead1 X_lead2 X_lead3, while keeping the existing variables as follow.obs X Y Z X_lead1 X_lead2 X_lead31 10 11 21 2 40 42 2 12 22 40 4 803 40 13 23 4 80 .4 4 14 24 80 . .5 80 15 25 . . .Thank YouCould you provide guidance on how to write SAS code to generate the second data set?
Since it is easier to remember the past than predict the future just sort the dataset in the opposite order and use LAG() functions.
Your example data does not appear to have any variable that can be used for ordering, so let's add one.
data reverse;
row+1;
set have;
run;
Now sort and then make the new variables.
proc sort data=reverse;
by descending row;
run;
data want;
set reverse ;
x_lead1 = lag1(x);
x_lead2 = lag2(x);
x_lead3 = lag3(x);
run;
data have;
input X Y Z;
cards;
10 11 21
2 12 22
40 13 23
4 14 24
80 15 25
;
run;
data want;
merge have
have(firstobs=2 keep=x rename=(x=X_lead1))
have(firstobs=3 keep=x rename=(x=X_lead2))
have(firstobs=4 keep=x rename=(x=X_lead3));
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.