BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,
Consider the following tables:

BigTable:
Obs Name score
1 Jack 10
2 Mary 11

SmallTable:
Obs Tool
1 screwdriver

and the following code:

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

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
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
http://support.sas.com/resources/papers/proceedings09/136-2009.pdf


SAS Language Reference: Concepts - DATA Step Processing
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a001281588.htm
deleted_user
Not applicable
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
deleted_user
Not applicable
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.
deleted_user
Not applicable
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 11803 views
  • 1 like
  • 2 in conversation