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

Hello,

I am trying to transpose the chart one (horizontal)  into the chart two (vertical), by using 

proc transpose data=test;
by project project_sub;
Var a: b: c: ;
run;

however, I wonder whether there are any codes to allow me to transpose into the chart III? thank you

Chart I

ProjectProject_Suba1a2b1b2c1c2
Asuccess      
Afail      
Bsuccess      
Bfail      
Csuccess      
Cfail      

Chart II

ProjectProject_Subnamevalue
Asuccessa1 
Asuccessa2 
Asuccessb1 
Asuccessb2 
Asuccessc1 
Asuccessc2 
Afaila1 
Afaila2 
Afailb1 
Afailb2 
Afailc1 
Afailc2 

 

Chart III

 

ProjectProject_Subname_avaluename_bvaluename_cvalue
Asuccessa1 b1 c1 
Asuccessa2 b2 c2 
Afaila1 b1 c1 
Afaila2 b2 c2 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So convert it to tall stucture.

Parse the two parts of the _NAME_ into two variables.

Re-order it.

Transpose it again.

data have;
  input Project $ Project_Sub $ a1  a2  b1  b2  c1  c2 ;
cards;
A  success 1 2 3 4 5 6
A  fail    5 6 7 8 9 1
B  success 9 1 2 3 4 5
B  fail    4 5 6 7 8 9
C  success 8 9 1 2 3 4
C  fail    3 4 5 6 7 8
;

proc transpose data=have out=step1;
  by project project_sub notsorted;
run;

data step2;
  set step1;
  suffix = substr(_name_,length(_name_));
  _name_ = substr(_name_,1,length(_name_)-1);
run;
proc sort;
  by project project_sub suffix _name_;
run;

proc transpose data=step2 out=want(drop=_name_);
  by project project_sub suffix ;
  id _name_ ;
  var col1;
run;

Tom_0-1653598733641.png

 

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

So convert it to tall stucture.

Parse the two parts of the _NAME_ into two variables.

Re-order it.

Transpose it again.

data have;
  input Project $ Project_Sub $ a1  a2  b1  b2  c1  c2 ;
cards;
A  success 1 2 3 4 5 6
A  fail    5 6 7 8 9 1
B  success 9 1 2 3 4 5
B  fail    4 5 6 7 8 9
C  success 8 9 1 2 3 4
C  fail    3 4 5 6 7 8
;

proc transpose data=have out=step1;
  by project project_sub notsorted;
run;

data step2;
  set step1;
  suffix = substr(_name_,length(_name_));
  _name_ = substr(_name_,1,length(_name_)-1);
run;
proc sort;
  by project project_sub suffix _name_;
run;

proc transpose data=step2 out=want(drop=_name_);
  by project project_sub suffix ;
  id _name_ ;
  var col1;
run;

Tom_0-1653598733641.png

 

Reeza
Super User

Yes, but it takes multiple steps.


Between Chart2 & 3 split the Name variable so that it has the Letter and the Number separated. Then you can transpose it again. I would recommend a different final structure that may be the same overall though. This would be easier to generate if it would work for you. If the values in NAME may change this isn't quite as your example and is slightly more difficult.

 

Project Project_Sub Order valueA valueB valueC
A success 1      
A success 2      
A fail 1      
A fail 2      
Tom
Super User Tom
Super User

If you know the variable names in advance then do it with a single data step.

data want;
  set have;
  length row 8 a b c 8;
  array old [3,2] a1 -- c2;
  array new [3] a b c ;
  do row=1 to 2;
   do col=1 to 3;
     new[col] = old[col,row];
   end;
   output;
  end;
  drop a1 -- c2 col ;
run;

Result

Tom_0-1653599136440.png

 

ballardw
Super User

You may need to provide some sort of actual example data.

 

You cannot have two variables with the same name. So you would need to have something besides "value" as the variable name and you really don't save anything that way. What do you expect to do with the III version that can't be done with the II?

 

A slightly more useful, at least for general purposes table would be something like which could be made with a data step from the data set like Chart I.

 

Project Project_Sub Row a b c
A success 1      
A success 2      
A fail 1      
A fail 2      

 

Tom
Super User Tom
Super User

If you are reading the data from a source text file, such as a CSV File then just read it that way to start with.

data want;
  input Project $ Project_Sub $ @;
  row=1;
  input (a 2*b 2*c) (:) @;
  output;
  row=2;
  input @1 Project $ Project_Sub $ (2*a 2*b 2*c) (:) ;
  output;
cards;
A  success 1 2 3 4 5 6
A  fail    5 6 7 8 9 1
B  success 9 1 2 3 4 5
B  fail    4 5 6 7 8 9
C  success 8 9 1 2 3 4
C  fail    3 4 5 6 7 8
;
Ksharp
Super User
data have;
  input Project $ Project_Sub $ a1  a2  b1  b2  c1  c2 ;
cards;
A  success 1 2 3 4 5 6
A  fail    5 6 7 8 9 1
B  success 9 1 2 3 4 5
B  fail    4 5 6 7 8 9
C  success 8 9 1 2 3 4
C  fail    3 4 5 6 7 8
;

proc transpose data=have out=chart1;
  by project project_sub notsorted;
  Var a: b: c: ;
run;
data temp(index=(x=(project project_sub)));
 set chart1;
 name=compress(_name_,,'ka');
run;
proc sql noprint;
select distinct catt('temp(where=(name="',name,'") rename=(_name_=name_',name,' col1=value_',name,') )') 
 into : merge separated by ' '
  from temp ;
quit;
data want;
 merge &merge. ;
 by  project project_sub;
 output;
 call missing(of _all_);
 drop name;
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
  • 465 views
  • 3 likes
  • 5 in conversation