DATA Step, Macro, Functions and more

How to transpose multiple variables in a dataset

Reply
Super Contributor
Posts: 272

How to transpose multiple variables in a dataset

Dear,

In my data the variables are presented as "height" height_raw" height_std" height_UN" height_STD_UN" BMi" bmi_raw" bmi_std" bmi_UN" bmi_STD_UN". I need to transpose the variables.  I did transpose using my code. I want to know if there is a way to simplify the code.

Thank you.

 

data

 

ID  height(N)   height_std(C)   height_UN   height_STD_UN  BMi (N)  bmi_raw(C)  bmi_std(C)   bmi_UN   bmi_STD_UN    dt

 

 1     76              190                   in                cm                       35          35                    35               kg/m2      kg/m2         2015-01-02

 

Output need;

 

ID   Test                N         C         S            U        SU                dt

 

  1   HEIGHT         76        76       190        IN         CM            2015-06-22

   1    BMI               35        35         35    kg/m2      kg/m2      2015-06-22    

 

MYCODE;      

 

proc transpose data=ONE out=TWO (drop= _LABEL_ rename =(_name_=VSTESTCD col1=VSORRES));
by id dt;
var bmi height;
run;

 

I peroformed Proc transpose on other variables  (bmi_raw  height_raw) 4 more times and merged by ID and dt variables to get the output I need.

 

 

Is there a way to simplify the code. Thank you.

SAS Super FREQ
Posts: 8,862

Re: How to transpose multiple variables in a dataset

Posted in reply to knveraraju91
My tendency would be to use a DATA step program and one or more than one ARRAY statements. Or, possibly you wouldn't need an array. Not sure about ARRAY, am sure about DATA step.

cynthia
SAS Super FREQ
Posts: 8,862

Re: How to transpose multiple variables in a dataset

Posted in reply to knveraraju91
Also, I am not sure about why N and C are the same value in your desired output. The way I read your INPUT data (the original), height(N) is 76 and height_std(C) is 190, not 76? So why do N and C both have the same values???
cynthia
Super Contributor
Posts: 272

Re: How to transpose multiple variables in a dataset

Posted in reply to Cynthia_sas

Thanks for the reply. N means Numeric and C means chracter forms.

Super User
Posts: 19,770

Re: How to transpose multiple variables in a dataset

Posted in reply to knveraraju91

Your tables don't come across clearly in the forum. 

Can you please format them so it's easier to see what you're trying to do?

I don't think a proc transpose will work.  

 

If you could provide data as a datastep that's even better ... typing out your data means a lot of people will just skip on to the next question.

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

Super Contributor
Posts: 272

Re: How to transpose multiple variables in a dataset

Posted in reply to Cynthia_sas

Sorry. There is charcter  varaibale missing in the data.

 

data

 

ID  h(N)    h(C)   h_std(C)   h_UN   h_STD_UN  BMi (N)  bmi_raw(C)  bmi_std(C)   bmi_UN   bmi_STD_UN    dt

 

 1     76   76        190           in                cm         35          35                 35               kg/m2      kg/m2         2015-01-02

 

Output need;

 

ID   Test                N         C         S            U        SU                dt

 

  1   HEIGHT         76        76       190        IN         CM            2015-06-22

   1    BMI               35        35         35    kg/m2      kg/m2      2015-06-22    

 

Ask a Question
Discussion stats
  • 5 replies
  • 876 views
  • 0 likes
  • 3 in conversation