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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

1 REPLY 1
Jagadishkatam
Amethyst | Level 16

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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