Search code examples
sassas-ods

SAS ODS Query/Statement print along with it's output


SAS EG

Is there any way I can print the query/statement used to get the output, along with the output, using SAS ODS?

Suppose,

ods pdf file=pdfile;
proc sql;
select a.*
from tab1 a inner join tab2 b
on a.something=b.something
where <>
having <>;
quit;
ods _all_ close;

this would print the OUTPUT generated from the above query. But can I also get the query printed via the ods pdf along with the output?


Solution

  • There's no automatic way to redirect the log that I'm aware of.

    There are a few ways to get what you want, however.

    First off, if you are able to use Jupytr, SAS has plugins to enable that to work with SAS, and then you can simply write in the notebook and run the code, and the results appear with your code just as you want. See Chris Hemedinger's blog post on the subject for more details.

    Second, SAS Studio will support a notebook-style interface probably with the next major revision (I believe version 5.0) which will release late next year. So similarly, you would put your code and get your output in the same windows.

    Finally, the third option is to do as Reeza suggested - write to a log file, then print that to the output. It's messy but possible.

    Here's an example of the latter. I don't make any effort to clean it up, note, you'd probably want to remove the logging related to PROC PRINTTO and the otehr notes (or turn on NONOTE).

    ods pdf file="c:\temp\test.pdf";
    filename logfile temp;
    proc printto log=logfile;
    run;
    proc sql;
    select * from sashelp.class;
    quit;
    proc printto;
    run;
    data _null_;
      infile logfile;
      input @1 @;
      call execute(cats('ods text="',trim(_infile_),'";'));
    run;
    
    ods _all_ close;