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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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