## Use of return statement

Solved
Super Contributor
Posts: 285

# 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_=1x=21 y=25 _ERROR_=0 _N_=1x=. y=. _ERROR_=0 _N_=2x=. y=. _ERROR_=0 _N_=3x=7 y=17 _ERROR_=0 _N_=3x=. y=. _ERROR_=0 _N_=4```

Accepted Solutions
Solution
‎12-16-2015 12:30 PM
Super User
Posts: 23,754

## 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;``````

All Replies
Posts: 3,167

## 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
;``````
Super Contributor
Posts: 285

## 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: 23,754

## 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;``````
Super Contributor
Posts: 285

## Re: Use of return statement

Thanks Reeza, this example really helped understand return statment.

Posts: 3,167

## 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.

Super Contributor
Posts: 285

Thanks, Haikuo.

Posts: 5,535

## 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
Super Contributor
Posts: 285

## Re: Use of return statement

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

Super User
Posts: 13,563

## 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

Super Contributor
Posts: 285

## Re: Use of return statement

Thanks, ballardw.

Posts: 3,852

## 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_;
43            put 'Did I return?';
44            stop;
45          a: put 'Did I link?';
46            return;
47            run;

Did I return?``````
Super User
Posts: 6,781

## 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 and locked.