BookmarkSubscribeRSS Feed
CameronLawson
Obsidian | Level 7
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
statement;

%macro getcode(n);
proc sql noprint;
select
progtext
into:
strVar separated by ';'
from
progparse2
where
start_pos =&n;
run;
%mend;

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

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

This has me stumped. Any help is appreciated and if this does not
make sense just let me know.
2 REPLIES 2
Olivier
Pyrite | Level 9
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) :
[pre]
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 */
RUN ;
[/pre]
Regards,
Olivier
CameronLawson
Obsidian | Level 7
Oliver,
You would be correct. Many thanks. Ironically I always forget about retain!. Many thanks. This is far more efficient also.

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
  • 2 replies
  • 712 views
  • 0 likes
  • 2 in conversation