DATA Step, Macro, Functions and more

Call Execute not conditionally executing

Posts: 66

Call Execute not conditionally executing

Hi All,
I have some code where I parse a .sas file into a data set. I then
apply some recoding rules to flag whether it is the start of a
statement, the input dataset, the output dataset etc. I am using this
as a basis to develop a code documentation tool.

What I want to do is to combine the rows of code that I parse into
rows back into a field based on the code block (ie: I want line 1
which has 'Proc printto log=mylog new;' and line 2 'run;' to become
field 1 in a new table.

I wrote the following code to do this. combining is based on a col
called start_pos which is a number relating to the data step/proc

%macro getcode(n);
proc sql noprint;
strVar separated by ';'
start_pos =&n;

data testCallExecute;
set progparse2(keep=start_pos code_block);
format code_block $500.;
call execute("%getcode("||start_pos||");");

I have tried the above code either referencing progparse2 via set and
also an array looping through each line. What I get in the output
dataset is the first combined data step for each step. ie: the call
execute statement does not execute the %getcode macro based on the set
dataset value of start_pos. It will always return the first combined

This has me stumped. Any help is appreciated and if this does not
make sense just let me know.
Super Contributor
Posts: 260

Re: Call Execute not conditionally executing

Hi Cameron.
This is a timing issue : the CALL EXECUTE statement just stores code in memory, and executes the whole bunch of stored code after the end of the Data step.
What you would like here is your macro-program to execute within the Data step. It will not. In fact, even if you changed the code to have %GetCode execute for each observation, it would not write anything correct in the output data set, because your macro is SQL, and it cannot be run within the Data step. Remember that macro is just copy & paste ! So if you run %getCode, that means that you execute the proc Sql ... statements just at the time the %getCode statement is read.

I think you could come to an answer without macro coding. Something like that (not tested) :
DATA work.testCallExecute (KEEP=start_pos code_block) ;
SET work.progparse2 ;
BY start_pos ; /* keep track of changing values for start_pos (dataset must be sorted beforehand */
RETAIN code_block ; /* store values in memory from an observation to the following */
LENGTH code_block $ 500 ;
FORMAT code_block $500.;
IF FIRST.start_pos THEN code_block="" ; /* initialize */
code_block=COMPBL(code_block!!" "!!progText!!";") ; /* build up values */
IF LAST.code_block THEN OUTPUT ; /* write when done */
Posts: 66

Re: Call Execute not conditionally executing

You would be correct. Many thanks. Ironically I always forget about retain!. Many thanks. This is far more efficient also.
Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation