1

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?

samkart
  • 6,007
  • 2
  • 14
  • 29
  • Try using PROC PRINTO LOG = 'path' for printing the whole log, from there you can use the query – G.Arima Nov 30 '17 at 07:46
  • Is there no way to print it to the ods output file? I am aware of `proc printto`, but want to get it before or after the table in the ods output. – samkart Nov 30 '17 at 14:48
  • 1
    The really ugly way is use PROC PRINTTO, read back the file and then print it to the ODS. Unfortunately, I'm not aware of any other method to do this. You can add it to the SAS Ballot so it's on the list of requested items though. – Reeza Nov 30 '17 at 16:56
  • That's quite an elegant solution! – samkart Dec 01 '17 at 17:59

1 Answers1

1

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;
Joe
  • 62,789
  • 6
  • 49
  • 67