DATA Step, Macro, Functions and more

Use of return statement

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Use of return statement

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

 


Accepted Solutions
Solution
‎12-16-2015 12:30 PM
Super User
Posts: 19,877

Re: Use of return statement

Posted in reply to SAS_inquisitive

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;

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: Use of return statement

Posted in reply to SAS_inquisitive

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
;
Super Contributor
Posts: 271

Re: Use of return statement

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 !

Solution
‎12-16-2015 12:30 PM
Super User
Posts: 19,877

Re: Use of return statement

Posted in reply to SAS_inquisitive

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;
Super Contributor
Posts: 271

Re: Use of return statement

Thanks Reeza, this example really helped understand return statment.

Respected Advisor
Posts: 3,156

Re: Use of return statement

Posted in reply to SAS_inquisitive

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.

 

Super Contributor
Posts: 271

Re: Use of return statement

Thanks, Haikuo. 

Respected Advisor
Posts: 4,936

Re: Use of return statement

Posted in reply to SAS_inquisitive

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
;
PG
Super Contributor
Posts: 271

Re: Use of return statement

I think this was what I want to see as the effect of return statment.

Super User
Posts: 11,343

Re: Use of return statement

Posted in reply to SAS_inquisitive

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

Super Contributor
Posts: 271

Re: Use of return statement

Thanks, ballardw.

Respected Advisor
Posts: 3,799

Re: Use of return statement

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?
Super User
Posts: 5,518

Re: Use of return statement

Posted in reply to SAS_inquisitive

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.

🔒 This topic is solved and locked.

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

Discussion stats
  • 12 replies
  • 580 views
  • 9 likes
  • 7 in conversation