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
Data want; set have ;
by date IDA IDB ;
if first.IDA then Group = 1
else if first.idb the group+1;
run;
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;
Sorry, for some reason the code does not work.
"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).
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 |
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;
I think OP hasn't framed the question well. OP is perhaps expecting responders to guess
@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.
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!
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.
Ready to level-up your skills? Choose your own adventure.