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

This is the data I have:

MBR_IDProv_IDDRGStart_DTEnd_DT
0015840910010077406Dec201129Jul2012
0015840910010077406Dec201129Jul2012
0021142410010076516Jan201221Sep2012
0032458210119077524Oct201113Feb2012
0032458210119077524Oct201113Feb2012
0033087110022076605Jan201230Aug2012
0033087110022076605Jan201230Aug2012
0033433410014077513Feb201224Oct2012
0033433410014077513Feb201224Oct2012
0033738110019077525Aug201118Apr2012
0033738110019077525Aug201118Apr2012
0034041810004076620Mar201220Dec2012
0034041810004076620Mar201220Dec2012
0034572710014077522Jul201101Mar2012
0034572710014077522Jul201101Mar2012
0034764310015076611Jan201227Apr2012
0034764310015076611Jan201227Apr2012
0034864110020077523Feb201201Nov2012
0034864110020077523Feb2012

01Nov2012

I want to create a new var Admit_ID like this: if the values for the 5 vars in original data set are same for row1 to row2, we sign 1 to the Admit_ID then we move on to compare row3, row 3 is different (if any one of the five values is different, we conside row2 and row3 are different), then we sign 2 to row3. then we move on to compare row3 to row4....

MBR_IDProv_IDDRGStart_DTEnd_DTAdmit_ID
0015840910010077406Dec201129Jul20121
0015840910010077406Dec201129Jul20121
0021142410010076516Jan201221Sep20122
0032458210119077524Oct201113Feb20123
0032458210119077524Oct201113Feb20123
0033087110022076605Jan201230Aug20124
0033087110022076605Jan201230Aug20124
0033433410014077513Feb201224Oct20125
0033433410014077513Feb201224Oct20125
0033738110019077525Aug201118Apr20126
0033738110019077525Aug201118Apr20126
0034041810004076620Mar201220Dec20127
0034041810004076620Mar201220Dec20127
0034572710014077522Jul201101Mar20128
0034572710014077522Jul201101Mar20128
0034764310015076611Jan201227Apr20129
0034764310015076611Jan201227Apr20129
0034864110020077523Feb201201Nov201210
0034864110020077523Feb201201Nov201210
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

This is commonly known as creating an enumeration or counter variable. 

 

Data want;

set have;

by var1 var2... var5;

 

retain counter 0;

 

If first.var5 = 1 then counter+1;

 

run;

View solution in original post

6 REPLIES 6
Reeza
Super User

This is commonly known as creating an enumeration or counter variable. 

 

Data want;

set have;

by var1 var2... var5;

 

retain counter 0;

 

If first.var5 = 1 then counter+1;

 

run;

zhuxiaoyan1
Quartz | Level 8
Thank for Reeza! This works perfectly. But I don't understand the by statement. Can you explain how the by statement works in this situation? Thanks a lot!

Xiaoyan
ballardw
Super User

One of the things the BY statement does in a data step is keep track of the whether the the current record is the first or last value of the combination of the by variables. That makes the boolean values FIRST.Variable and LAST.Variable available. So FIRST.VAR5 is true when it is the first of each level of values within VAR5 for the combination of values Var1 through Var4.

 

If the other variables are not included then the order of the processing would be incorrect and actually likely generate an error about data not being sorted in order for var5.

zhuxiaoyan1
Quartz | Level 8
Got it. Thank you, ballardw!
Reeza
Super User

It's worth skimming over the documentation, even if you don't read or understand it all at this point. 

Especially the examples. Then the next time you see a problem that's similar you can connect the dots.

 

It's a long chapter:

http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#n138da4gme3zb7n1nifp...

 

 

zhuxiaoyan1
Quartz | Level 8
I'll read this chapter. thank you so much, Reeza!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 970 views
  • 3 likes
  • 3 in conversation