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

Hello,

 

Long term reader first time poster here in this wonderful community.

 

So the issue I'm having is that when I call a macro from another program using the %include statement and run the macro which will print using the %put statement, the output I get does not appear aligned properly, see below:

-------------------------------------
-  Values for Current Month     -
-Year    (YYYY)   = 2019   -
-Month   (MM)  = 07     -
-Month & Year  (YYYYMM) = 201907 -
-Month & Year  (YYYY-MM) = 2019-07-
-Month & Year  (MMMYY)  = JUL19  -
-------------------------------------

 

but when running the macro directly from the file containing the macro, it is properly aligned, see below:

-------------------------------------
-      Values for Current Month     -
-Year          (YYYY)      = 2019   -
-Month         (MM)        = 07     -
-Month & Year  (YYYYMM)    = 201907 -
-Month & Year  (YYYY-MM)   = 2019-07-
-Month & Year  (MMMYY)     = JUL19  -
-------------------------------------

 

I'd like to set up my macros in a toolbox location which is called using the %include statement in other programs but not suffer from the issue of losing spaces/tabs in the log when printing, can anyone provide some help with this.

 

macro code below:

 

%macro date_list(offset);
	%do i = 0 %to 11;
		%global yyyymm&i. mmmyy&i. yyyy_mm&i. yyyy&i. mm&i.;
			%let yyyy&i. 	= %sysfunc(intnx(month,%sysfunc(today()),-&offset.-&i.),year.);
			%let mm&i.	 	= %sysfunc(putn(%sysfunc(intnx(month,%sysfunc(today()),-&offset.-&i.),month.),z2.));
			%let yyyymm&i. 	= %sysfunc(intnx(month,%sysfunc(today()),-&offset.-&i.),yymmn6.);
			%let mmmyy&i. 	= %sysfunc(intnx(month,%sysfunc(today()),-&offset.-&i.),monyy5.);
			%let yyyy_mm&i. = %sysfunc(intnx(month,%sysfunc(today()),-&offset.-&i.),yymmd.);
		%end;

	data dates;
		do i = 0 to 11;
			yyyy	= put(symget(cats('yyyy',i)),$6.);
			mm		= put(symget(cats('mm',i)),$2.);
			yyyymm 	= put(symget(cats('yyyymm',i)),$6.);
			mmmyy 	= put(symget(cats('mmmyy',i)),$5.);
			yyyy_mm = put(symget(cats('yyyy_mm',i)),$7.);
		output;
		end;
	run;

	proc print data = dates;
	run;

	%put -------------------------------------;
	%put -		Values for Current Month     -;
	%put -Year 			(YYYY) 		= &yyyy0.   -;
	%put -Month 		(MM)		= &mm0.     -;
	%put -Month & Year 	(YYYYMM)	= &yyyymm0. -;
	%put -Month & Year 	(YYYY-MM)	= &yyyy_mm0.-;
	%put -Month & Year 	(MMMYY)		= &mmmyy0.  -;
	%put -------------------------------------;
%mend;

/*options nomprint nomlogic;*/
%date_list(0);
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you open a text file in the program editor and submit the code then SAS will normally replace the tabs with spaces.

If you %include the same file then the tabs are not replaced.

 

Best solution is to not use tabs in your code.  

 

If you really must keep tabs in your code then perhaps you should add a step to pre-process the file and replace the tabs and %INCLUDE the fixed version.

filename code temp;
filename fixed temp;

data _null_;
 file code ;
 put 'data _null_; input; list;cards;'
   / 'This has a' '09'x 'tab character'
   /';'
 ;
run;


data _null_;
  infile code expandtabs;
  file fixed;
  input;
  put _infile_;
run;

%include code / source2 ;
%include fixed / source2;

Results:

383   %include code / source2 ;
NOTE: %INCLUDE (level 1) file CODE is file C:\...\#LN00067.
384  +data _null_; input; list;cards;

RULE:       ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+

385 CHAR    This has a.tab character
    ZONE    56672667260766266676676722222222222222222222222222222222222222222222222222222222
    NUMR    48930813019412038121345200000000000000000000000000000000000000000000000000000000
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


386  +;
NOTE: %INCLUDE (level 1) ending.
387   %include fixed / source2;
NOTE: %INCLUDE (level 1) file FIXED is file C:\...\#LN00068.
388  +data _null_; input; list;cards;

RULE:       ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+
389         This has a      tab character
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


390  +;
NOTE: %INCLUDE (level 1) ending.

 

View solution in original post

9 REPLIES 9
Nahidul
Calcite | Level 5
Its included in my description. if you copy the date_list macro and run it directly, the output is okay. if you call it into another program using the %include statement, the put statement at the bottom goes wild
Reeza
Super User
Can you run both of those cases back to back and post the full log?
ballardw
Super User

TAB characters are notable unreliable. Please look at your post and note that even in the code you posted that the different default width of tabs as treated by the forum make your example not align either.

 

If this were my project I would likely use the information in a  separate data _null_ step to use column controls with PUT to place things where I wanted. Something like:

 

data _null_;
put "---------------------------------------";
put "-       Values for Current Month"                    @39"-";
put "-Year"          @16"(YYYY)"    @26"=" @31"&yyyy0."   @39"-";
put "-Month"         @16"(MM)"      @26"=" @31"&mm0."     @39"-";
put "-Month & Year"  @16"(YYYYMM)"  @26"=" @31"&yyyymm0." @39"-";
put "-Month & Year"  @16"(YYYY-MM)" @26"=" @31"&yyyy_mm0."@39"-";
put "-Month & Year"  @16"(MMMYY)"   @26"=" @31"&mmmyy0."  @39"-";
put "---------------------------------------";
run;

That may be a tad of overkill for controlling where each column appears but if you need to change layout it provides a good example of what might be needed.

Tom
Super User Tom
Super User

Why do your code files contain tabs to begin with?

If you are using SAS to edit your code the various editors have options to replace tabs with spaces.

Nahidul
Calcite | Level 5
I use tabs to help me keep my code easy to read and manage, not everyone's cup of tea I know but it helps me.
Tom
Super User Tom
Super User

If you open a text file in the program editor and submit the code then SAS will normally replace the tabs with spaces.

If you %include the same file then the tabs are not replaced.

 

Best solution is to not use tabs in your code.  

 

If you really must keep tabs in your code then perhaps you should add a step to pre-process the file and replace the tabs and %INCLUDE the fixed version.

filename code temp;
filename fixed temp;

data _null_;
 file code ;
 put 'data _null_; input; list;cards;'
   / 'This has a' '09'x 'tab character'
   /';'
 ;
run;


data _null_;
  infile code expandtabs;
  file fixed;
  input;
  put _infile_;
run;

%include code / source2 ;
%include fixed / source2;

Results:

383   %include code / source2 ;
NOTE: %INCLUDE (level 1) file CODE is file C:\...\#LN00067.
384  +data _null_; input; list;cards;

RULE:       ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+

385 CHAR    This has a.tab character
    ZONE    56672667260766266676676722222222222222222222222222222222222222222222222222222222
    NUMR    48930813019412038121345200000000000000000000000000000000000000000000000000000000
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


386  +;
NOTE: %INCLUDE (level 1) ending.
387   %include fixed / source2;
NOTE: %INCLUDE (level 1) file FIXED is file C:\...\#LN00068.
388  +data _null_; input; list;cards;

RULE:       ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+
389         This has a      tab character
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


390  +;
NOTE: %INCLUDE (level 1) ending.

 

Nahidul
Calcite | Level 5
thanks - I replaced tabs with spaces in the macro file and this has resolved the problem.
Tom
Super User Tom
Super User

@Nahidul wrote:
thanks - I replaced tabs with spaces in the macro file and this has resolved the problem.

Make sure that you don't have tabs inside of your quoted strings (ore macro variable values) and the difference in spacing shouldn't impact your code.

 

It can be a pain since some old time editors used to replace any spaces they could with tabs. I think the idea was to save a few bytes of disk space.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 9 replies
  • 1009 views
  • 2 likes
  • 4 in conversation