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

I have a table that looks like this:

 

CustomerYearWidget AWidget B
Customer A200112
Customer A200234
Customer B200156
Customer B200278

 

And I need to both transpose and merge variables in such a way that it looks like this after:

CustomerWidget A_2001Widget A_2002Widget B_2001Widget B_2002
Customer A1324
Customer B5768

 

Essentially having only 1 row for each customer. How would I go about doing this? proc transpose doesn't seem to be helping me in this situation.

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @Ani7 

 

You can run successively two proc transpose:

data have;
	infile datalines dlm="09"x;
	input Customer :$20.	Year	Widget_A	Widget_B;
	datalines;
Customer A	2001	1	2
Customer A	2002	3	4
Customer B	2001	5	6
Customer B	2002	7	8
;

proc transpose data=have out= have_tr name=widget;
	var Widget_A Widget_B;
	by Customer year;
run;

proc transpose data=have_tr out= want (drop=_name_) delimiter=_;
	var col1;
	id widget year;
	by Customer;
run;

Capture d’écran 2020-06-09 à 15.30.35.png

Best,

View solution in original post

3 REPLIES 3
ed_sas_member
Meteorite | Level 14

Hi @Ani7 

 

You can run successively two proc transpose:

data have;
	infile datalines dlm="09"x;
	input Customer :$20.	Year	Widget_A	Widget_B;
	datalines;
Customer A	2001	1	2
Customer A	2002	3	4
Customer B	2001	5	6
Customer B	2002	7	8
;

proc transpose data=have out= have_tr name=widget;
	var Widget_A Widget_B;
	by Customer year;
run;

proc transpose data=have_tr out= want (drop=_name_) delimiter=_;
	var col1;
	id widget year;
	by Customer;
run;

Capture d’écran 2020-06-09 à 15.30.35.png

Best,

novinosrin
Tourmaline | Level 20

@ed_sas_member 's approach is right albeit requires a proc sort in between

 

proc transpose data=have out=temp ;
by customer year;
var widget:;
run;

proc sort data=temp;
by customer _name_;
run;

proc transpose data=temp out=want(drop=_:) delimiter=_;
by customer;
id _name_ year;
var col1;
run;
Ksharp
Super User
data have;
	infile datalines expandtabs;
	input Customer :$20.	Year	Widget_A	Widget_B;
	datalines;
CustomerA	2001	1	2
CustomerA	2002	3	4
CustomerB	2001	5	6
CustomerB	2002	7	8
;
proc sql noprint;
select distinct catt('have(where=(year=',year,') rename=(
Widget_A=Widget_A_',year,' Widget_B=Widget_B_',year,' ))') into : merge separated by ' '
from have;
quit;
data want;
 merge &merge;
 by Customer;
 drop year;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1048 views
  • 0 likes
  • 4 in conversation