@Syntas_error wrote:
I will try to repeat my question:
If the first DO-group won't execute until the IF-statement is true, and if no observations are read from sasuser.monthsum until the IF-statement evaluates to true, how can _n_ ever be evaluated to one? This seems like circular reasoning to me. Does the DATA-step iterate before any of these steps and in that case where?
Yes. The data step starts running and then executes the statements in order. In your program the first executable statement is the IF statement. The ARRAY statements are NOT executable. The non executable statements (like ARRAY, LENGTH, FORMAT) don't actually generate anything that needs to run, they just help SAS figure out what you want to happen.
Look at the examples I have posted on this thread, one without any SET statement at all.
You can think of the "iterations" as if SAS wrapped a big DO loop around your whole data step.
do forever ;
increment _N_ ;
.... your code ...
if no explicit OUTPUT statements in code then OUTPUT;
if it looks like you are in an infinite loop then STOP;
enddo;
I appreciate the effort you're putting down but I'm not sure if I can generalize from your examples.
My hangup is this:
How can the IF-statement trigger "itself", doesn't it "have" to evaluate to true before the data step iterates and the automatic variable is incremented by one?
The only way I can see this functioning is if the DATA step "iterates" before the IF-statement is even evalutated, are you saying this is the case?
Right, so in other words, even a DATA-step like the following would have its _n_ set to one?
data filename; run;
The implication then being that setting the condition _n_1 in an IF-statement (inside a DATA-statement that is) is a way to guarantee that statement will be evaluated to true,
@Syntas_error wrote:
Right, so in other words, even a DATA-step like the following would have its _n_ set to one?
data filename; run;
The implication then being that setting the condition _n_1 in an IF-statement (inside a DATA-statement that is) is a way to guarantee that statement will be evaluated to true,
(_n_=1) will be true ONLY on the FIRST iteration of the data step. When it starts the next iteration _N_ will be incremented to 2 and the condition (_n_=1) will be FALSE. Your trivial data step will never start a second iteraton because SAS will recognize that your program isn't reading in any data so it would be an infinite loop if it kept iterating.
Here is an exercise you can try.
Move the second SET statement to the top of your data step (just below the DATA statement).
Does it change how it runs?
If so WHY? If not WHY NOT?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.