BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kcvaldez98
Obsidian | Level 7

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:

DATA WORK.SwimLog2;
SET WORK.SwimMinutes;
BY Name;
ARRAYDay{*} Day1 - Day3;
DO i = 1 TO DIM(Day);
Day = Day{i};
IF MISSING(Day) = 0 THEN OUTPUT;
END;
DROP i Day1 - Day3;
RUN;
 
The end dataset result should look like:

SAS Data Set ‘SwimLong2’

 

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

12

Dina

21

3

22

 

Please advise

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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;

View solution in original post

3 REPLIES 3
kcvaldez98
Obsidian | Level 7
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;
Tom
Super User Tom
Super User

@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;

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 3 replies
  • 884 views
  • 0 likes
  • 3 in conversation