I need a work around for Proc Transpose. Can you suggest anything?
@VinitvictorCorr wrote:
lets say
data k;
input subject $ height weight bmi;
datalines;
abc 198 87 22.5
pqr 209 98 25
;
run;
Try this:
data want;
set k;
array vars _numeric_;
length name $32 value 8;
do i = 1 to dim(vars);
name = vname(vars{i});
value = vars{i};
output;
end;
keep subject name value;
run;
What is the reason for not using proc transpose?
Please be more specific than this.
Explain why you want to avoid PROC TRANSPOSE. This makes it easier to understand your problem.
One way is to use Array.
Give a Data Set, give explanation for transposing with the expected output data set. It will be then easier to help you.
Your data is Ok. How do you want to re-shape it without using Proc Transpose? Show your Shape.
You have given a Data Set as:
data k; input subject $ height weight bmi; datalines; abc 198 87 22.5 pqr 209 98 25 ; run;
I don't see the variable names as given now as:
subject vstestcd vsorres
Please clarify your correct specification.
@VinitvictorCorr wrote:
the output will be like
SUBJECT VSTESTCD VSORRES
abc Height 198
abc Weight 87
abc BMI 22.5
pqr height 209
pqr weight 98
pqr BMI 25
I already gave you code for that, you only need to change variable names.
My program is similar to @Kurt_Bremser .
data k;
input SUBJECT $ height weight bmi;
datalines;
abc 198 87 22.5
pqr 209 98 25
;
run;
The following data step uses an array to hold the Labels for names of the variables (Height, Weight, BMI). Another array (V) is used to get the values of the variables. Using Do-Loop they are individually output to the new Data Set (WANT).
data want;
array names[3] $ ('Height', 'Weight', 'BMI');
set k;
array v height -- bmi;
do j = 1 to dim(v);
VSTESTCD = names[j];
VSORRES = v[j];
output;
end;
keep SUBJECT VSTESTCD VSORRES;
run;
The output is:
Transposing data tutorials:
Long to Wide:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/
https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/
Wide to Long:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/
https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/
And sometimes a double transpose is needed for extra wide data sets:
https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.