Return to Homepage

Goto the Tip of the Month Archive

Other interesting pages ...
LinkedIn Profile
SAS Cheat Sheet
Useful SAS Code
Full SAS Example
Basic Statistics
Contact Information

SAS Tip of the Month
March 2011
(for SAS and WPS)

When creating output in a text file, I like to have a horizontal line at the bottom followed by the program name and the date and time the program was run -- this latter part is usually right aligned.

This would be easy if the linesize and pagesize were always the same, but different clients of mine want different settings based on their particular standard. For this task the pagesize is not important, but the linesize is.

First, lets get the linesize and create a horizontal line:

   data _null_;
      ls=getoption('LS');  *This will be the linesize of the output;
      horizline=repeat('-',ls-1);  *REPEAT function creates a text string;
      call symput("horizline",strip(horizline));  *Put line as a macro variable;
   run;
   footnote1 "&horizline";

The FOOTNOTE1 statement will now produce a single horizontal line of '-'s for the entire width of the output.

The next piece is to get the name of the program being run AND put the date and time the program is run in the second line of footnotes.

First, lets get the name of the program:

   data _null_;
      length pgmfull $200 pgm $100;
      pgmfull=getoption('sysin');  *Program Name and Directory;
      pgm=scan(pgmfull,-1,'\'); *Program Name only;
      if missing(pgm) then
       pgm='INTERACTIVE';  *If Interactive, name and directory not present when run in BATCH mode;
   run;

Next is the date and time of run;

   data _null_;
      dtrun="Date: &sysdate9., &systime.";
   run;

Now lets bring these two pieces of information together:

   data _null_;
      length pgmfull $200 pgm $100 dtrun $50;
      ls=getoption('ls');  *Need this for linesize;
      pgmfull=getoption('sysin');  *Program Name and Directory;
      pgm='Program: '||scan(pgmfull,-1,'\'); *Program Name only;
      if missing(pgmfull) then
       pgm='Program: INTERACTIVE';
         *If Interactive, name and directory not present -- only in BATCH mode;
      dtrun="Date: &sysdate9., &systime.";
      foot2txt=repeat(' ',ls-1);  *Create initial blank line;
      substr(foot2txt,1,length(pgm))=strip(pgm);
        *Replace the first characters of the string with the program name;
      substr(foot2txt,ls-length(dtrun)+1)=strip(dtrun);
        *Replace the last characters of the string with date and time of run;
      call symput('foot2txt',strip(foot2txt));
   run;
   footnote2 "&foot2txt";

Rather than have two separate datasteps to create our two footnotes, lets create one:

   data _null_;

      ls=getoption('LS');  *This will be the linesize of the output;

      horizline=repeat('-',ls-1);  *REPEAT function creates a text string;
      call symput("horizline",strip(horizline));  *Put line as a macro variable;

      length pgmfull $200 pgm $100 dtrun $50;
      pgmfull=getoption('sysin');  *Program Name and Directory;
      pgm='Program: '||scan(pgmfull,-1,'\'); *Program Name only;
      if missing(pgmfull) then
       pgm='Program: INTERACTIVE';
         *If Interactive, name and directory not present -- only in BATCH mode;
      dtrun="Date: &sysdate9., &systime.";
      foot2txt=repeat(' ',ls-1);  *Create initial blank line;
      substr(foot2txt,1,length(pgm))=strip(pgm);
        *Replace the first characters of the string with the program name;
      substr(foot2txt,ls-length(dtrun)+1)=strip(dtrun);
        *Replace the last characters of the string with date and time of run;
      call symput('foot2txt',strip(foot2txt));

   run;

Now, lets see this in action:

   ========= PROGRAM =========

   *--------------------------------------------------*;
   * Setup;
   *--------------------------------------------------*;

   options ls=68 ps=30 nonumber nodate;
   run;


   *--------------------------------------------------*;
   * Our data;
   *--------------------------------------------------*;

   data USMountains;
      infile cards;
      input name $ 1-28 state $29-36 height 37-41;
   *...x....1....x....2....x....3....x....4.;  * Just a ruler to help in column spec.;
   cards;
   Mount McKinley              Alaska  20320
   Mount McKinley, North Peak  Alaska  19470
   Mount Saint Elias           Alaska  18008
   Mount Foraker               Alaska  17400
   Mount Bona                  Alaska  16500
   Mount Blackburn             Alaska  16390
   Mount Sanford               Alaska  16237
   South Buttress              Alaska  15885
   Mount Vancouver             Alaska  15700
   Mount Churchill             Alaska  15638
   ;
   run;


   *--------------------------------------------------*;
   * Setup the horizontal line and text with program;
   * name and date/time of run;
   *--------------------------------------------------*;

   data _null_;

      ls=getoption('LS');  *This will be the linesize of the output;

      horizline=repeat('-',ls-1);  *REPEAT function creates a text string;
      call symput("horizline",strip(horizline));  *Put line as a macro variable;

      length pgmfull $200 pgm $100 dtrun $50;
      pgmfull=getoption('sysin');  *Program Name and Directory;
      pgm='Program: '||scan(pgmfull,-1,'\'); *Program Name only;
      if missing(pgmfull) then
       pgm='Program: INTERACTIVE';
         *If Interactive, name and directory not present -- only in BATCH mode;
      dtrun="Date: &sysdate9., &systime.";
      foot2txt=repeat(' ',ls-1);  *Create initial blank line;
      substr(foot2txt,1,length(pgm))=strip(pgm);
        *Replace the first characters of the string with the program name;
      substr(foot2txt,ls-length(dtrun)+1)=strip(dtrun);
        *Replace the last characters of the string with date and time of run;
      call symput('foot2txt',strip(foot2txt));

   run;


   *--------------------------------------------------*;
   * Output;
   *--------------------------------------------------*;

   proc print data=USMountains noobs label;
      title1 "Listing of the Ten Highest Mountains in the US";
      title2 "Reference: http://www.americasroof.com/fourteeners.shtml, 03MAR2011";
      footnote1 "&horizline";
      footnote2 "&foot2txt";
      var name state height;
      label name='Name'
            state='State'
            height='Height (feet)';
      format height comma8.;
   run;


   ========= OUTPUT =========

              Listing of the Ten Highest Mountains in the US
   Reference: http://www.americasroof.com/fourteeners.shtml, 01MAR2011

                                                       Height
             Name                          State       (feet)

             Mount McKinley                Alaska      20,320
             Mount McKinley, North Peak    Alaska      19,470
             Mount Saint Elias             Alaska      18,008
             Mount Foraker                 Alaska      17,400
             Mount Bona                    Alaska      16,500
             Mount Blackburn               Alaska      16,390
             Mount Sanford                 Alaska      16,237
             South Buttress                Alaska      15,885
             Mount Vancouver               Alaska      15,700
             Mount Churchill               Alaska      15,638












   --------------------------------------------------------------------
   Program: USHighestMountains                   Date: 03MAR2011, 09:17

In nearly all the code I present on this site, the SAS Code and Output is presented for you to copy and play with -- I encourage you to change some of the code and see what it happens.

I hope this is useful. See you in April.

________________________________
Updated March 3, 2011