BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tom
Super User Tom
Super User

@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;
Syntas_error
Quartz | Level 8

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?

 

 

Tom
Super User Tom
Super User
You can think of the _N_ variable as being initialized to 1. Or you can think of it being initialized to zero and that the first thing the data step does is increment it.
That is part the concept of it being an AUTOMATIC variable.
There are also other things that SAS will do that change variable values before your statements execute. Like variables defined by the NOBS= option on a SET statement or the END= option a SET or INFILE statement.
Syntas_error
Quartz | Level 8

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,

Reeza
Super User
Try it.

data _null_;
put _n_;
run;
Tom
Super User Tom
Super User

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

Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 21 replies
  • 3196 views
  • 1 like
  • 3 in conversation