Output values to excel in sas - sas

Can some one please help me to output the data generated from the following code(yt) to an excel data set?
proc iml;
d = 0.4;
call farmasim(yt, d) n=10 sigma=2 seed=123457;
print yt;
run;

ods tagsets.excelxp file="myfile.xml";
proc iml ... etc. ... quit;
ods tagsets.excelxp close;
For more info google "tagsets.excelxp" or see http://support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html
Alternately, you can move yt into a dataset, and use PROC EXPORT, either to EXCEL if you have ACCESS to PC FILES licensed, or to CSV if not.

Related

SAS only include chi-square output in proc freq

I'm using the following code to do a chi-square test in SAS.
proc freq data=data;
tables var1*var2/chisq;
run;
It gives the output of both chi-square and Fisher's Test. Anyway to only include chi-square test in the output?
You can use ODS SELECT/EXCLUDE to control what is displayed. If you don't want the fishers test you can exclude that table, assuming the table name is FISHERSEXACT.
ods exclude fishersexact;
Alternatively you can select in only the tables you want and everything else is excluded by default.
ods select crosstabfreqs chisquare;
I do not think you can supress printing some of the tables in PROC FREQ; it seems to me you can supress all, or none.
However, still you can work as follows:
First, do a one-off investigation: issue the ods trace on / label before your statement so that the Log contains labels of the ODS tables:
ods trace on / label;
proc freq data=data;
tables var1*var2/chisq;
run;
ods trace off;
Alternatively, find the name of your table in PROC FREQ docs.
Once you know the table name, run your code again but this time save the respective ODS table to a regular table. This is done by issuing ods output <ODS=table-name>= <table-to-save-it-into>; in front of your command. Assuming you want to save the ChiSq table the code looks like:
ods output ChiSq=work.my_chisq_table;
proc freq data=data;
tables var1*var2/chisq;
run;
The chi-square table will be saved as work.my_chisq_table and you can print it elsewhere.

ods excel and proc template

I am running into an error when trying to use ods excel after defining a style named excel via ods template. I was wondering if anyone could explain why this is happening as I thought ods styles and ods destinations were two completely separate things.
The following ods excel statement works fine:
ods excel file="%sysfunc(pathname(work))\x.xlsx";
proc print data=sashelp.class;
run;
ods excel close;
But if I try to run it after running the below proc template code, I get an error.
proc template;
define style excel;
parent=styles.htmlblue;
class graph / attrpriority='none';
style graphdata1 / contrastColor=#416FA6 markersymbol='circlefilled';
style body from body / pagebreakhtml=_undef_; * REMOVE THE HORIZONTAL RULE;
end;
run;
ods excel file="%sysfunc(pathname(work))\x.xlsx";
proc print data=sashelp.class;
run;
ods excel close;
The error is:
ERROR: Could not find method.
ERROR: No body file. EXCEL output will not be created.
I can just rename my style to something other than excel to fix the issue, but I don't understand why this is happening in the first place. Is anyone able to explain? Thanks.
From comments, thanks #Tom:
Use ods styles.excel instead of ods excel:
ods styles.excel file="%sysfunc(pathname(work))/x.xlsx";
proc print data=sashelp.class;
run;
ods styles.excel close;

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?
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;

SAS ODS output formatted weird

I'm trying to export a content to Excel. I use the below code but my output excel formatting is horrible.
ods excel file= "&cur_path/&project_name._Proc_Means.xlsx" style=printer ;
proc means data=&this_lib..&this_data;
run;
ods excel close;
The output looks like
The huge blank gap makes the file unreadable. I also find out that it puts all the outputs in the same row instead of many different rows.
Any suggestions on how to fix it?
Thanks in advance.
Assuming you have SAS 9.3+, which you must to use ODS EXCEL, you can add the stackodsoutput option to the PROC MEANS statement; that will give you a much more nicely formatted sheet.
ods excel file= "c:\temp\Proc_Means.xlsx" style=printer ;
proc means data=sashelp.cars stackodsoutput;
run;
ods excel close;
If you have prior to 9.3, you may want to use the OUT= option in PROC MEANS and then output the dataset yourself using PROC EXPORT or PROC PRINT. The default PROC MEANS ODS output is not very table-friendly.

How to have multiple graphs generated by one gplot procedure output to a single PDF file?

The following GPLOT procedure generates many graphs(It gives sales by different product). Say if my product has 'Sofa', 'bed', 'Chairs', it will give 3 graphs, one for sofa, one for chairs, one for bed.
I'd like to have all the three graphs generated to be output to one single PDF file. I tried the following, but it only keep the last graph generated. Any ideas how I can do this?
ODS PDF FILE= 'OUTPUT.PDF';
PROC GPLOT data = AB.TEMP;
plot sales*Months=Product;
by Region;
run;
ODS PDF CLOSE;
Thanks!
Sandwich your code between ODS PDF and ODS PDF CLOSE statements.
ODS PDF FILE='my_file.pdf' style=meadow;
PROC GPLOT data = AB.TEMP;
plot sales*Months=Product;
by Region;
run;
ODS PDF CLOSE;
Does this work for you? If so, then you have something wrong in your code. Post your code and log in that case.
proc sort data=sashelp.cars out=cars;
by origin;
run;
ods pdf file="C:\_localdata\temp.pdf" style=meadow;
proc gplot data=cars;
plot mpg_city*msrp=make;
by origin;
run;
ods pdf close;