BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aaronh
Quartz | Level 8

Hello,

 

Thanks for bearing with me. 

 

I am having difficulties wrapping my head around a do loop within a two-dimensional array, which is created as a lookup table in reference to the table with 3 observations below (which describes the year, and the monthly revenue for a small business...):

 

data work.reference;
input year mo1-mo12;
datalines;
1997 118 182 876 276 198 653 832 234 546 533 765 423
1998 134 198 900 324 203 666 870 210 520 531 799 376
1999 210 299 823 289 176 543 254 109 170 201 354 276
;
run;

 

The aforementioned array is defined as:

 

data lookup1;
array targets{1997:1999, 12} _temporary_;
if _n_=1 then do i = 1 to 3;
  set work.reference;
  array mnth{*} mo1--mo12;
  do j=1 to 12;
      targets{year, j} = mnth{j};
  end;

set x;
...... run;

In the data step above, I am trying to use the data stored in the lookup table (created in the temp array named 'targets') to do something to the 'x' dataset (omitted here because it's not relevant really).

 

According to an expert (author of the book I am reading actually...), the DO loop executes three times here, once for each observation in work.reference. But I do NOT understand how SAS knows the i=1 to 3 is referring to the row number in work.reference here. I mean within the DO loop, there is no clear mentioning of what 'i' is defined, except in

if _n_=1 then do i = 1 to 3;

 

 

Sorry if this question is too vague or poorly formulated. Some explanations will be much appreciated.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Because every time the set statement executes in the loop, a new observation is read. The dataset pointer is moved with every execution of the set statement, not with every iteration of the data step.

View solution in original post

6 REPLIES 6
Reeza
Super User
One way to think about it, possibly too simplistic, is that the i loop forces the data step loop that is automatic in a regular data step. It forces the loop through the rows for each year and temporary arrays are retained by default so the values are maintained. It also makes them slightly less useful because you need to know the size ahead of time, though that is trivial to determine.
ChrisNZ
Tourmaline | Level 20

> But I do NOT understand how SAS knows the i=1 to 3 is referring to the row number 

It isn't referring to the row number.

The do loop just executes the SET statement thrice.

 

 

Astounding
PROC Star

Perhaps this version would be more intuitive.  But all of the variations depend on knowing what is in the REFERENCE data set:

 

data lookup1;
array targets{1997:1999, 12} _temporary_;
if _n_=1 then do until (done);
  set work.reference end=done;
  array mnth{*} mo1-mo12;
  do j=1 to 12;
      targets{year, j} = mnth{j};
  end;

set x;...... 
run;
Kurt_Bremser
Super User

The SET statement is both declarative and imperative. It determines the dataset(s) to be read (causing the data step compiler to set up buffers and variables), but it also determines the moment when the read occurs during data step execution.

aaronh
Quartz | Level 8

Thanks all for the input! Yes... so I guess my question would be better phrased as:

 

if SAS executes the same exact DO loop 3 times, how does it know to read in the 1st row in work.reference when i=1, 2nd row when i=2, and the 3rd row when i=3?

 

Thanks 

Kurt_Bremser
Super User

Because every time the set statement executes in the loop, a new observation is read. The dataset pointer is moved with every execution of the set statement, not with every iteration of the data step.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 932 views
  • 6 likes
  • 5 in conversation