DATA Step, Macro, Functions and more

How to concatenate multiple rows into one row by ID number

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to concatenate multiple rows into one row by ID number

I want to combine rows with the same ID from a "long" data set into a "wide" data set based on common ID numbers.  File is currently in excel format.  Some IDs may have only 1 row, but others have up to 30 rows.  There are 5 variables per row (including the ID number).

Thanks!

 

Data have:

IDNumDateResultType
111/1/2013words1
2101/10/2013words3
226/1/2014words4
257/1/2015words2
3233/1/2014words1
346/2/2015words2
424/1/2013words1

 

Data want (with var going up to Num30, Date30, Result30, Type30)

IDNum1Date1Result1Type1Num2Date2Result2Type2Num3Date3Result3Type3
111/1/2013words1        
2101/10/2013words326/1/2014words457/1/2015words2
3233/1/2014words146/2/2015words2    
424/1/2013words1        

 


Accepted Solutions
Solution
‎07-18-2016 11:50 AM
Super User
Posts: 10,044

Re: How to concatenate multiple rows into one row by ID number

The simplest way is using proc means + idgroup .

Or if you have big table check MERGE skill proposed by me , Matt, Arthur.T 

 

http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf

 

 

 

data have;
infile cards expandtabs truncover;
input ID	Num	Date : $20.	Result $	Type;
cards;
1	1	1/1/2013	words	1
2	10	1/10/2013	words	3
2	2	6/1/2014	words	4
2	5	7/1/2015	words	2
3	23	3/1/2014	words	1
3	4	6/2/2015	words	2
4	2	4/1/2013	words	1
;
run;


proc sql noprint;
 select max(n) into : n 
  from (select count(*) as n from have group by id);
quit;
proc summary data=have nway;
class id;
output out=want(drop=_:) idgroup(out[&n] (Num Date Result Type)=);
run;

 

View solution in original post


All Replies
Super User
Posts: 5,438

Re: How to concatenate multiple rows into one row by ID number

Usually this is a format for a report, not a dataset.
But if you actually bed this take a look at PROC TRANSPOSE.
Data never sleeps
Solution
‎07-18-2016 11:50 AM
Super User
Posts: 10,044

Re: How to concatenate multiple rows into one row by ID number

The simplest way is using proc means + idgroup .

Or if you have big table check MERGE skill proposed by me , Matt, Arthur.T 

 

http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf

 

 

 

data have;
infile cards expandtabs truncover;
input ID	Num	Date : $20.	Result $	Type;
cards;
1	1	1/1/2013	words	1
2	10	1/10/2013	words	3
2	2	6/1/2014	words	4
2	5	7/1/2015	words	2
3	23	3/1/2014	words	1
3	4	6/2/2015	words	2
4	2	4/1/2013	words	1
;
run;


proc sql noprint;
 select max(n) into : n 
  from (select count(*) as n from have group by id);
quit;
proc summary data=have nway;
class id;
output out=want(drop=_:) idgroup(out[&n] (Num Date Result Type)=);
run;

 

New Contributor
Posts: 3

Re: How to concatenate multiple rows into one row by ID number

This was exactly what I needed!  Thanks!

Super User
Super User
Posts: 7,993

Re: How to concatenate multiple rows into one row by ID number

Well, if your designing an e.g. a pdf or rtf file, then I would suggest just doing this in the proc report step:

http://support.sas.com/resources/papers/proceedings14/SAS388-2014.pdf

Shows several examples.

 

I would not recommend doing this if your just using this as data as it will make your programming far more complicated than it needs to be, you have the ideal structure already - fixed, if you change it to your suggestion then you need to know how many rows appear, program in to do array looping etc.

New Contributor
Posts: 3

Re: How to concatenate multiple rows into one row by ID number

I have ~2000 unique IDs and a few thousand rows, so I do need a program to avoid mistakes with manually cutting/pasting observations.

 

Thanks

Super User
Super User
Posts: 7,993

Re: How to concatenate multiple rows into one row by ID number

Sorry, what do you mean cutting and pasting?  That sounds like Excel, if so then you are producing a report - look at the link I gave with regards to doing this in the proc report which generates the Excel file.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 2574 views
  • 0 likes
  • 4 in conversation