Hi everyone,
I a trying to get the final code after all the macro variables are resolved. I am using Mfile and Mprint options of the sas to get the generated code and store it in my local system.
I am using something like
filename mprint '........\.mfile.sas';
options mfile mprint macrogen mlogic;
%macro genmacro( vlist, table, filter);
select
&vlist
from
&table
where
&filter
%mend;
So the generated code (a .sas file named: mfile.sas) gets stored at the local machine and entire code is printed in one single line like one string. This can be consumed if the code is small, but if the code is very large then it's very hard for user to understand this and make sense out of it.
Is there any way to get the indentation same as the SQL codes written inside the macro function? Please help me with this.
Thanks in advance for you time and help.
oh I see what you are saying, the select clause is on one line.
I guess you have to post process the file and -for example- add a CR before commas and keywords.
1. The macro language is a text-manipulation language. It normally left justifies text values.
So
%let a=1;
is the same as
%let a= 1;
2. You should not a get a one-line result.
filename mprint "%sysfunc(pathname(WORK))\mfile.sas";
options mfile mprint nomlogic;
%macro genmacro( vlist, table, filter);
data T; X=1;
proc sql;
select 1
from T; quit;
%mend;
%genmacro;
data t;
infile mprint;
input;
put _infile_;
run;
generates:
data T;
X=1;
proc sql;
select 1 from T;
quit;
So I suspect you might generating Unix files and reading them using Windows.
Look up the TERMSTR option.
oh I see what you are saying, the select clause is on one line.
I guess you have to post process the file and -for example- add a CR before commas and keywords.
Thanks for the response. This is helpful. I will post in case of any concerns.
This might get you started:
%macro genmacro( vlist, table, filter);
data T; X=1;
proc sql;
select 1 ,1
from T, T where 1; quit;
%mend;
%genmacro;
data _null_;
infile mprint lrecl=32000 pad;
file "%sysfunc(pathname(WORK))\mfile.sas" lrecl=32000;
input X $32000.;
L=length(X);
if X =: 'select ' then
do until(POS=0);
POS=prxmatch('/ (from|inner|left|right|full|union|intersect|except|where|on|group by|having|order by)/',X);
if POS then do;
put X $varying32000. POS;
X=substr(X,POS+1);
end;
else put X ;
end;
else put X $varying32000. L;
run;
data _null_;
infile "%sysfunc(pathname(WORK))\mfile.sas" ;
input;
put _infile_;
run;
data T;
X=1;
proc sql;
select 1 ,1
from T, T
where 1;
quit;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.