BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HAHA888
Calcite | Level 5

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 :

 

idvar1var2new id
aa1213 
Bb1314aa
Cc1516Bb
1 ACCEPTED SOLUTION
6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20
data want;
   set temp;
   newid = lag(id);
run;
HAHA888
Calcite | Level 5

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

var1var2new id
aa1213aa
Bb1314aa
Cc1516Bb
HAHA888
Calcite | Level 5

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

var1var2new id
aa1213aa
Bb1314aa
Cc1516Bb
maguiremq
SAS Super FREQ

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.

SAS Innovate 2025: Register Now

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!

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
  • 6 replies
  • 1089 views
  • 0 likes
  • 4 in conversation