BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

12 REPLIES 12
Haikuo
Onyx | Level 15

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
;
SAS_inquisitive
Lapis Lazuli | Level 10

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 !

Reeza
Super User

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;
SAS_inquisitive
Lapis Lazuli | Level 10

Thanks Reeza, this example really helped understand return statment.

Haikuo
Onyx | Level 15

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.

 

SAS_inquisitive
Lapis Lazuli | Level 10

Thanks, Haikuo. 

PGStats
Opal | Level 21

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
SAS_inquisitive
Lapis Lazuli | Level 10

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

ballardw
Super User

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

SAS_inquisitive
Lapis Lazuli | Level 10

Thanks, ballardw.

data_null__
Jade | Level 19

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?
Astounding
PROC Star

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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 3857 views
  • 16 likes
  • 7 in conversation