PRINTTO Procedure

Example 3: Using Procedure Output as an Input File

Features:

PRINTTO statement without options

PRINTTO statement options::
LOG=
NEW
PRINT=

Details

This example uses PROC PRINTTO to route procedure output to an external file and then uses that file as input to a DATA step

Program

   data test;
      do n=1 to 1000;
         x=int(ranuni(77777)*7);
         y=int(ranuni(77777)*5);
         output;
      end;
   run;
filename routed 'output-filename';
   ods listing;
   proc printto print=routed new;
   run;
   proc freq data=test;
      tables x*y / chisq;
   run;
   proc printto print=print;
   run;
   data probtest;
      infile routed;
      input word1 $ @;
      if word1='Chi-Squa' then
         do;
            input df chisq prob;
            keep chisq prob;
            output;
         end;
   run;
   proc print data=probtest;
      title 'Chi-Square Analysis for Table of X by Y';
   run;

Program Description

Generate random values for the variables.The DATA step uses the RANUNI function to randomly generate values for the variables X and Y in the data set A
   data test;
      do n=1 to 1000;
         x=int(ranuni(77777)*7);
         y=int(ranuni(77777)*5);
         output;
      end;
   run;
Assign a fileref and route procedure output to the file that is referenced.The FILENAME statement assigns a fileref to an external file. PROC PRINTTO routes subsequent procedure output to the file that is referenced by the fileref ROUTED. In order to write to an external file, the LISTING destination must be open. See PROC FREQ Output Routed to the External File Referenced as ROUTED below.
filename routed 'output-filename';
   ods listing;
   proc printto print=routed new;
   run;
Produce the frequency counts.PROC FREQ computes frequency counts and a chi-square analysis of the variables X and Y in the data set TEST. This output is routed to the file that is referenced as ROUTED.
   proc freq data=test;
      tables x*y / chisq;
   run;
Close the file.You must use another PROC PRINTTO to close the file that is referenced by fileref ROUTED so that the following DATA step can read it. The step also routes subsequent procedure output to the default destination. PRINT= causes the step to affect only procedure output, not the SAS log.
   proc printto print=print;
   run;
Create the data set PROBTEST. The DATA step uses ROUTED, the file containing PROC FREQ output, as an input file and creates the data set PROBTEST. This DATA step reads all records in ROUTED but creates an observation only from a record that begins with Chi-Squa.
   data probtest;
      infile routed;
      input word1 $ @;
      if word1='Chi-Squa' then
         do;
            input df chisq prob;
            keep chisq prob;
            output;
         end;
   run;
Print the PROBTEST data set.PROC PRINT produces a simple listing of data set PROBTEST. This output is routed to the default destination. See PROC PRINT Output of Data Set PROBTEST, Routed to Default Destination in the Output section.
   proc print data=probtest;
      title 'Chi-Square Analysis for Table of X by Y';
   run;

Output

PROC FREQ Output Routed to the External File Referenced as ROUTED
PROC FREQ Output Routed to the External File Referenced as ROUTED-Screen 1
PROC FREQ Output Routed to the External File Referenced as ROUTED-Screen 2
PROC FREQ Output Routed to the External File Referenced as ROUTED-Screen 3
PROC PRINT Output of Data Set PROBTEST, Routed to the Default Destination
PROC PRINT Output of Data Set PROBTEST, Routed to the Default Destination