BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DK_0808
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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.

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

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.

 

 

 

 

 

ChrisNZ
Tourmaline | Level 20

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.

DK_0808
Calcite | Level 5

Thanks for the response. This is helpful. I will post in case of any concerns. 

ChrisNZ
Tourmaline | Level 20

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1074 views
  • 0 likes
  • 2 in conversation