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
Kurt_Bremser
Super User

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
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 2024

Innovate_SAS_Blue.png

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. 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

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.

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