I want to compare the id is it same or not by comparing them in a same row.
And I tried this data step, Can someone help me :
data temp;
input id $ var1 var2;
New_id= id;
datalines;
aa 12 13
Bb 13 14
Cc 15 16
;
data want :
id | var1 | var2 | new id |
aa | 12 | 13 | |
Bb | 13 | 14 | aa |
Cc | 15 | 16 | Bb |
For values of a previous observation, you use the LAG function:
new_id = lag(id);
data want;
set temp;
newid = lag(id);
run;
Thank you so much!
But if now I want the new_id in the first row is equal to first id value (aa), what should I do?
id | var1 | var2 | new id |
aa | 12 | 13 | aa |
Bb | 13 | 14 | aa |
Cc | 15 | 16 | Bb |
For values of a previous observation, you use the LAG function:
new_id = lag(id);
Thank you!
But if now I want the new_id in the first row is equal to first id value (aa), what should I do?
id | var1 | var2 | new id |
aa | 12 | 13 | aa |
Bb | 13 | 14 | aa |
Cc | 15 | 16 | Bb |
I think everyone's getting a little confused because your want data set keeps getting changed. Below is a cheap way to get what you want since the first record doesn't have a lag value to look for.
data want;
set temp;
new_id = lag(id);
if missing(new_id) then new_id = id;
run;
id var1 var2 New_id aa 12 13 aa Bb 13 14 aa Cc 15 16 Bb
Can't guarantee this will hold up depending on the complexity of your actual data set.
Just add an additional override:
data want;
set have;
new_id = lag(id);
if _n_ = 1 then new_id = id;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.