Hi @sascode
Thank you for the feedback. My original intent was to illustrate the possibility of a Markdown like document in SAS but as you rightly pointed out it is not a complete program by any means.
However, I made some modifications to include macro code and this seems to work.
Here is the updated macro code.
%macro ODSTEXTparseIt(macvarname);
/* parse the code in the macro variable into separate lines of code */
/* print each line using ods text */
/* this code also creates a first level indentation of the code while printing to ODS */
%let totalCount=%sysfunc(countw(%str(&&&macvarname.),';')); /* count number of semi colons to get the number of SAS Statements */
%let instep=0; /* keeps track of whether we are in a data step or a proc - to indent one level */
%do ictr=1 %to &totalCount.; /* loop over number of statements */
%let thisline=%qscan(%str(&&&macvarname.),&ictr,';'); /* parse each line */
%let firstword=%qscan(%str(&thisline.),1,' '); /* get the first word of each line to see if it is data or proc or run statement */
%if "%str(%lowcase(%nrstr(&firstword)))"="%str(run)" or
"%str(%lowcase(%nrstr(&firstword)))"="%str(mend)" %then %let instep=0; /* if its run, then we are not in a step any more */
/* if it is data or proc, then we are entering a step */
%if "%str(%lowcase(%nrstr(&firstword)))"="%str(data)"
or "%str(%lowcase(%nrstr(&firstword)))"="%str(proc)"
or "%str(%lowcase(%nrstr(&firstword)))"="%nrstr(%macro)"
%then %do;
%let instep=1;
%end;
%if not("%str(%lowcase(%nrstr(&firstword)))"="%str(data)"
or "%str(%lowcase(%nrstr(&firstword)))"="%str(proc)" or
"%str(%lowcase(%nrstr(&firstword)))"="%str(run)" or
"%str(%lowcase(%nrstr(&firstword)))"="%nrstr(%macro)")
and &instep=1 %then %do;
ods text=%quote(".... &thisline. ;"); /* if in a step but the line is not the data or proc statement itself, then indent the text */
%end;
%else %do;
ods text=%quote("&thisline. ;"); /* otherwise don't indent the text */
%end;
%end;
/* execute the code contained in the macro variable to create the output */
%unquote(&&&macvarname);
%mend ODSTEXTparseIt;
The call to this macro should use %nrstr (instead of %str) since you don't want the macro code to be resolved until it is actually executed.
%let step3=%nrstr( %macro printclass; proc print data=sashelp.class; run; %mend; %printclass );
In this example code, I noticed that %m (in %macro) doesn't print in html, so I had to switch to PDF to see the correct text printed in the ODS output (partially shown below).
I am pretty sure this will not work in all situations but hopefully provides the idea to extend the concept.
Thank you,
Vj
... View more