DATA Step, Macro, Functions and more

Groups

Reply
Contributor
Posts: 23

Groups

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

 

 

PROC Star
Posts: 1,792

Re: Groups

Posted in reply to RandyStan

Data want; set have ;

by date IDA IDB ;

if first.IDA then   Group = 1

else if first.idb the group+1;

run;

 

Esteemed Advisor
Posts: 5,526

Re: Groups

Posted in reply to RandyStan

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
Contributor
Posts: 23

Re: Groups

Sorry, for some reason the code does not work.

Super User
Posts: 10,229

Re: Groups

Posted in reply to RandyStan

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 23

Re: Groups

Posted in reply to KurtBremser

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

Re: Groups

[ Edited ]
Posted in reply to RandyStan

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;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 75

Re: Groups

Posted in reply to KurtBremser

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

Super User
Posts: 13,523

Re: Groups

Posted in reply to RandyStan

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

 

Ask a Question
Discussion stats
  • 8 replies
  • 121 views
  • 4 likes
  • 6 in conversation