I am trying to make an output dataset in this format:
| Name | Age | Day | Minutes |
1 | Ann | 26 | 1 | 51 |
2 | Ann | 26 | 2 | 19 |
3 | Ann | 26 | 3 | 22 |
4 | Bob | 30 | 1 | 43 |
5 | Bob | 30 | 2 | 20 |
6 | Bob | 30 | 3 | 60 |
Here is the instream data I am using and the start of my DATA step. I am confused as to how I set up the array to accomplish the task of combining the Mon, Tue, Wed variable into a Minutes variable
data work.swimminutes;
infile datalines delimiter = ',';
input Name :$5.
Age :2.
Mon :2.
Tues :2.
Wed :2.;
datalines;
Ann,26,51,19,22
Bob,30,43,20,60
Chris,41,48,.,36
Dina,21,32,57,22
;
data Work.SwimLong2;
set WORK.SwimMinutes;
array ???;
do Day = 1 TO 3;
output;
end;
run;
You declared your array correctly, but you never use it anywhere.
I've added the relevant line below, as a spoiler. I would recommend you try it first yourself but the answer is there if you need it.
data Work.SwimLong2;
set WORK.SwimMinutes;
Array Minutes (3) Mon Tues Wed;
do Day = 1 TO 3;
Value = Minutes(day);
output;
end;
run;
You've declared arrays in your previous posts, why the issue in not knowing how to declare an array here?
data Work.SwimLong2;
set WORK.SwimMinutes;
array ???;
do Day = 1 TO 3;
output;
end;
run;
The arrays I have done in the past have been different because I have been creating variables, but in this case I have to change the data set to have a variables that are currently horizontal, in this case Mon Tues Wed and make a variable called Minutes that changes these variables into a format with them all in one column. It is just a different situation, so that is why I am a bit confused. The things I have tried so far just haven't worked.
Post what you've tried then.
This is what I tried but it obviously does not produce the desired results as it still produces the Mon Tues and Wed columns, but just repeats the observations in the DO loop.
data work.swimminutes;
infile datalines delimiter = ',';
input Name :$5.
Age :2.
Mon :2.
Tues :2.
Wed :2.;
datalines;
Ann,26,51,19,22
Bob,30,43,20,60
Chris,41,48,.,36
Dina,21,32,57,22
;
data Work.SwimLong2;
set WORK.SwimMinutes;
Array Minutes (3) Mon Tues Wed;
do Day = 1 TO 3;
output;
end;
run;
You declared your array correctly, but you never use it anywhere.
I've added the relevant line below, as a spoiler. I would recommend you try it first yourself but the answer is there if you need it.
data Work.SwimLong2;
set WORK.SwimMinutes;
Array Minutes (3) Mon Tues Wed;
do Day = 1 TO 3;
Value = Minutes(day);
output;
end;
run;
That makes sense. I felt like I was missing the place where the array was actually referenced, I think I just struggled with how to reference it.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.