DATA Step, Macro, Functions and more

create new var conditionally

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

create new var conditionally

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

Accepted Solutions
Solution
‎10-11-2016 12:35 PM
Super User
Posts: 17,819

Re: create new var conditionally

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


All Replies
Solution
‎10-11-2016 12:35 PM
Super User
Posts: 17,819

Re: create new var conditionally

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;

Contributor
Posts: 65

Re: create new var conditionally

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
Super User
Posts: 10,500

Re: create new var conditionally

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.

Contributor
Posts: 65

Re: create new var conditionally

Got it. Thank you, ballardw!
Super User
Posts: 17,819

Re: create new var conditionally

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...

 

 

Contributor
Posts: 65

Re: create new var conditionally

I'll read this chapter. thank you so much, Reeza!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 305 views
  • 3 likes
  • 3 in conversation