I looked at the pdv created by this program. When x=y it is returning to the top of the implicit loop of the data step without outputing ie. this is not written to the pdv. I wonder why is it showing up in the output data set?
data survey; put _all_; input x y; if x=y then return; put _all_; datalines; 21 25 20 20 7 17 ;
*** pdv *****;
x=. y=. _ERROR_=0 _N_=1
x=21 y=25 _ERROR_=0 _N_=1
x=. y=. _ERROR_=0 _N_=2
x=. y=. _ERROR_=0 _N_=3
x=7 y=17 _ERROR_=0 _N_=3
x=. y=. _ERROR_=0 _N_=4
It truncates the data step so nothing after the return is executed. This could be accomplished with if/then but can make processing faster.
data class;
set sashelp.class;
if age=13 then return;
weight=0;
run;
proc print data=class;
run;
Return statement will 'return' to the beginning of the data step AND output the PDV content.
Your code did not capture PDV at the right moment. Try this:
data survey;
put _all_;
input x y;
put _all_;
if x=y then
return;
/* put _all_;*/
datalines;
21 25
20 20
7 17
;
Haikuo, if is not affecting the output data set, then what is the purpose of the return statment? I wonder if there is an example with obvious effect of return statement. Thanks !
It truncates the data step so nothing after the return is executed. This could be accomplished with if/then but can make processing faster.
data class;
set sashelp.class;
if age=13 then return;
weight=0;
run;
proc print data=class;
run;
Thanks Reeza, this example really helped understand return statment.
You were actually seeing the effect of 'return' statement without knowing it.
if x=y then
return;
put _all_;
datalines;
In your original code, when 'return' happens (x=y), the 'put _all_;' is NOT executed, that is why in your log you did not see the PDV content.
Thanks, Haikuo.
You'll get what you expected by taking control of the output process, i.e. by making the output operation explicit:
data survey2;
put _all_;
input x y;
put _all_;
if x=y then return;
output;
datalines;
21 25
20 20
7 17
;
I think this was what I want to see as the effect of return statment.
There are two code branching statements available in the data step, GO TO and Link, that allow moving code that is executed conditionally out of the main program flow. The Return statement is then used to return to the line after the Link or Go To. These branches can be very helpful if you have a longish bit of code that is only executed for a few records so that the main flow of the datastep is easier to follow.
Other program languages have different mechanisms such as BASIC's GOSUB or
Thanks, ballardw.
Return does go back to the first line after the GOTO.
33 data _null_;
34 goto a;
35 put 'Did I return?';
36 stop;
37 a: put 'Did I goto?';
38 return;
39 run;
Did I goto?
NOTE: DATA statement used (Total process time):
real time 0.18 seconds
cpu time 0.01 seconds
40
41 data _null_;
42 link a;
43 put 'Did I return?';
44 stop;
45 a: put 'Did I link?';
46 return;
47 run;
Did I link?
Did I return?
There's a different statement that does what you describe:
if x=y then delete;
By having both available, you get your choice of which tool to use.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.