BookmarkSubscribeRSS Feed
Enne1932
Calcite | Level 5

 

example.PNGHi
I am using SAS Enterprise Guide and was wondering how to transpose several columns into one. For this I need to have the headings transposed in a separate column as in the example to the left. 
I would be great if anyone has a good idea how to do so. 

Thanks a lot! 

10 REPLIES 10
Kurt_Bremser
Super User

Please do NOT post data as pictures. Nobody here is keen to tediously type something off a screenshot so code can be tested.

Post example data in a data step with datalines, as this allows us to recreate your data exactly with a simple copy/paste and submit.

 

Use a double proc transpose:

proc transpose data=have out=trans(rename=(_name_=varname));
var a b c;
run;

proc transpose data=trans out=want (drop=_name_);
by varname;
var col:;
run;
PeterClemmensen
Tourmaline | Level 20

Welcome to the SAS communities 🙂

 

data have;
input a b c;
datalines;
3   0.5 1
1.3 0.9 1
2   3   2
;

proc transpose data=have out=temp(rename=(_name_=var));
   var a b c;
run;

proc transpose data=temp out=want(drop=_name_);
   by var;
   var col:;
run;
Ksharp
Super User
data have;
input a b c;
id+1;
datalines;
3   0.5 1
1.3 0.9 1
2   3   2
;

proc transpose data=have out=temp name=var;
by id;
   var a b c;
run;
proc sort data=temp out=want(drop=id);
by var;
run;
novinosrin
Tourmaline | Level 20
data have;
input a b c;
datalines;
3   0.5 1
1.3 0.9 1
2   3   2
;


data _null_;
if _n_=1 then do;
 dcl hash H (ordered: "A",multidata:'y') ;
   h.definekey  ("v_name") ;
   h.definedata ("v_name","want") ;
   h.definedone () ;
   end;
set have end=lr;
array t a--c;
length v_name $1;
do over t;
v_name=vname(t);
want=t;
h.add();
end;
if lr then h.output(dataset:'want');
run;

Sims
Calcite | Level 5

As a follow-up to this question. My dataset is large to add it after datalines. What can be an alternative in this case?

In fact, my main purpose is to calculate the combined frequency of 2 columns of categorical variables.

Kurt_Bremser
Super User

Please post your question in a new thread. As you have noticed, nobody saw your post and could therefore answer to it; I only stumbled across it by accident. Please do not hijack question threads that are not yours and are hopelessly out of date.

Kurt_Bremser
Super User

And a simple data step method (all variables need to be of the same type):

data want2;
set have;
array vars{*} _all_;
do i = 1 to dim(vars);
  varname = vname(vars{i});
  value = vars{i};
  output;
end;
keep varname value;
run;
Allaluiah
Quartz | Level 8

@Kurt_Bremser  That may look simple, but safer to user _numeric or _char_ . Also you would need to sort your want2 to actually meet OP's requirement stated in the question

Enne1932
Calcite | Level 5

Thanks a lot everyone for your help!

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 10 replies
  • 14282 views
  • 4 likes
  • 7 in conversation