BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Batman
Quartz | Level 8

macro generated code in the log from using the MPRINT option often appears as one long stream of continuous text.   Is there a way to retain the indentation and hard returns from the original program code?

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Just to add a reason why the answer is No, when you compile a macro, it does not maintain the carriage returns in the macro code.  Carriage returns could be completely ignored, or be converted to a blank space.  This is one of the reasons you cannot use a CARDS statement inside of a macro.  For example, if you run:

%macro me() ;
data _null_ ;
x="
hello
world
"
;
put x= ;
run ;

%let x=
hello
world 
;
%put &=x ;
%mend me ;

You get:

204  %me()
x=helloworld
X=hello world
The Boston Area SAS Users Group is hosting free webinars!
Next up: Rick Wicklin presents Ten Tips for Effective Statistical Graphics (with SAS code) on Wednesday March 26.
Register now at https://www.basug.org/events.

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@Batman wrote:

macro generated code in the log from using the MPRINT option often appears as one long stream of continuous text.   Is there a way to retain the indentation and hard returns from the original program code?


No

--
Paige Miller
ballardw
Super User

While I can see a very limited use for simple macro code doing such I question the general usefulness.

For example I may have code like this  to conditionally add variables into a list:

      %if %someParam=thisvalue %then %do;
           onevarname
      %end;
     %else %if %otherparameter=thatvalue %then %do;
         othervar
    %end;
  /* other code that eventually ends the list of variables*/

Something like this preserving hard returns would result in something that might look like

 

   set somedataset (keep=<base list of variables>

                 onevarname


                othervarname


   );

with nesting of %if %then macro code pushing some results into a possibly quite ugly horizontal "alignment" by preserving different levels of indentation.

 

It might be easier to save the log, or possibly MFILE generated file, through something removing MPRINT text, like the source file name and such and then run the resulting text through a code formatting program.

Tom
Super User Tom
Super User

@Batman wrote:

macro generated code in the log from using the MPRINT option often appears as one long stream of continuous text.   Is there a way to retain the indentation and hard returns from the original program code?


No.

 

If that is a major concern then use some other method of code generation that creates a text file with the code so it can be run via %INCLUDE statement instead.

Quentin
Super User

Just to add a reason why the answer is No, when you compile a macro, it does not maintain the carriage returns in the macro code.  Carriage returns could be completely ignored, or be converted to a blank space.  This is one of the reasons you cannot use a CARDS statement inside of a macro.  For example, if you run:

%macro me() ;
data _null_ ;
x="
hello
world
"
;
put x= ;
run ;

%let x=
hello
world 
;
%put &=x ;
%mend me ;

You get:

204  %me()
x=helloworld
X=hello world
The Boston Area SAS Users Group is hosting free webinars!
Next up: Rick Wicklin presents Ten Tips for Effective Statistical Graphics (with SAS code) on Wednesday March 26.
Register now at https://www.basug.org/events.
Patrick
Opal | Level 21

@Batman As others already explained the answer is no. If it's about inspecting some specific case then there are some manual workarounds.

Approach 1:

- Write the macro generated code to an external file using the MFILE Macro System Option

- Using SAS Studio or EG open the file (or copy/paste the code into a code window) and use Format Code (with EG: Right click into code window gives you the menu).

Approach 2:

For SQL code where everything is on one line (especially in server logs) what I'm often doing is to copy/paste the SQL into Notepad++ and then use plug-in Poor Man's T-Sql Formatter

 

And as a productivity tip: 

Poor Man's T-Sql Formatter is also really useful if you need to write a SQL for tables with a lot of columns where you need many but not all of these columns. 

What I tend to do to reduce typing (and me misspelling column names) is to run the SQL with the SAS Feedback option and then copy/paste the transformed code from the log into Notepad++ to format and tailor it.

options ps=max;
data work.test;
  array var_{1000} $1;
run;
proc sql feedback noexec;
  select t1.* from work.test t1;
quit;

 

 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1153 views
  • 2 likes
  • 6 in conversation