DATA Step, Macro, Functions and more

The _N_ variable in a SET statement

Posts: 0

The _N_ variable in a SET statement

Consider the following tables:

Obs Name score
1 Jack 10
2 Mary 11

Obs Tool
1 screwdriver

and the following code:

data MyTable;
SET BigTable;
if _N_=1 then SET SmallTable;

I'll get:

Obs Name score Tool
1 Jack 10 screwdriver
2 Mary 11 screwdriver

However, I don't understand why the value of "tool" is still "screwdriver" for the second observation. Indeed, when the data step comes to the second record of BigTable in the first set statement, _N_ has been incremented, its value is 2 and then the if..then statement returns false; that is, the "SET SmallTable" statement should NOT be executed !
However, it is...

Please help me to understand what happens.

Thanks a lot
Super Contributor
Super Contributor
Posts: 3,174

Re: The _N_ variable in a SET statement

Maybe your post would be better served if you explained what you are attempting to accomplish with your SAS application program?

Scott Barry
SBBWorks, Inc.

Understanding the SAS® DATA Step and the Program Data Vector
Steven First, Systems Seminar Consultants, Madison, WI

SAS Language Reference: Concepts - DATA Step Processing
Posts: 0

Re: The _N_ variable in a SET statement

Well, I don't want to accomplish anything, I just want to understand how SAS works.
I agree my post wasn't clear, but I don't know how to express it better.
Basicaly, I don't understand how the "if _N_=1 then SET" works because it seems to me that the data step iterates 2 times and then the SET statement shouldn't proceed anymore because _N_=2. Message was edited by: babaorumi
Posts: 0

Re: The _N_ variable in a SET statement

SAS is behaving normally.

The first set BigTable statement causes the MyTable data step to loop twice through the two observations in BigTable.
the _N_ value represents the current observation to be output to MyTable.
The if condition _N_ = 1 then set SmallTable causes SAS to read a value from SmallTable. SAS now assumes that you want to retain the value of "tool" in the next iteration because you used a "Set" statement to get the value for tool. It is not re-initializing the value to missing. So, it is working like a merge, sort of.

If you had if _N_ = 1 then tool = "screwdriver"; then SAS would re-initialize the "tool" variable back to missing.

If you were to use the code "Retain tool" then the value of tool would be retained in each iteration.
Posts: 0

Re: The _N_ variable in a SET statement

Ok I understood what's going on. It's a result of the set statement which retains the variables in the PDV and the "if" subsetting statement which immediately returns at the beginning of the data step when it evaluates the condition as false.

Thank you
Ask a Question
Discussion stats
  • 4 replies
  • 2 in conversation