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

Hello,

I try to modify my 'Have' data to 'Want'. 

I tried and it looks like I need to do subset and then merge, or transpose then subset and merge.

Is it a smart way to do it all at once? If you were me, what will you do?

stataq_2-1728568287578.png

 

data have;
input group   $ order  L1  L2 ;
datalines;
A1 1 45 36 
A1 2 25 55 
A2 1  88 99 
A2 2 74 78
A3 1 4 . 25
A3 2  88 99 
B4 1 78 65 
B4 2 . . 
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

This question gets asked a lot.    Using PROC transpose you will need to transpose to "full-tall" then flatten.

 

data have;
   input group $ order  L1  L2;
   datalines;
A1 1 45 36 
A1 2 25 55 
A2 1  88 99 
A2 2 74 78
A3 1 4 . 25
A3 2  88 99 
B4 1 78 65 
B4 2 . . 
;
run;

proc print;
   run;
proc transpose data=have out=tall;
   by group order;
   var L:;
   run;
proc sort;
   by group _name_ order; /*your variable order*/
   run;
proc print;
   run;
proc transpose data=tall out=wider(drop=_name_) delim=_;
   by group;
   var col1;
   id _name_ order;
   run;
proc print;
   run;

Capture.PNG

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Wide data sets are difficult to work with, compared to the same data in a long data set. So without a very good reason why you need this wide data set, I would say don't bother.

 

Instead of concentrating on the technical problem of transposing, better would be if you explain the problem from a big picture point of view; and explain what you will do next after you transpose.

--
Paige Miller
data_null__
Jade | Level 19

This question gets asked a lot.    Using PROC transpose you will need to transpose to "full-tall" then flatten.

 

data have;
   input group $ order  L1  L2;
   datalines;
A1 1 45 36 
A1 2 25 55 
A2 1  88 99 
A2 2 74 78
A3 1 4 . 25
A3 2  88 99 
B4 1 78 65 
B4 2 . . 
;
run;

proc print;
   run;
proc transpose data=have out=tall;
   by group order;
   var L:;
   run;
proc sort;
   by group _name_ order; /*your variable order*/
   run;
proc print;
   run;
proc transpose data=tall out=wider(drop=_name_) delim=_;
   by group;
   var col1;
   id _name_ order;
   run;
proc print;
   run;

Capture.PNG

JOL
SAS Employee JOL
SAS Employee

data test;
input group $ order L1 L2;
datalines;
A1 1 45 36
A1 2 25 55
A2 1 88 99
A2 2 74 78
A3 1 4 . 25
A3 2 88 99
B4 1 78 65
B4 2 . .
;
run;

 

proc transpose data=test out=test_L1 prefix=L1_;
by group;
var L1;
id order;
run;

 

proc transpose data=test out=test_L2 prefix=L2_;
by group;
var L2;
id order;
run;

 

data test_m;
merge test_L1 test_L2;
by group;
keep group L:;
run;

 

proc print data=test_m;
run;

Ksharp
Super User
data have;
input group   $ order  L1  L2 ;
datalines;
A1 1 45 36 
A1 2 25 55 
A2 1  88 99 
A2 2 74 78
A3 1 4 . 25
A3 2  88 99 
B4 1 78 65 
B4 2 . . 
;
run;

proc sql noprint;
select max(n) into :n from (select count(*) as n from have group by group);
quit;
proc summary data=have nway;
class group;
output out=want idgroup(out[&n.] (L1 L2)=);
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 507 views
  • 3 likes
  • 5 in conversation