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_id | tprfiled |
1 | 0 |
2 | |
3 | 0 |
4 | 0 |
5 | 0 |
6 | 0 |
7 | 0 |
8 | 0 |
9 | 0 |
10 | 0 |
11 | 0 |
12 | 0 |
13 | 0 |
14 | 0 |
15 | 0 |
16 | 0 |
17 | 0 |
18 | 0 |
19 | 0 |
20 | 1 |
21 | 1 |
22 | 0 |
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.
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;
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;
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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.