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
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: 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
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: 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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 417 views
  • 2 likes
  • 6 in conversation