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

Hello, 

How to obtain previous observation by id

Have

secid sas_date      TNA

1        201501           345

1        201502         234

2        201501          278

2       201502           876

 

want 

secid sas_date      TNA

1        201501           .

1        201502         345

2        201501          .

2       201502           278

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

It really is a good idea to be consistent about use of variable names. You said "by id" which makes me guess that you mean secid variable. Having to guess can result in poor performance.

 

One way: This adds a new variable Ltna so you can compare the results for accuracy.

data want;
   set have;
   by notsorted secid;
   ltna= lag(tna);
   if first.secid then call missing(ltna);
run;

The By statement assumes your data is at least grouped by Secid if not sorted by such. The By statement makes automatic variables that indicate whether the current record is either the first or last of a by group.

The Lag function returns values from the previous record with some limitations. The above approach always gets a previous value and then sets it to missing when it is the first of Secid group.

View solution in original post

1 REPLY 1
ballardw
Super User

It really is a good idea to be consistent about use of variable names. You said "by id" which makes me guess that you mean secid variable. Having to guess can result in poor performance.

 

One way: This adds a new variable Ltna so you can compare the results for accuracy.

data want;
   set have;
   by notsorted secid;
   ltna= lag(tna);
   if first.secid then call missing(ltna);
run;

The By statement assumes your data is at least grouped by Secid if not sorted by such. The By statement makes automatic variables that indicate whether the current record is either the first or last of a by group.

The Lag function returns values from the previous record with some limitations. The above approach always gets a previous value and then sets it to missing when it is the first of Secid group.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 1 reply
  • 184 views
  • 4 likes
  • 2 in conversation