BookmarkSubscribeRSS Feed
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;
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: 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 25. 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
  • 4 replies
  • 126 views
  • 2 likes
  • 5 in conversation