RETURN statement not working properly?

Reply
Contributor
Posts: 26

RETURN statement not working properly?

Hi SAS community,

 

According to SAS documentation, the RETURN statement "stops executing statements at the current point in the DATA step and returns to a predetermined point in the step", which means that no statement after the RETURN keyword is executed. Here is the code I wrote to test this function:

 

data days;
input dates date9.;
if dates<'01jan2000'd then return;
format dates weekdate30.;
datalines;
31mar1988
07jun2010
05aug1999
08may2013
27nov2015
;
run;

proc print data=days;
run;

However, upon running the above code you get the following output:

 

Obs 	dates
1 	Thursday, March 31, 1988
2 	Monday, June 7, 2010
3 	Thursday, August 5, 1999
4 	Wednesday, May 8, 2013
5 	Friday, November 27, 2015

This is very strange because if nothing executes after the RETURN statement then aren't "31mar1988" and "05aug1999" supposed to be left as a bunch of numbers?

 

Thanks for your help!

Super User
Posts: 3,238

Re: RETURN statement not working properly?

Actually NO executable statements follow your RETURN statement so it has no effect. DATALINES are read in by the INPUT statement which is prior to your RETURN. The FORMAT statement is not executable either so it applies across all rows for the column specified.

 

If it is your intention to subset your data then just use:

 

if dates<'01jan2000'd;
Super User
Posts: 19,188

Re: RETURN statement not working properly?

FYI in over a decade of programming with SAS I've never once used the RETURN statement. 

 

Although it seems useful the loop inherent in a data step has never made it necessary. 

 

Good to know it exists but not particularly useful 

Super User
Posts: 7,465

Re: RETURN statement not working properly?

[ Edited ]

You simply need to read the fine documentation for the return statement. Quote:

Details

 

The point to which SAS returns depends on the order in which statements are executed in the DATA step.

The RETURN statement is often used with the

  • GO TO statement

  • HEADER= option in the FILE statement

  • LINK statement.

When RETURN causes a return to the beginning of the DATA step, an implicit OUTPUT statement writes the current observation to any new data sets (unless the DATA step contains an explicit OUTPUT statement, or REMOVE or REPLACE statements with MODIFY statements). Every DATA step has an implied RETURN as its last executable statement.


 

The behaviour of your data step is perfectly explained.

If you want to prevent writing of certain observations, use a subsetting if.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 4,137

Re: RETURN statement not working properly?

Here a code sample which demonstrates how RETURN works.

data test;
  set sashelp.class;
  if sex='M' then return;
  text='Sex is not M';
run;

What RETURN does is to end the current iteration of the data step and return to the beginning of the next iteration and though all executable statements after "RETURN" won't get executed for this iteration.

 

Your example didn't show differences because the FORMAT statement gets executed only once and alread during the compilation phase of the data step.

 

RETURN (and LINK / RETURN) is very rarely used as you normally use IF...THEN...ELSE constructs for conditional execution of commands.

Super User
Posts: 7,465

Re: RETURN statement not working properly?


expertyejin wrote:

This is very strange because if nothing executes after the RETURN statement then aren't "31mar1988" and "05aug1999" supposed to be left as a bunch of numbers?

 

Thanks for your help


Now I see what you're getting at.

The FORMAT statement is not an executable statement, but a declarative one. It (only) plays a role when the data step is compiled, as it defines attributes for variables when the PDV is established. Therefore that statement (like the LENGTH, ATTRIB or LABEL statements) can not be subject to a condition.

Think of the dataset structure: the format is included only in the header, and is valid throughout all rows. This is not Excel, where any cell in any location can have any attributes.

If you want to have conditionally formatted values, you need to store those formatted values into an extra character column, using the put() function.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 26

Re: RETURN statement not working properly?

Thank you all for your precious help and advice Smiley Happy, I feel so honored to be assisted by several SAS experts! I understand now that only executable statements are masked by the RETURN statement. Also, it looks like while the RETURN statement or any other statements generally associated with it certainly further complements the flexibility of SAS, in real life situations however these statements aren't really programmers' first choice.

Super User
Posts: 3,238

Re: RETURN statement not working properly?

I have had occasion to build LINK routines into complicated DATA steps in which case you would use RETURN. They definitely have a use. but its not that common.

Ask a Question
Discussion stats
  • 7 replies
  • 313 views
  • 7 likes
  • 5 in conversation