BookmarkSubscribeRSS Feed
Aexor
Lapis Lazuli | Level 10

Dear all,

 

I am trying to calculate next year value based on a panel data like below:

 

ID                Year                Value

                1993                0.29

1                 1994                0.65

1                 1995                0.79

2                 1993                1.23

2                 1994                1.56

3                 2001                0.98

3                 2002                0.73

 

What I would want is to calculate lead(value). For example:

 

ID                Year                Value                lead1   lead2

                1993                0.29                   0.65    0.79

1                 1994                0.65                   0.79     1.23

1                 1995                0.79                   1.23 

 

Please help me in creating lead1 and lead2.

Thanks!

4 REPLIES 4
PaigeMiller
Diamond | Level 26

How is it that in your output, in ROW 2, LEAD2 has a value of 1.23. Could you explain this?

 

How is it that in your output, in ROW 3, LEAD1 has a value of 1.23. Could you explain this?

 

--
Paige Miller
Aexor
Lapis Lazuli | Level 10
I want to print the next value , the output should come like that.
i am afraid that i am getting your query here.
i want to print a coloumn lead1 and lead2 like this.
PaigeMiller
Diamond | Level 26

Why in ROW 2 is LEAD2 = 1.23? Explain how you get that number. Don't just say, that's what you want, explain where the number comes from. Does ID play any role in this?

--
Paige Miller
mkeintz
PROC Star

Assuming you did not intend for the last observations of ID=1 to get LEAD1 value from the first observation of ID2 (or the other example of propagating "lead" values accross ID's), then this should solve the problem.  It is ready-made for use of a _TEMPORARY_ array to be built in the first pass of each id, and to retrieve in the second pass:

 

data want;
  set have (in=firstpass)  have (in=secondpass);
  by id;
  array values {1990:2001} _temporary_;

  if first.id then call missing(of values{*});
  if firstpass then values{year}=value;

  if secondpass;
  lead1=values{year+1};
  lead2=values{year+2};
run;

Just make sure to specify upper and lower bounds for array VALUES to cover your entire data range.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

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
  • 4 replies
  • 693 views
  • 1 like
  • 3 in conversation