• Print  |
  • Feedback  |

Knowledge Base


TS-549

Is there a way to return the last page number generated by a SAS procedure?

The SAS System does not have an option to return this value,
but the following macro will.  Once you run a PROC that
generates several report pages, invoke %PAGENUM.  This
routes a temporary data set to a file and then reads the
first line of the file searching for the page number.  A
DATA step reads the page number and creates a macro variable
called LASTPAGE that contains the value of the last page
number.


%macro pagenum;
   %global lastpage;
   filename temp 'temporary_file';

   proc printto print=temp new;

   data test;
      x=1;

   options number;
   proc print;

   proc printto;

   data _null_;
   infile temp truncover;
   input record $ 1-200;
   call symput('lastpage',trim(left(put(input(reverse(scan(
      reverse(record),1)),6.)-1,6.))));
   stop;
 run;

 options pageno=&lastpage;
%mend pagenum;

/** Place code which generates your report here  **/

%pagenum
%put *The last page number generated is &lastpage;

* The %GLOBAL statement globalizes the macro variable
  LASTPAGE so it will be available when the macro finishes
  executing.  The FILENAME statement allocates a temporary
  file with a file reference of TEMP.


* PROC PRINTTO routes the PROC PRINT output to the
  temporary file.  The NEW option clears any information
  that may already exist in the file.  This file will be
  used later in the macro.


* To generate a page number, run PROC PRINT on the TEST
  data set.  The NUMBER option specifies that page
  numbers be printed.

  A second PROC PRINTTO is executed so that output is routed
  back to the standard print file.


* In the DATA _NULL_  step the INFILE statement reads
  the TEMP file referenced by the previous PROC PRINTTO
  step.  The TRUNCOVER option allows you to read a value
  that is shorter than its definition on the INPUT
  statement.  The INPUT statement then reads the first line
  into a DATA step variable called RECORD.


* CALL SYMPUT is issued to store the page number into a
  macro variable called LASTPAGE.  Since a page number
  appears at the right side of the first line, the REVERSE
  function is used to return the first line in reverse
  order, which moves the page number to the very front of
  the string.  The SCAN function reads up to the first blank
  thus pulling off the page number.  A second REVERSE
  rotates the digits in the page number back from the
  previous REVERSE.  A 6. informat is used in the INPUT
  function to return a numeric value.  Now in a PUT function
  we subtract one, since PROC PRINT increments the page
  number by one earlier in this macro, and then we convert
  the numeric value to character.  The LEFT function moves
  any leading blanks to the end of the value and then the
  TRIM function removes any trailing blanks.

  The STOP statement causes the SAS System to stop
  processing the current DATA step after it reads the first
  record.


* This macro causes the page number to be incremented by
  one, so the final OPTIONS statement sets the PAGENO option
  back to the value it had prior to the macro invocation.


* The %PUT statement shows the result of the macro
  execution.

NOTE:In this macro we turn the NUMBER option on, so if you
     previously had it off you will need to reset it after
     invoking this macro.  Another note, due to a 200 
     character limit in reading in data there could be a
     problem if your LS is set greater than 200.