Hi
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!
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;
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;
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;
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;
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.
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.
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;
@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
Absolutely. The only method that requires only one step is @novinosrin's, but it may run out of resources if a dataset is very large.
Thanks a lot everyone for your help!
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.
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.
Ready to level-up your skills? Choose your own adventure.