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 open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.
Find more tutorials on the SAS Users YouTube channel.