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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.