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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 617 views
  • 2 likes
  • 4 in conversation