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-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!

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
  • 1247 views
  • 3 likes
  • 3 in conversation