DATA Step, Macro, Functions and more

Non-executable set statement

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

Non-executable set statement

This chunk of code inherited by previous author.

 

data test;
	if 0 then
		set sashelp.class;
    stop;
run;

This code produces zero observation. Without stop, there is one observation in the test data.  I thought non-executable set statment should not produce any observation even without stop statement. 


Accepted Solutions
Solution
‎12-16-2015 10:36 PM
PROC Star
Posts: 1,233

Re: Non-executable set statement

Hi,

During COMPILE time, the DATA step constructs the program data vector (PDV), a logical construct that you can think of as a single row (thus vector) that will hold records as they are processed.  When a SET statement executes, it reads a record into the PDV.  When an assignemnt statement executes, it changes a value in the PDV.  When an OUTPUT statement executes, it outputs the values in the PDV to a dataset.

 

When the PDV is constructed, it has null values.  If you OUTPUT a PDV with null values, you output a record with null values.

 

Below are a few examples that may be helpful:

 

140  data OneObs;
141    if 0 then set sashelp.class;
142    *implied OUTPUT;
143  run;

NOTE: DATA STEP stopped due to looping.
NOTE: The data set WORK.ONEOBS has 1 observations and 5 variables.

144
145  data ZeroObs;
146    if 0 then set sashelp.class;
147    stop;
148    *implied OUTPUT;
149  run;

NOTE: The data set WORK.ZEROOBS has 0 observations and 5 variables.

150
151  data OneObs;
152    if 0 then set sashelp.class;
153    output;
154    stop;
155  run;

NOTE: The data set WORK.ONEOBS has 1 observations and 5 variables.

156
157  data TwoObs;
158    if 0 then set sashelp.class;
159    output;
160    output;
161    stop;
162  run;

NOTE: The data set WORK.TWOOBS has 2 observations and 5 variables.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,115

Re: Non-executable set statement

This is just the standard DATA step behavior. To cite the documentation: "When SAS executes the last statement in the DATA step, all values in the PDV ... are written as a single observation to the data set ..." However, the STOP statement "causes SAS to stop processing the current DATA step immediately", i.e. the PDV is not written to the output dataset.

 

Please note that the PDV contains only missing values in that one observation you obtain without STOP, not any value from SASHELP.CLASS. The IF condition 0 being false, no values are retrieved from SASHELP.CLASS.

Regular Contributor
Posts: 234

Re: Non-executable set statement

[ Edited ]

FreelanceReinhard, I checked the difference between compile phase and execution phase before posting this question. I know  intializing to missing values was the part of the execution phase. Since the set statment never executes, I thought it does not  initialize ; just compile the variable names only. So my take was even without stop statment it should contain only variable names  not missing observartions. 

http://www2.sas.com/proceedings/sugi23/Begtutor/p50.pdf

Solution
‎12-16-2015 10:36 PM
PROC Star
Posts: 1,233

Re: Non-executable set statement

Hi,

During COMPILE time, the DATA step constructs the program data vector (PDV), a logical construct that you can think of as a single row (thus vector) that will hold records as they are processed.  When a SET statement executes, it reads a record into the PDV.  When an assignemnt statement executes, it changes a value in the PDV.  When an OUTPUT statement executes, it outputs the values in the PDV to a dataset.

 

When the PDV is constructed, it has null values.  If you OUTPUT a PDV with null values, you output a record with null values.

 

Below are a few examples that may be helpful:

 

140  data OneObs;
141    if 0 then set sashelp.class;
142    *implied OUTPUT;
143  run;

NOTE: DATA STEP stopped due to looping.
NOTE: The data set WORK.ONEOBS has 1 observations and 5 variables.

144
145  data ZeroObs;
146    if 0 then set sashelp.class;
147    stop;
148    *implied OUTPUT;
149  run;

NOTE: The data set WORK.ZEROOBS has 0 observations and 5 variables.

150
151  data OneObs;
152    if 0 then set sashelp.class;
153    output;
154    stop;
155  run;

NOTE: The data set WORK.ONEOBS has 1 observations and 5 variables.

156
157  data TwoObs;
158    if 0 then set sashelp.class;
159    output;
160    output;
161    stop;
162  run;

NOTE: The data set WORK.TWOOBS has 2 observations and 5 variables.
Regular Contributor
Posts: 234

Re: Non-executable set statement

Thanks Quentin.

Regular Contributor
Posts: 234

Re: Non-executable set statement

Thanks, FreelanceReinhard.
Respected Advisor
Posts: 4,651

Re: Non-executable set statement

[ Edited ]

Try

 

data test1; run;
/* or */
data test2; stop; run;
/* or */
data test3 test4;
output test3;
stop;
output test4;
run;

and read about What causes a data step to stop executing.

PG
Regular Contributor
Posts: 234

Re: Non-executable set statement

Thanks, PG. This helps a lot.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 385 views
  • 3 likes
  • 4 in conversation