Create a paper in SAS - sas

Here is another issue I'm facing with SAS:
I have to write a full paper/report in SAS and can't find how to do so. So I already have a multitude of coding (to clean the data, create some new variable, merge three datasets, etc etc etc. I also made some tables and a few plots in the whole process). Now what I need to do for the project is: Write a scientific paper of a only a few pages (introduction, methodology, results, discussion, ...) in SAS. (I think the teacher said something about ods text ="blablabla..." to insert the text.) And of course use some of the tables/plots from my previous coding in this. This paper should be exported into a pdf file.
I was thinking of using
ODS TRACE ON;
...
ODS TRACE OFF;
for the tables I made (code for the creation of the tables is between those two lines of code). But then I just don't understand how I can re-use this output later on when creating my pdf-paper with:
ODS PDF FILE ='c:\...'
...
ODS PDF CLOSE;
I was hoping I could just 'trace' all of the tables and plots I needed in my code. And than open this pdf file, add some titles, some text, some of the tables/plots in between the text, and close the pdf file. However, I have a bad feeling it isn't as simple as that. :'(
Thanks in advance!

Related

SAS : ODS and code highlighting

When using R markdown for making statistical reports, I have the ability to echo the R code in my output document. I'm learning SAS and I was wondering if it was possible to highlight or echo the SAS code in my final ODS report ? I'm using a dirty hack right know to display the code in my document, which is using "ods text = ", but it seems quite redundant. Plus it doesn't add syntax highlighting.
That feature does not exist in the SAS language right now, but it has been mentioned in several talks by Amy Peters, principal product manager of the SAS programing environments, as a planned feature for a near-future SAS release (with no specific date yet, but hopefully in the next 2 years). It would likely be implemented in a similar fashion to Jupyter Notebooks, in that you write your code and get your output inline.
That said, SAS does support Jupyter Notebooks, which is the best current (third party) solution. Contact your SAS administrator for more information.
I have an idea here, I'm the type of guy who dosent take no for answer and find a way to fiddle and get it done... but i think this is abit far fetch... you can try still I think it will work with mostly evrything but may have a hard time to play with quotes when you have multiple semi colomns....
Check:
I started by creating a dumbass dataset :
data tata;
x=1;
run;
then we do the following:
%let code= select * from tata;
proc sql;
create table report as
&code.;
quit;
proc print data=report;
footnote "&code.";
run;
The rationale:
I think it you put your code in macrovariables and then execute those macro variables you would be able to print show the code after by printing the macrovariable following your text...
See the sample

Display webout HTML generated by data steps in a SAS Web Report Studio

Here is how I build reports for SAS EnterpriseGRC:
I write sas base or macro code to collect and process data using
procs and data steps.
Then I use file _webout; in data steps and use
put statements to generate html
Then I register this code as a stored process and define filters which I am already handling in the code.
And then I navigate to Reports tab in EGRC and wala I have a report
For example my code would look like:
proc sql noprint;
CREATE TABLE work.risks AS
SELECT *
FROM opdetail.risk_L;
quit;
data _null_;
file _webout;
put '<html>';
put '<body>';
put '<table>';
put '<tr><td>Risk ID</td><td>Risk RK</td></tr>'
run;
data _null_;
set work.risks;
put '<tr>';
put '<td>'; put risk_id;'</td>'
put '<td>'; put risk_rk;'</td>'
put '</tr>';
run;
data _null_;
put '</table>';
put '</body>';
put '</html>';
run;
Now this is a very simplistic approach but a very effective one since I can theoretically represent my data in any complicated form like a correlation heat etc, as much as HTML allows
Now this technique has only one problem. Clients have a problem printing this report from within EGRC. The code I have written produces a stream output so I cant use this STP in a SAS Web Report Studio report where printing options are available. How can I solve my printing problem in the least complicated way?
p.s I know can build Informationmaps and build reports like they are supposed to but sometimes clients request demands a format of report which SAS Web Report Studio can not handle.
I could put this Print This Page in the HTML, but that puts date and page title on the top of printing page which is out of my control.
Well the least complicated way was to put a javascript print function through HTML inside the report. Though if someone IS still looking for a proper solution than ODS is the answer
Well the simplest way to print your _stream'd html report is via your trusty web browser!
Simply navigate to www.YOURSASMIDTER/SASStoredProcess and select "List Available Stored Processes and Reports". When you find your STP, right click and open in a new tab.
You now have your html report, and the URL which can be shared with end customers - who can apply your registered filters, and print your report in their browser of choice.
Of course this is not a the recommended way to build web based reports with SAS. Your SAS code will get overcomplicated by many / complex put statements (mixing SAS code with html/css/javascript). You will also struggle to apply the thousands of excellent libraries that can jazz up your report (such as highcharts, handsontable, d3).
If you are happy to deliver your reports purely from the browser (instead of EGRC) then a suggested / more scaleable approach is to use the open source SASjs adapter. More info at https://sasjs.io

Exporting multiple results to same Excel Sheet

I have this Macro A. Everytime I run it, I get a result as the image below (include 2 tables and 1 graph). Note that this time I copied them from SAS output window and paste it on Excel then format it manually. My question is, when I run Macro A multiple times, how can I export all the results to a single specific Excel Sheet, one after another automatically?
Since I'm kinda a beginner in SAS, so a simple solution is preffered :)
Thanks a bunch!!
SAS result in excel
Use ODS Excel and sheet_interval option. This assumes SAS 9.4+
Ods excel file='myfile.xlsx' options(sheet_interval='none') style=meadow;
%macro_run();
Ods excel close;
Within the macro you may need to add:
Ods excel options(sheet_interval='none');

Separate report pages when printed out-sas

I have a sas output, and for each person have some information. But each person is supposed to be on a separate page when printed out, in other words that PDF should be one page for each person. I didn't use macro in my code. Also I don't know how to make macro. So is there any way that I can separate pages without using macro?
Code:
data _null_;
set maingroup;
call execute('%bygroup(' || trim(maingroup) || ')');
run;
This code separate the people for each page. But I don't have macro, I changed the code little bit. Check the report as below.:
Ayda Ceyhan: 325
1258 458
Grade:3.0
Issues: Test
-------
Justin Costay: 526
1568 132
Grade:3.5
Issues: NA
This is the output, there are two people in here. I need them to separate for each page when print out.
This depends largely on your actual report; but in general, you should be able to use by groups rather than using macros.
A simple example:
ods pdf file="c:\temp\test.pdf" startpage=bygroup;
proc report data=sashelp.class nowd;
by name;
columns age sex height weight;
run;
ods pdf close;
The startpage=bygroup tells the PDF engine to print out a new page for each by group. You might need to use notsorted if your by variable cannot be sorted on. This may or may not exactly do what you want, depending on how you're producing the report.
If you're doing this with data step programming, you may have a harder time without having access to the macro that's doing it. I honestly wouldn't use data step programming; nowadays, proc report/tabulate/etc. are very good at producing reports in whatever format you want, and they're much more powerful than data step programming.
In your specific simple example, you may be able to issue ods pdf startpage=now; commands via call execute (and then use startpage=never on the original ods pdf statement).

Turn off automatic graph saving in SAS 9.4

In SAS 9.3, I could use ODS HTML GPATH to specify the path where I wanted graphs to be saved (if I so desired). In 9.4, by default (i.e. every time I open SAS) whenever I make a graph (with PROC SGPLOT, e.g.), it automatically saves the plot to the location where the SAS program is saved. I've tried going to Tools --> Options --> Preferences --> Results and unchecking every combination of the HTML and ODS options, but no matter what I'm still getting automatically saved graphs. How can I turn this off? Preferably I'd still have ODS output within SAS, but I do not want these PNG (or whatever) images to be saved to my computer outside SAS automatically.
EDIT: More information because the differences as stated above were not clear.
1) In 9.3 I had to say ODS GRAPHICS ON and specify ODS HTML GPATH in order to have SAS save my plots to my computer outside of SAS (or so I thought). If I wanted ODS graphics inside of SAS, but not save graphs outside of SAS, I could just say ODS GRAPHICS ON and skip the ODS HTML GPATH statement.
2) When I open 9.4 and do not make any statements about ODS (i.e. leave settings at default), but run a procedure such as SGPLOT, I A) get both a html graph (ODS graph that shows up in SAS's 'Results' window) and a graph that I can double-click to open in Windows Photo Viewer, and B) the plot is additionally and automatically saved where my SAS program is located as a PNG.
Trying to stop this automatic graphing saving, I have tried the following in SAS 9.4 before running PROC SGPLOT:
1) ODS GRAPHICS OFF: Nothing changes. I still get everything listed in point (2) above.
2) ODS HTML CLOSE (with ODS GRAPHICS ON): Lose html/ODS version of graph within SAS, but still have graph in SAS I could double-click that opens in Windows Photo Viewer, and still the graph saves automatically to my SAS program's location.
3) ODS GRAPHICS OFF and ODS HTML CLOSE: Same thing as previous case ((2) directly above).
What I want (and I feel like this is how it was in 9.3) is to yes, have ODS graphs come up within SAS (don't really need the version you can double-click to open in Windows Photo Viewer), but no, do not have SAS save a PNG to my computer (specifically, my SAS program's location).
First off, a few notes about what you tried.
ODS GRAPHICS on/off will not have any real effect on SGPLOT or any of the SG procedures; they are all ODS GRAPHICS no matter what. What it does affect is PROC UNIVARIATE and similar procedures that have two types of graphics - old style graphics and ODS GRAPHICS. ODS GRAPHICS ON tells them to use ODS GRAPHICS, and OFF tells them to use the older method.
ODS HTML CLOSE will tell SAS not to produce HTML output, but as long as you have another destination open (ODS LISTING?) it will produce graphs still to the GRAPH destination. Addtionally, the fact that it still produces graphics at all with ODS HTML CLOSE (as opposed to the note "No output destinations active" and no output) tells me you still have a destination open (again, probably LISTING). Thus, ODS HTML GPATH will not necessarily solve your problem (as it will only impact where the HTML output will go). You need to set GPATH for each open destination (which is either LISTING, HTML, or both, depending on the checkboxes in your preferences).
The solution:
Since you want it to go away, your best bet is to make it in your work directory (which is cleaned up by SAS when it properly shuts down).
ods listing gpath="%sysfunc(getoption(work))";
proc sgplot data=sashelp.class;
vbar sex;
run;
Note that the .png files are created (as they always are), but now they go into the work catalog (which you can browse like a sub-library and see each of the files inside).
You could put the initial line in an autoexec.sas file and tell SAS to run that when SAS starts up (-AUTOEXEC option on command line).
You also could uncheck Listing in tools->preferences->Results, and/or use ODS LISTING CLOSE;, and those files should not appear.
Go to Tools --> Options --> Preferences --> Results and uncheck Create listing. It should take care of the automatic saving of PNG files to your program files.
In 9.3, when ODS HTML is on, graphs are defaulted to the user's home directory. They are saved to the hard drive, even if you don't specify a path. Otherwise, there is no way for the browser to display the images. The default location may have moved in 9.4 (I don't have a copy to test), but both versions put png files on your hard drive.