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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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