BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasphd
Lapis Lazuli | Level 10

Hello,

I know that when we want the previous observation, we use (lag) function

What can I use when I want next observation?

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

Hi,

Please see below.

data have;
input Obs    Height;
datalines;
1     69.0
2     56.5
3     65.3
4     62.8
5     63.5
;

Proc sql;
Select have.*, next.*
from have left join have as next
on have.obs + 1 = next.obs;
Quit;

View solution in original post

5 REPLIES 5
MikeZdeb
Rhodochrosite | Level 12

here's one idea ...

data new;

merge sashelp.class (keep=height) sashelp.class (firstobs=2 keep=height rename=(height=nextheight));

run;

Obs    Height    nextheight

  1     69.0        56.5

  2     56.5        65.3

  3     65.3        62.8

  4     62.8        63.5

  5     63.5        57.3

  6     57.3        59.8

  7     59.8        62.5

  8     62.5        62.5

  9     62.5        59.0

10     59.0        51.3

11     51.3        64.3

12     64.3        56.3

13     56.3        66.5

14     66.5        72.0

15     72.0        64.8

16     64.8        67.0

17     67.0        57.5

18     57.5        66.5

19     66.5          .

stat_sas
Ammonite | Level 13

data have;
input a;
datalines;
1
2
3
4
5
;

Proc sql;
Select have.a, next.a as next_obs
from have left join have as next
on have.a + 1 = next.a;
Quit;

MikeZdeb
Rhodochrosite | Level 12

Hi ... have you tried your solution with some data other than 1 2 3 4 5 (anything that is not just a bunch of consecutive values)?

stat_sas
Ammonite | Level 13

Hi,

Please see below.

data have;
input Obs    Height;
datalines;
1     69.0
2     56.5
3     65.3
4     62.8
5     63.5
;

Proc sql;
Select have.*, next.*
from have left join have as next
on have.obs + 1 = next.obs;
Quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 4768 views
  • 0 likes
  • 4 in conversation