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.

Ready to join fellow brilliant minds for the SAS Hackathon?

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