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.
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.
> 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.
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;
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.
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
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.