BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Dear Experts,

 

I have to merge the comments to one row for the duplicate IDs, I ll give you the sample set with expected output.

data aaa;
Input ID Date mmddyy10. Remarks$;
format Date mmddyy10.;
cards;
123 05/05/2002 comment1
123 05/05/2002 comment2
123 08/07/2003 comment1
145 03/17/2005 comment1
145 03/17/2005 comment2
145 10/12/2006 comment1
;

Expected Output

123 05/05/2002 comment1, comment2

123 08/07/2003 comment1

145 03/17/2005 comment1, comment2

145 10/12/2006 comment1

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

please try the below code

 

data aaa;
Input ID Date mmddyy10. Remarks$;
format Date mmddyy10.;
cards;
123 05/05/2002 comment1
123 05/05/2002 comment2
123 08/07/2003 comment1
145 03/17/2005 comment1
145 03/17/2005 comment2
145 10/12/2006 comment1
;

data want;
length newvar $200.;
set aaa;
by id date notsorted;
retain newvar;
if first.date then newvar=remarks;
else newvar=catx(',',newvar,remarks);
if last.date;
run;
Thanks,
Jag

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

In a data step, do

by id date;

Define a new character variable (e.g. remarks) with sufficient length. At first.date, set it to remark, otherwise do

remarks = catx(',',remarks,remark);

Then use a subsetting if at last.date to only keep one observation per id and date.

andreas_lds
Jade | Level 19

Idea:

  • sort "aaa" by Id and Date, then use set+by in a data step
  • you will have to create a new variable to hold all remarks, calculating the length could be interesting
  • join the remarks with catx (as shown by @Kurt_Bremser)
  • use last.date and output to control which obs is written to the dataset
Jagadishkatam
Amethyst | Level 16

please try the below code

 

data aaa;
Input ID Date mmddyy10. Remarks$;
format Date mmddyy10.;
cards;
123 05/05/2002 comment1
123 05/05/2002 comment2
123 08/07/2003 comment1
145 03/17/2005 comment1
145 03/17/2005 comment2
145 10/12/2006 comment1
;

data want;
length newvar $200.;
set aaa;
by id date notsorted;
retain newvar;
if first.date then newvar=remarks;
else newvar=catx(',',newvar,remarks);
if last.date;
run;
Thanks,
Jag
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
  • 3 replies
  • 1086 views
  • 2 likes
  • 4 in conversation