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

Hi,

Here is my dataset. I would like to have one row for each ID. 

Data: Have

IDD_DATEAABBCCDD
16/10/20155400
17/10/20157003
19/10/20150030
210/2/20150103
210/30/20150000
212/10/20150300
31/8/20160053
32/19/20160200
43/11/20160060
44/7/20160200
55/11/20160003
56/16/20160000

 

Data: Want: Notice that not all IDs have the same number of repeated values. IDs 1 and 2 have 3 repeated measurements and IDs 3,4,5 have only 2.

 

IDD_Date1AA1BB1CC1DD1D_Date2AA2BB2CC2DD2D_Date3AA3BB3CC3DD3
16/10/201554007/10/201570039/10/20150030
210/2/2015010310/30/2015000012/10/20150300
31/8/201600532/19/20160200     
43/11/201600604/7/20160200     
55/11/201600036/16/20160000     
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi again @rajd1  My apologies as I missed to include the D_date variable series in the output in my previous post. Here below, I have modified that piece too to meet your requirement.

 


data have;
input ID	D_DATE :mmddyy10.	AA	BB	CC	DD;
format D_DATE mmddyy10.;	
cards;
1	6/10/2015	5	4	0	0
1	7/10/2015	7	0	0	3
1	9/10/2015	0	0	3	0
2	10/2/2015	0	1	0	3
2	10/30/2015	0	0	0	0
2	12/10/2015	0	3	0	0
3	1/8/2016	0	0	5	3
3	2/19/2016	0	2	0	0
4	3/11/2016	0	0	6	0
4	4/7/2016	0	2	0	0
5	5/11/2016	0	0	0	3
5	6/16/2016	0	0	0	0
;

data temp/view=temp;
 do n=1 by 1 until(last.id);
  set have;
  by id;
  array t D_DATE	AA--DD;
  do over t;
   vn=vname(t);
   temp=t;
   output;
  end;
 end;
 drop D_DATE aa--dd;
run;

proc transpose data=temp out=want(drop=_:);
 by id ;
 var temp ;
 id vn n;
 format d_date: mmddyy10.;
run;

View solution in original post

6 REPLIES 6
Reeza
Super User

Transposing data tutorials:
Long to Wide:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/

https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/

You need to transpose your data. The second example in the first link is what you want.

 


@rajd1 wrote:

Hi,

Here is my dataset. I would like to have one row for each ID. 

Data: Have

ID D_DATE AA BB CC DD
1 6/10/2015 5 4 0 0
1 7/10/2015 7 0 0 3
1 9/10/2015 0 0 3 0
2 10/2/2015 0 1 0 3
2 10/30/2015 0 0 0 0
2 12/10/2015 0 3 0 0
3 1/8/2016 0 0 5 3
3 2/19/2016 0 2 0 0
4 3/11/2016 0 0 6 0
4 4/7/2016 0 2 0 0
5 5/11/2016 0 0 0 3
5 6/16/2016 0 0 0 0

 

Data: Want: Notice that not all IDs have the same number of repeated values. IDs 1 and 2 have 3 repeated measurements and IDs 3,4,5 have only 2.

 

ID D_Date1 AA1 BB1 CC1 DD1 D_Date2 AA2 BB2 CC2 DD2 D_Date3 AA3 BB3 CC3 DD3
1 6/10/2015 5 4 0 0 7/10/2015 7 0 0 3 9/10/2015 0 0 3 0
2 10/2/2015 0 1 0 3 10/30/2015 0 0 0 0 12/10/2015 0 3 0 0
3 1/8/2016 0 0 5 3 2/19/2016 0 2 0 0          
4 3/11/2016 0 0 6 0 4/7/2016 0 2 0 0          
5 5/11/2016 0 0 0 3 6/16/2016 0 0 0 0          

 

rajd1
Quartz | Level 8
Thanks a lot! this works great when there is a limited number of variables.
novinosrin
Tourmaline | Level 20

Hi @rajd1  Please see if this helps-


data have;
input ID	D_DATE :mmddyy10.	AA	BB	CC	DD;
format D_DATE mmddyy10.;	
cards;
1	6/10/2015	5	4	0	0
1	7/10/2015	7	0	0	3
1	9/10/2015	0	0	3	0
2	10/2/2015	0	1	0	3
2	10/30/2015	0	0	0	0
2	12/10/2015	0	3	0	0
3	1/8/2016	0	0	5	3
3	2/19/2016	0	2	0	0
4	3/11/2016	0	0	6	0
4	4/7/2016	0	2	0	0
5	5/11/2016	0	0	0	3
5	6/16/2016	0	0	0	0
;

data temp/view=temp;
 do n=1 by 1 until(last.id);
  set have;
  by id;
  array t 	AA--DD;
  do over t;
   vn=vname(t);
   temp=t;
   output;
  end;
 end;
 drop aa--dd;
run;

proc transpose data=temp out=want(drop=_:);
 by id ;
 var temp;
 id vn n;
run;
novinosrin
Tourmaline | Level 20

Hi again @rajd1  My apologies as I missed to include the D_date variable series in the output in my previous post. Here below, I have modified that piece too to meet your requirement.

 


data have;
input ID	D_DATE :mmddyy10.	AA	BB	CC	DD;
format D_DATE mmddyy10.;	
cards;
1	6/10/2015	5	4	0	0
1	7/10/2015	7	0	0	3
1	9/10/2015	0	0	3	0
2	10/2/2015	0	1	0	3
2	10/30/2015	0	0	0	0
2	12/10/2015	0	3	0	0
3	1/8/2016	0	0	5	3
3	2/19/2016	0	2	0	0
4	3/11/2016	0	0	6	0
4	4/7/2016	0	2	0	0
5	5/11/2016	0	0	0	3
5	6/16/2016	0	0	0	0
;

data temp/view=temp;
 do n=1 by 1 until(last.id);
  set have;
  by id;
  array t D_DATE	AA--DD;
  do over t;
   vn=vname(t);
   temp=t;
   output;
  end;
 end;
 drop D_DATE aa--dd;
run;

proc transpose data=temp out=want(drop=_:);
 by id ;
 var temp ;
 id vn n;
 format d_date: mmddyy10.;
run;
rajd1
Quartz | Level 8
Thanks a lot! this works great with number of variables. I am dealing with 100's of them :). Thanks a lot again Sir
Ksharp
Super User

Merge Skill:

 


data have;
input ID	D_DATE :mmddyy10.	AA	BB	CC	DD;
format D_DATE mmddyy10.;	
cards;
1	6/10/2015	5	4	0	0
1	7/10/2015	7	0	0	3
1	9/10/2015	0	0	3	0
1	6/10/2015	5	4	0	0
1	7/10/2015	7	0	0	3
1	9/10/2015	0	0	3	0
1	6/10/2015	5	4	0	0
1	7/10/2015	7	0	0	3
1	9/10/2015	0	0	3	0
1	6/10/2015	5	4	0	0
1	7/10/2015	7	0	0	3
1	9/10/2015	0	0	3	0
2	10/2/2015	0	1	0	3
2	10/30/2015	0	0	0	0
2	12/10/2015	0	3	0	0
3	1/8/2016	0	0	5	3
3	2/19/2016	0	2	0	0
4	3/11/2016	0	0	6	0
4	4/7/2016	0	2	0	0
5	5/11/2016	0	0	0	3
5	6/16/2016	0	0	0	0
;
data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
run;
proc sql noprint nowarn;
select distinct catt('temp(where=(n=',n,') rename=(
d_date=d_date',n,' aa=aa',n,' bb=bb',n,' cc=cc',n,' dd=dd',n,'))')
into : merge separated by ' '
from temp
order by n;
quit;
data want;
 merge &merge;
 by id;
drop n;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 627 views
  • 2 likes
  • 4 in conversation