- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 10-29-2009 05:38 PM
(12746 views)
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Thank you