DATA Step, Macro, Functions and more

tranpose variable into obs

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

tranpose variable into obs

hello all,

I'm trying to transpose  my variable into observation.

from

  name       week0     week2   week4 

max011060
bijou0855344
cooper70800568
princess8569688
lucy9276297

and I want the output look like this

 

name week     eos

max   week0   0

          week2   1106

          week4    0

bijou  week0   0

          week2   855

          week4  344

.

.

.

and so on

here is my code

data dogs3;
infile '/folders/myshortcuts/SASUniversityEdition/module 6/dogs3.txt'
firstobs=3
;
input name $ week0 week2 week4;
proc
print data = dogs3;
proc sort data= dogs3;
by name;


proc
transpose data = dogs3
out = newdog3;
by name;
ID week0 week2 week4;
var week0 week2 week4 ;
proc
print data = newdog3;

here is my log

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
61
62 data dogs3;
63 infile '/folders/myshortcuts/SASUniversityEdition/module 6/dogs3.txt'
64 firstobs=3
65 ;
66 input name $ week0 week2 week4;
 
NOTE: The infile '/folders/myshortcuts/SASUniversityEdition/module 6/dogs3.txt' is:
(no system-specific pathname available),
(no system-specific file attributes available)
 
NOTE: 25 records were read from the infile '/folders/myshortcuts/SASUniversityEdition/module 6/dogs3.txt'.
The minimum record length was 32.
The maximum record length was 32.
NOTE: The data set WORK.DOGS3 has 25 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.03 seconds
 
67 proc
68 print data = dogs3;
 
 
NOTE: There were 25 observations read from the data set WORK.DOGS3.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.56 seconds
cpu time 0.46 seconds
 
69 proc sort data= dogs3;
 
70 by name;
71
72
 
NOTE: There were 25 observations read from the data set WORK.DOGS3.
NOTE: The data set WORK.DOGS3 has 25 observations and 4 variables.
NOTE: PROCEDURE SORT used (Total process time):
real time 0.02 seconds
cpu time 0.02 seconds
 
73 proc
74 transpose data = dogs3
 
75 out = newdog3;
76 by name;
77 ID week0 week2 week4;
78 var week0 week2 week4 ;
 
NOTE: There were 25 observations read from the data set WORK.DOGS3.
NOTE: The data set WORK.NEWDOG3 has 75 observations and 27 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
real time 0.03 seconds
cpu time 0.02 seconds
 
79 proc
80 print data = newdog3;
 
81
82
83
84 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
97
 
If you guys have another to create an output such as array would be appreciate.
Thank you guys

 


Accepted Solutions
Solution
‎07-23-2017 08:23 AM
Trusted Advisor
Posts: 1,131

Re: tranpose variable into obs

Please try the arrays as below

 

data have;
input name$       week0     week2   week4 ;
cards;
max	0	1106	0
bijou	0	855	344
cooper	70	800	568
princess	85	69	688
lucy	92	762	97
;

data want;
set have;
array wk  week0     week2   week4;
do over wk;
week=vname(wk);
eos=wk;
output;
end;
keep name week eos;
run;
Thanks,
Jag

View solution in original post


All Replies
Solution
‎07-23-2017 08:23 AM
Trusted Advisor
Posts: 1,131

Re: tranpose variable into obs

Please try the arrays as below

 

data have;
input name$       week0     week2   week4 ;
cards;
max	0	1106	0
bijou	0	855	344
cooper	70	800	568
princess	85	69	688
lucy	92	762	97
;

data want;
set have;
array wk  week0     week2   week4;
do over wk;
week=vname(wk);
eos=wk;
output;
end;
keep name week eos;
run;
Thanks,
Jag
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 174 views
  • 1 like
  • 2 in conversation