DATA Step, Macro, Functions and more

call execute at first pass

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

call execute at first pass

What is the difference between specifying CALL EXECUTE at first iteration and not specifying any interation at all?

data test;
	call execute('proc print data=sashelp.class;run;');
run;

data test;
	if _n_=1 then
		call execute('proc print data=sashelp.class;run;');
run;

Accepted Solutions
Solution
‎03-01-2016 02:31 PM
Respected Advisor
Posts: 3,799

Re: call execute at first pass

[ Edited ]
Posted in reply to SAS_inquisitive

I was referring to "The" documentation.  https://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#p1blnvlvci...

 

Spoiler

Details

If argument resolves to a macro invocation, the macro executes immediately and DATA step execution pauses while the macro executes. If argument resolves to a SAS statement or if execution of the macro generates SAS statements, the statement(s) execute after the end of the DATA step that contains the CALL EXECUTE routine. CALL EXECUTE is fully documented in SAS Macro Language: Reference.

But I realize that %NRSTR is not mentioned.

 

I believe Roland's explanation of the issue is adequate.  Here is the link to the SAS-L tread where he learnt how CALL EXECUTE works. https://listserv.uga.edu/cgi-bin/wa?A2=ind1210c&L=SAS-L&D=0&X=83E8E1025408227B5F&Y=datanull%40gmail....

View solution in original post


All Replies
Super User
Super User
Posts: 7,955

Re: call execute at first pass

[ Edited ]
Posted in reply to SAS_inquisitive

Not a lot.  If you have a data step, with no set clause like that, an empty observation is created, hence because there is one observation the call execute gets executed exactly once.  In your second example, exactly the same thing happens, one blank observation is create and it has _n_=1, so call execute happens once also. 

Respected Advisor
Posts: 3,799

Re: call execute at first pass

Posted in reply to SAS_inquisitive

Notice that both data steps produce the same data set output.

 

NOTE: The data set WORK.TEST has 1 observations and 0 variables.

I've yet to find a use for that. :-)  Although a 0 obs 0 vars can be useful.

 

 

Probably a case for 

data _null_;

makes you look you know what you're doing.

Super Contributor
Posts: 271

Re: call execute at first pass

Posted in reply to data_null__

 @RW9 @data_null__. Thank you. The other thing that baffles me when the argument of CALL EXECUTE is some macro.  CALL EXECUTE sends its argument to macro processor for execution. Doesn't macro code go to macro processor without the need of CALL EXECUTE?

Respected Advisor
Posts: 3,799

Re: call execute at first pass

Posted in reply to SAS_inquisitive

Macro code or a macro call is executed immediately by CALL EXECUTE any code emitted by calling a macro or code written directly by CALL EXECUTE is "stacked" for execution after the data step that is doing the CALL EXECUTEing is done.  This is subtle difference between calling a macro the "regular way" vs EXECUTE and not well understood by most users.

 

See the bit about %NRSTR in the documentation.

Super Contributor
Posts: 271

Re: call execute at first pass

Posted in reply to data_null__

@data_null__. Thank you. 

 

I found this one (%nrstr in call excute).

 

http://www.datasavantconsulting.com/roland/call_exec.html

Super User
Super User
Posts: 7,955

Re: call execute at first pass

Posted in reply to SAS_inquisitive

Do note, I generally avoid using the two together, there is really no need, they both do a similar job at the end of the day, so its simpler to choose one or the other approach.  That being said, 90% of the time you don't need to use either, Base SAS can do most things.

Super Contributor
Posts: 271

Re: call execute at first pass

@RW9 You wrote "Do note, I generally avoid using the two together".

 

two means CALL EXECUTE and Macro?

 

I really want to expand my sas skills horizon that's why I want to learn useful new technique if there is. I have found people using CALL EXECUTE in their program.I have not used it in my program yet. But curious if I can use in my case. 

Super User
Super User
Posts: 7,955

Re: call execute at first pass

Posted in reply to SAS_inquisitive

Yes, only use one method to generate code, either macro or call execute.  They both have their benefits and drawbacks, but ultimately do a similar thing.

Its really down to what the code is to be used for.  If its a one off program, then use Base SAS which can basically do 99% of things, and if you really need to repeat code, use call execute.  If you need reusable code, then macro is probably better.

Solution
‎03-01-2016 02:31 PM
Respected Advisor
Posts: 3,799

Re: call execute at first pass

[ Edited ]
Posted in reply to SAS_inquisitive

I was referring to "The" documentation.  https://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#p1blnvlvci...

 

Spoiler

Details

If argument resolves to a macro invocation, the macro executes immediately and DATA step execution pauses while the macro executes. If argument resolves to a SAS statement or if execution of the macro generates SAS statements, the statement(s) execute after the end of the DATA step that contains the CALL EXECUTE routine. CALL EXECUTE is fully documented in SAS Macro Language: Reference.

But I realize that %NRSTR is not mentioned.

 

I believe Roland's explanation of the issue is adequate.  Here is the link to the SAS-L tread where he learnt how CALL EXECUTE works. https://listserv.uga.edu/cgi-bin/wa?A2=ind1210c&L=SAS-L&D=0&X=83E8E1025408227B5F&Y=datanull%40gmail....

Super Contributor
Posts: 271

Re: call execute at first pass

Posted in reply to data_null__

@data_null__. Thank you.

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 341 views
  • 4 likes
  • 3 in conversation