BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jane_V
Calcite | Level 5

Hello Everyone,

I am trying to transpose this dataset but don't know how to transpose weightu, tempu and hru under one variable name "UNIT" (these are the units for weight, temp and hr respectively) along with the respective observation. Please help! 

 

data VS;
input usubjid weight weightu $ temp tempu $ hr hru $ visit $ dd mm yy;
datalines;
100 102 KG 38 C 70 BEATS/MIN WEEK1 01 01 2000 
100 225 KG 97 F 71 BEATS/MIN WEEK2 14 01 2000 
100 112 KG 38 C 72 BEATS/MIN WEEK3 28 01 2000 
100 102 KG 38 C 72 BEATS/MIN WEEK4 07 02 2000 
101 220 KG 98.6 C 75 BEATS/MIN WEEK1 01 01 2000 
101 102 KG 38 C 72 BEATS/MIN WEEK2 14 01 2000 
101 104 KG 38 C 73 BEATS/MIN WEEK3 28 01 2000 
;
run;
proc sort data=vs out=sorted_vs;
by usubjid visit dd mm yy;
run;

proc transpose data=sorted_vs out=transp_vs name=TESTCD prefix=results;
by usubjid visit dd mm yy;
var weight temp hr;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

you need to add two more steps to your code to get the units

 

 

data VS;
input usubjid weight weightu $ temp tempu $ hr hru $ visit $ dd mm yy;
datalines;
100 102 KG 38 C 70 BEATS/MIN WEEK1 01 01 2000 
100 225 KG 97 F 71 BEATS/MIN WEEK2 14 01 2000 
100 112 KG 38 C 72 BEATS/MIN WEEK3 28 01 2000 
100 102 KG 38 C 72 BEATS/MIN WEEK4 07 02 2000 
101 220 KG 98.6 C 75 BEATS/MIN WEEK1 01 01 2000 
101 102 KG 38 C 72 BEATS/MIN WEEK2 14 01 2000 
101 104 KG 38 C 73 BEATS/MIN WEEK3 28 01 2000 
;
run;

proc sort data=vs out=sorted_vs;
by usubjid visit dd mm yy;
run;

proc transpose data=sorted_vs out=transp_vs name=TESTCD prefix=results;
by usubjid visit dd mm yy;
var weight temp hr;
run;

proc transpose data=sorted_vs out=transp_vs2 name=TESTCD prefix=units;
by usubjid visit dd mm yy;
var weightu tempu hru;
run;

data all;
merge transp_vs transp_vs2;
by usubjid visit dd mm yy;
run;
Thanks,
Jag

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

you need to add two more steps to your code to get the units

 

 

data VS;
input usubjid weight weightu $ temp tempu $ hr hru $ visit $ dd mm yy;
datalines;
100 102 KG 38 C 70 BEATS/MIN WEEK1 01 01 2000 
100 225 KG 97 F 71 BEATS/MIN WEEK2 14 01 2000 
100 112 KG 38 C 72 BEATS/MIN WEEK3 28 01 2000 
100 102 KG 38 C 72 BEATS/MIN WEEK4 07 02 2000 
101 220 KG 98.6 C 75 BEATS/MIN WEEK1 01 01 2000 
101 102 KG 38 C 72 BEATS/MIN WEEK2 14 01 2000 
101 104 KG 38 C 73 BEATS/MIN WEEK3 28 01 2000 
;
run;

proc sort data=vs out=sorted_vs;
by usubjid visit dd mm yy;
run;

proc transpose data=sorted_vs out=transp_vs name=TESTCD prefix=results;
by usubjid visit dd mm yy;
var weight temp hr;
run;

proc transpose data=sorted_vs out=transp_vs2 name=TESTCD prefix=units;
by usubjid visit dd mm yy;
var weightu tempu hru;
run;

data all;
merge transp_vs transp_vs2;
by usubjid visit dd mm yy;
run;
Thanks,
Jag
Kurt_Bremser
Super User

Or you use a custom data step:

data VS;
input usubjid weight weightu $ temp tempu $ hr hru $ visit $ dd mm yy;
datalines;
100 102 KG 38 C 70 BEATS/MIN WEEK1 01 01 2000 
100 225 KG 97 F 71 BEATS/MIN WEEK2 14 01 2000 
100 112 KG 38 C 72 BEATS/MIN WEEK3 28 01 2000 
100 102 KG 38 C 72 BEATS/MIN WEEK4 07 02 2000 
101 220 KG 98.6 C 75 BEATS/MIN WEEK1 01 01 2000 
101 102 KG 38 C 72 BEATS/MIN WEEK2 14 01 2000 
101 104 KG 38 C 73 BEATS/MIN WEEK3 28 01 2000 
;
run;

data want;
set vs;
array units {3} weightu hru tempu;
array values {3} weight hr temp;
do i = 1 to 3;
  unit = units{i};
  value = values{i};
  output;
end;
keep usubjid unit value visit dd mm yy;
run;
Jagadishkatam
Amethyst | Level 16

another approach would be with arrays which is the simplest

 

data VS;
input usubjid weight weightu $ temp tempu $ hr hru $ visit $ dd mm yy;
datalines;
100 102 KG 38 C 70 BEATS/MIN WEEK1 01 01 2000 
100 225 KG 97 F 71 BEATS/MIN WEEK2 14 01 2000 
100 112 KG 38 C 72 BEATS/MIN WEEK3 28 01 2000 
100 102 KG 38 C 72 BEATS/MIN WEEK4 07 02 2000 
101 220 KG 98.6 C 75 BEATS/MIN WEEK1 01 01 2000 
101 102 KG 38 C 72 BEATS/MIN WEEK2 14 01 2000 
101 104 KG 38 C 73 BEATS/MIN WEEK3 28 01 2000 
;
run;

proc sort data=vs out=sorted_vs;
by usubjid visit dd mm yy;
run;

data want;
set vs;
by usubjid visit dd mm yy;
array testcds(3) weight temp hr;
array testu(3)$ weightu tempu hru;
array testlbl(3)$ ('weight' 'temp' 'hr');
do i = 1 to 3;
if first.yy then testcd=testlbl(i);
if first.yy then result=testcds(i);
if first.yy then units=testu(i);
output;
end;
drop testlbl: i;
run;
Thanks,
Jag
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You would need a few transposes to do it like that. Easier with arrays:

data vs;
  input usubjid weight weightu $ temp tempu $ hr hru $ visit $ dd mm yy;
datalines;
100 102 KG 38 C 70 BEATS/MIN WEEK1 01 01 2000 
100 225 KG 97 F 71 BEATS/MIN WEEK2 14 01 2000 
100 112 KG 38 C 72 BEATS/MIN WEEK3 28 01 2000 
100 102 KG 38 C 72 BEATS/MIN WEEK4 07 02 2000 
101 220 KG 98.6 C 75 BEATS/MIN WEEK1 01 01 2000 
101 102 KG 38 C 72 BEATS/MIN WEEK2 14 01 2000 
101 104 KG 38 C 73 BEATS/MIN WEEK3 28 01 2000 
;
run;

proc sort data=vs out=sorted_vs;
  by usubjid visit dd mm yy;
run;

data want (keep=usubjid visit dd mm yy paramcd aval paru);
  set sorted_vs;
  array test{3} weight temp hr;
  array unit{3} weightu tempu hru;
  do i=1 to 3;
    paramcd=vname(test{i});
    aval=test{i};
    paru=unit{i};
    output;
  end;
run;
Jane_V
Calcite | Level 5

Thank you everyone!

 

At this novice stage, all the solutions (with/ without proc transpose) are really helpful to learn SAS.

 

Best!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 997 views
  • 0 likes
  • 4 in conversation