Hi SAS coders,
I am trying to restructure a dataset using an array and do loop and I keep getting errors. My datelines are:
DATA WORK.SwimMinutes;
INFILE DATALINES;
INPUT Name $1-5
Age 7-8
Mon 10-11
Tues 13-14
Wed 16-17 ;
DATALINES;
Ann 26 51 19 22
Bob 30 43 20 60
Chris 41 48 . 36
Dina 21 32 57 22
;
My code that I have used is:
SAS Data Set ‘SwimLong2’
Please advise |
@kcvaldez98 wrote:
I did that but I want Day to be a variable that reflects Mon as 1, Tues as 2, and Wed as 3. I have the code that I've used but I am missing something:
DATA WORK.SwimLog2;
SET WORK.SwimMinutes;
BY Name;
ARRAY Day{*} Mon Tues Wed;
DO i = 1 TO DIM(Day);
Minutes = Day{i};
IF MISSING(Minutes) = 0 THEN OUTPUT;
END;
DROP i Mon Tues Wed;
RUN;
You are using I as the variable that has 1,2,3 in it.
You cannot use DAY as that variable since you already used DAY as the name of the ARRAY.
You could use a different name for the array and then use DAY in the DO loop.
data work.swimlog2;
set work.swimminutes;
by name;
array days Mon Tues Wed;
do day = 1 to dim(days);
minutes = days[day];
if not missing(minutes) then output;
end;
drop mon tues wed;
run;
There are no variables Day1-Day3 in your dataset, only Mon, Tue and Wed. Use those in the array definition.
array Day{*} Mon Tue Wed;
@kcvaldez98 wrote:
I did that but I want Day to be a variable that reflects Mon as 1, Tues as 2, and Wed as 3. I have the code that I've used but I am missing something:
DATA WORK.SwimLog2;
SET WORK.SwimMinutes;
BY Name;
ARRAY Day{*} Mon Tues Wed;
DO i = 1 TO DIM(Day);
Minutes = Day{i};
IF MISSING(Minutes) = 0 THEN OUTPUT;
END;
DROP i Mon Tues Wed;
RUN;
You are using I as the variable that has 1,2,3 in it.
You cannot use DAY as that variable since you already used DAY as the name of the ARRAY.
You could use a different name for the array and then use DAY in the DO loop.
data work.swimlog2;
set work.swimminutes;
by name;
array days Mon Tues Wed;
do day = 1 to dim(days);
minutes = days[day];
if not missing(minutes) then output;
end;
drop mon tues wed;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.