BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Golf
Pyrite | Level 9
Hello all,   @Kurt_Bremser 
Suppose I have  3 variables  (X, Y, and Z)
    obs    X         Y       Z
      1       10       11       21
      2        2       12      22
      3      40      13      23
      4       4        14      24
       5      80      15      25
Now 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_lead3
      1         10       11       21          2                   40              4
      2          2       12      22       40                     4             80
      3       40       13      23         4                   80                .
     4          4        14      24      80                      .                 .
     5        80        15       25        .                        .                 .
Thank You
 
Could you provide guidance on how to write SAS code to generate the second data set?
1 ACCEPTED SOLUTION

Accepted Solutions
Golf
Pyrite | Level 9
However, when I applied your code as follow:
data reverse;
input time x y z;
cards;
1 10 11 21
2 2 12 22
3 40 13 23
4 4 14 24
5 80 15 25
;
run;

proc sort data=reverse;
by descending time;
run;
data want;
set reverse ;
x_lead1 = lag1(x);
x_lead2 = lag2(x);
x_lead3 = lag3(x);
run;

proc print data = want;
var time x y z x_lead1 x_lead2 x_lead3;
The result is not what I want.

View solution in original post

8 REPLIES 8
ballardw
Super User

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_Bremser 
Suppose I have  3 variables  (X, Y, and Z)
    obs    X         Y       Z
      1       10       11       21
      2        2       12      22
      3      40      13      23
      4       4        14      24
       5      80      15      25
Now 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_lead3
      1         10       11       21          2                   40              4
      2          2       12      22       40                     4             80
      3       40       13      23         4                   80                .
     4          4        14      24      80                      .                 .
     5        80        15       25        .                        .                 .
Thank You
 
Could you provide guidance on how to write SAS code to generate the second data set?

 

Golf
Pyrite | Level 9
"Thank you for your help. Just to clarify, if I change "obs" to "time", would it still make sense? Also, when I say "I want to create one until three period", I actually mean "I want to create data for one to three periods ahead." Lastly, the periods (".") shown in the "want" data indicate that the data is not available." Thank You.
Tom
Super User Tom
Super User

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;
Golf
Pyrite | Level 9
Thank You very much for solving my problem. Bests.
Golf
Pyrite | Level 9
However, when I applied your code as follow:
data reverse;
input time x y z;
cards;
1 10 11 21
2 2 12 22
3 40 13 23
4 4 14 24
5 80 15 25
;
run;

proc sort data=reverse;
by descending time;
run;
data want;
set reverse ;
x_lead1 = lag1(x);
x_lead2 = lag2(x);
x_lead3 = lag3(x);
run;

proc print data = want;
var time x y z x_lead1 x_lead2 x_lead3;
The result is not what I want.

Golf
Pyrite | Level 9
I was able to achieve my goal by reversing the data by time variable once again. Thanks a lot.
Ksharp
Super User
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;
Golf
Pyrite | Level 9
Thank you for sharing the code. I have learned a lot.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1023 views
  • 3 likes
  • 4 in conversation