DATA Step, Macro, Functions and more

Use of return statement

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
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: 17,829

Re: Use of return statement

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,124

Re: Use of return statement

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
;
Regular Contributor
Posts: 234

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: 17,829

Re: Use of return statement

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;
Regular Contributor
Posts: 234

Re: Use of return statement

Thanks Reeza, this example really helped understand return statment.

Respected Advisor
Posts: 3,124

Re: Use of return statement

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.

 

Regular Contributor
Posts: 234

Re: Use of return statement

Thanks, Haikuo. 

Respected Advisor
Posts: 4,649

Re: Use of return statement

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
Regular Contributor
Posts: 234

Re: Use of return statement

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

Super User
Posts: 10,500

Re: Use of return statement

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

Regular Contributor
Posts: 234

Re: Use of return statement

Thanks, ballardw.

Respected Advisor
Posts: 3,777

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,082

Re: Use of return statement

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.

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

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