BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

My Data set is as follows

 

Date                   ST                IDA            IDA

01JAN2018         500                 A             AA

01JAN2018          501                A             AA

02JAN2018          501               A               AA

01JAN2018          499               A               AB

02JAN2018           506              A               AB

03JAN2018           590              A                AB

 

I need to group by IDA and IDB and add a variable Group

 

Date                   ST                IDA            IDAB        Group

01JAN2018         500                 A             AA            1

01JAN2018          501                A             AA             1

02JAN2018          501               A               AA             1

01JAN2018          499               A               AB              2

02JAN2018           506              A               AB              2

03JAN2018           590              A                AB             2

 

I wrote this code

proc sort data = have ; by date IDA IDB ; run;

 

Data want; set have ;

by date IDA IDB ;

if first.IDA then do;  Group = IDA +1;

run;

 

Am I making a mistake?

  Randy

 

 

8 REPLIES 8
novinosrin
Tourmaline | Level 20

Data want; set have ;

by date IDA IDB ;

if first.IDA then   Group = 1

else if first.idb the group+1;

run;

 

PGStats
Opal | Level 21

You want to change group when IDB changes value, so:

Data want; 
set have ;
by date IDA IDB ;
if first.IDB then Group + 1;
run;
PG
RandyStan
Fluorite | Level 6

Sorry, for some reason the code does not work.

Kurt_Bremser
Super User

"does not work" is extremely unhelpful.

Post the log of the code as you ran it, and state clearly where the result does not meet your expectations.

Note that this might be a consequence of not posting example data in a usable form (data step with datalines, which allows us to recreate your data exactly as it is).

RandyStan
Fluorite | Level 6

Here is the data set and what i need.  Thanks

 

datetcodetmTIDWant TIDGET_TID
3-Jan-1522663111
4-Jan-1500002J66663221
3-Jan-1500011BS663231
4-Jan-1500011DEVD663241
3-Jan-1500011KANN663251
3-Jan-1500011KRS663261
4-Jan-1500011NLSH663271
4-Jan-1500011PAD663281
3-Jan-1500011USDE663291
3-Jan-1500069Z0016632101
4-Jan-151003910492111
3-Jan-15101631049211 
3-Jan-15102731049211 
3-Jan-15102731049611 
3-Jan-15102731049711 
3-Jan-15102731049811 
3-Jan-15102731049911 
4-Jan-151027310491011 
4-Jan-151027310491111 
4-Jan-151027310491211 
4-Jan-151027310491311 
4-Jan-151027310491411 
4-Jan-151027310491511 
3-Jan-151048910492121
3-Jan-151062910492131
Kurt_Bremser
Super User

First of all, this still does not allow us to recreate a dataset with all the attributes as you have it.

Second, this has different columns and does not correspond to your initial post at all.

 

From what I see, you want to increment every time there is a change in tcode.

This would look like

data want;
set have;
by tcode notsorted;
retain tid 0;
if first.tcode then tid + 1;
run;

 

Andygray
Quartz | Level 8

I think OP hasn't framed the question well. OP is perhaps expecting responders to guess

ballardw
Super User

@RandyStan wrote:

Here is the data set and what i need.  Thanks

 

date tcode tm TID Want TID GET_TID
3-Jan-15 22 663 1 1 1
4-Jan-15 00002J66 663 2 2 1
3-Jan-15 00011BS 663 2 3 1
4-Jan-15 00011DEVD 663 2 4 1
3-Jan-15 00011KANN 663 2 5 1
3-Jan-15 00011KRS 663 2 6 1
4-Jan-15 00011NLSH 663 2 7 1
4-Jan-15 00011PAD 663 2 8 1
3-Jan-15 00011USDE 663 2 9 1
3-Jan-15 00069Z001 663 2 10 1
4-Jan-15 10039 1049 2 11 1
3-Jan-15 10163 1049 2 11  
3-Jan-15 10273 1049 2 11  
3-Jan-15 10273 1049 6 11  
3-Jan-15 10273 1049 7 11  
3-Jan-15 10273 1049 8 11  
3-Jan-15 10273 1049 9 11  
4-Jan-15 10273 1049 10 11  
4-Jan-15 10273 1049 11 11  
4-Jan-15 10273 1049 12 11  
4-Jan-15 10273 1049 13 11  
4-Jan-15 10273 1049 14 11  
4-Jan-15 10273 1049 15 11  
3-Jan-15 10489 1049 2 12 1
3-Jan-15 10629 1049 2 13 1

But your question

I need to group by IDA and IDB and add a variable Group

 

Date                   ST                IDA            IDAB        Group

01JAN2018         500                 A             AA            1

So, how do we know what is ST IDA IDAB in your question when compared to the example data? It really does not help to discuss a problem in one term and then use different variables.

 

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