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);
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.
What does the code for that macro look like?
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.
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.
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 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 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.