BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
soomx
Fluorite | Level 6

Hi,

 

I have a data like this:

 

ID     Date             Trt

1      3/10/2018     AN

1      5/3/2018       BH

1      5/3/2018       AN

1      9/9/2018      CL

2      2/10/2018     AN

3      5/30/2018       CL

3      5/30/2018       TH

3      12/9/2018      AN

4      3/15/2018     GB

4      6/3/2018       BH

4      10/3/2018       AN

 

I want this:

ID     Date             Trt

1      3/10/2018     AN

1      5/3/2018       BH/AN

1      9/9/2018      CL

2      2/10/2018     AN

3      5/30/2018       CL/TH

3      12/9/2018      AN

4      3/15/2018     GB

4      6/3/2018       BH

4      10/3/2018       AN

 

Basically, I am combining the TRT that occur on the same dates into one value. I can't seem to do this without combining all the TRT for each ID into one.

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20



 
data have;
input ID     Date :mmddyy10.            Trt $;
format date mmddyy10.;
cards;
1      3/10/2018     AN
1      5/3/2018       BH
1      5/3/2018       AN
1      9/9/2018      CL
2      2/10/2018     AN
3      5/30/2018       CL
3      5/30/2018       TH
3      12/9/2018      AN
4      3/15/2018     GB
4      6/3/2018       BH
4      10/3/2018       AN
;

data want;
do until(last.date);
set have;
by id date;
length want $50;
want=catx('/',want,trt);
end;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20



 
data have;
input ID     Date :mmddyy10.            Trt $;
format date mmddyy10.;
cards;
1      3/10/2018     AN
1      5/3/2018       BH
1      5/3/2018       AN
1      9/9/2018      CL
2      2/10/2018     AN
3      5/30/2018       CL
3      5/30/2018       TH
3      12/9/2018      AN
4      3/15/2018     GB
4      6/3/2018       BH
4      10/3/2018       AN
;

data want;
do until(last.date);
set have;
by id date;
length want $50;
want=catx('/',want,trt);
end;
run;
soomx
Fluorite | Level 6

Worked perfectly. Thank you so much...can't believe it's so simple!

novinosrin
Tourmaline | Level 20

Yes you are right. Just a matter of understanding by group processing in a sorted dataset by id date

 

Rest is a breeze 🙂

PGStats
Opal | Level 21

Use a DO UNTIL loop and the catx function:

 

data have;
input ID Date:mmddyy. Trt$ ;
format date yymmdd10.;
datalines;
1      3/10/2018     AN
1      5/3/2018       BH
1      5/3/2018       AN
1      9/9/2018      CL
2      2/10/2018     AN
3      5/30/2018       CL
3      5/30/2018       TH
3      12/9/2018      AN
4      3/15/2018     GB
4      6/3/2018       BH
4      10/3/2018       AN
;

data want;
length trts $20;
do until (last.date);
    set have; by ID date;
    trts = catx("/", trts, trt);
    end;
drop trt;
run;

proc print data=want noobs;
run;
PG

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1402 views
  • 0 likes
  • 3 in conversation