BookmarkSubscribeRSS Feed
Paul_NYS
Obsidian | Level 7

Hi Everyone

I am using the below data step to set my tprfiled variable to the prior value for it except for the first record in the data set. However, it is not recognizing the first record's 0 when processing the second record--a null is inserted instead of 0. Any ideas why this is?

Paul

data s1astpr3;

set s1astpr2;

if _n_ ne 1 then tprfiled=lag(tprfiled);

run;

entity_idtprfiled
10
2
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
201
211
220
4 REPLIES 4
pradeepalankar
Obsidian | Level 7

Hi,

Try with just -> tprfiled=lag(tprfiled);  it will give null value for first row and lag values for subsequent rows.

your code is giving the same value for tprfiled which must be 0 in your source data, and lagging null value to second row.

Paul_NYS
Obsidian | Level 7

I tried doing just doing just the lag initially, but wanted to accommodate the issue with the first record. But I just tried the below, which is a variation of this and it works:

data s1astpr3;

set s1astpr2;

tprfiled2=tprfiled;

tprfiled=lag(tprfiled);

if _n_ = 1 then do;

tprfiled=tprfiled2;

end;

drop tprfiled2;

run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Not sure about lag, tried using it myself some time back and had some weird results.  So this doesn't answer your question, but does do whats requested:

proc sql;

  create table WANT as

  select  A.ENTITY_ID,

          B.TPRFILED

  from    HAVE A

  left join HAVE B

  on      A.ENTITY_ID=(B.ENTITY_ID+1);

quit;

Paul_NYS
Obsidian | Level 7

Hi Rw9

The only issue with this is that I have a lot more columns that I indicated and they change regularly, so I don't really want to deal with proc sql and writing out column names.

Paul

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1475 views
  • 3 likes
  • 3 in conversation