SAS- how to PROC EXPORT multiple PROC FREQ created by a macro? - sas

I have a macro which looks like this:
%macro mac_name (st, en=);
%do j=1 %to &en.;
%let k=%eval(&j.+1);
proc freq data=data_name;
tables status&j. * status&k. / nocol norow nopercent missing;
run;
%end;
%mend;
%mac_name (st=1, en=%sysfunc(week(%sysfunc(today()), u)));
The output produces multiple proc freq tables with the same title.
I need this output put into a excel spreadsheet. Ideally all proc freqs in one sheet, one above the other or separate sheets.
Is this possible?
Thanks in advance!!!

The easiest way to do this is to use ODS EXCEL, if you have SAS 9.4.
ods excel file="yourfilename.xlsx";
proc freq data=sashelp.class;
tables age;
run;
proc freq data=sashelp.class;
tables sex;
run;
ods excel close;
You have options for whether they're all on one sheet or separate sheets. You can use ODS TAGSETS.EXCELXP if you have an earlier version of SAS, though they're less "true excel" files. You can also make CSV files or various other things with ODS.
In your case you'd put the opening ODS EXCEL line before the first call of the macro (doesn't have to precede the definition of the macro) and then the ODS EXCEL CLOSE line after the last call.

Related

Write multiple SAS datasets into one Excel sheet

I am using SAS enterprise guide 7.15.
I want to export several datasets into multiple excel sheets (multiple tables in every sheet).
I'm using ODS and even though i'm setting sheet_interval="none", after two tables it breaks the page, and shoves the next tables to another excel sheet.
this is an example to my code, here it's exporting 2 tables, later I want to add 20 tables more in the same sheet:
%macro table_1;
proc report data=table_1 out=sheet_1 headline split='#' spacing=1 nowd missing
style(summary)={font_weight=bold}
columns
segment
diff
cnt_old
cnt_new
;
compute before _page_/style=[font_size=3 font_weight=bold foreground=white background=Dark Blue];
line "table 1";
line ' ';
line 'Number of Customers';
endcomp;
compute after;
endcomp
run;
%mend table_1;
%macro table_3;
proc report data=table_3 out=sheet_1 headline split='#' spacing=1 nowd missing
style(summary)={font_weight=bold}
columns
FinalRiskRating
diff
cnt_old
cnt_new;
compute before _page_/style=[font_size=3 font_weight=bold foreground=white background=Dark Blue];
line "table 3";
endcomp;
compute after;
endcomp
run;
%mend table_3;
%table_1; %table_3;
%let shk = table_1 + table_3;
ods path work.temptemp(update) sasuser.templat(update) sashelp.tmplmst(read);
ods path show;
Options mprint mlogic symbolgen nobyline;
ods listing close;
%macro b;
%do i=1 %to 2;
%let mshk=%scan(&shk., &i.,+);
/*ods tagsets.excelxp options(SHEET_INTERVAL='NONE' PAGEBREAK="NO" sheet_name="sheet1" ABSOLUTE_COLUMN_WIDTH='8' AUTOFIT_HEIGHT='yes' embed_titles_once = 'yes' embedded_titles='yes');*/
ods tagsets.excelxp options(sheet_interval="none" sheet_name="sheet1" ABSOLUTE_COLUMN_WIDTH='8' AUTOFIT_HEIGHT='yes' embed_titles_once = 'yes' embedded_titles='yes');
%&mshk.;
%end;
%mend b;
ods tagsets.excelxp file="&reportlocation";
%b;
ods tagsets.excelxp close;
ods _all_ close;
;
My suspicion is because you don't specify sheet_interval='none' on the initial ods tagsets.excelxp.
Like yours, the first example has this problem:
ods tagsets.excelxp file="h:\temp\test.xls";
ods tagsets.excelxp options(sheet_interval='none');
proc print data=sashelp.class;
run;
ods tagsets.excelxp options(sheet_interval='none');
proc print data=sashelp.class;
run;
ods tagsets.excelxp close;
But this works as expected:
ods tagsets.excelxp options(sheet_interval='none') file="h:\temp\test.xls";
proc print data=sashelp.class;
run;
proc print data=sashelp.class;
run;
ods tagsets.excelxp close;
Interestingly, if you remove the second one, it still works - so it's not exactly that it's not on the first line, but rather it's the new options statement. I guess that resets it somehow. But in your case there's really no reason not to put any of those details on the initial ods tagsets.excelxp statement.
ODS EXCEL doesn't work that way, it always goes all on one sheet. I'd recommend using that, if you can, in any event.

SAS - how to stop results tab opening in sas using code.

I have the below code:
ods _all_ close;
ods csv file="filename.csv"
%macro mac_name (st, en=);
%do j=1 %to &en.;
%let k=%eval(&j.+1);
proc freq data=data_name;
tables status&j. * status&k. / nocol norow nopercent missing;
run;
%end;
%mend;
%mac_name (st=1, en=%sysfunc(week(%sysfunc(today()), u)));
ods csv close;
which works fine.
The only problem is i don't want the results tab to open up, and this has to be done from within the code as i am to schedule the job.
Any ideas?
Thanks in advance!
If you're not running it in batch, I'd recommend using the ods noresults statement. I've posted a simple, reproducible example using Sashelp.Cars below. This was tested in windows SAS 9.4 only.
ods _all_ close;
ods noresults;
ods csv file="filename.csv";
proc print data=Sashelp.Cars;
run;
ods csv close;
ods _all_ close;

SAS ODS Excel to produce a excel sheet with three tables side by side

I am trying to prepare a report in Excel using ODS Excel in SAS. The report contains two sheets with three tables place side by side. I used Panelcol option but it is not available in ODS Excel. Also, the Start_at= option does not seem to be working?
Is there anyway it can be done in ODS Excel? Please let me know.
Any help will be very much appreciated.
Thank you,
Shankar
Shankar:
The ODS EXCEL docs for 9.4 state the start_at= options can not be changed mid-sheet. Link
(START_AT='string') specifies a starting cell for the report. The
default is to start at column 1 and row 1. Default 1,1 Tip This option
cannot be changed in the middle of a sheet. Example ods excel
options(start_at="2,2");
So this example does not work as you expect. The first start_at is honored, and subsequent output on the same page is stacked while still honoring the first start at column.
ods _all_ close;
ods excel file='%temp%\3-tables-in-first-tab-sample.xlsx'
options (sheet_interval='none')
;
ods excel options (start_at="2,2");
* top of output is upper left corner;
proc print noobs data=sashelp.class(keep=name age);run;
* top of output is to the right and down three rows from the first output;
ods excel options (start_at="5,5");
proc print noobs data=sashelp.class(keep=name weight height);run;
ods excel options (start_at="1,9");
proc print noobs data=sashelp.class(keep=name sex);run;
ods excel close;
One way to put multiple tables together is to create a faux table that appends data in a sidewise manner.
Example:
%macro RHS_append (out=, data=);
%if not %sysfunc(exist(&out)) %then %do;
data &out; set &data; run;
%return;
%end;
%local nout names labels;
proc contents noprint data=&out out=outvar(keep=name varnum label);
proc contents noprint data=&data out=datavar(keep=name varnum label);
proc sql noprint; select count(*) into :nout trimmed from outvar;
select
cats(name,'=v',&nout+varnum+1)
, case
when label=''
then cats('v',&nout+varnum+1,'=',quote(trim(name)))
else ''
end
into
:names separated by ' '
, :labels separated by ' '
from datavar order by varnum
;
create table gap (v%eval(&nout+1) char(1) label='a0'x);
quit;
data &out;
merge &out gap &data(rename=(&names)); %* rare use of merge without by;
label &labels;
run;
%mend;
options mprint;
proc delete data=foo;
run;
%RHS_append (out=foo, data=sashelp.class);
%RHS_append (out=foo, data=sashelp.gas);
%RHS_append (out=foo, data=sashelp.cars);
ods excel file='%temp%\3-tables-in-first-tab-sample.xlsx';
proc print label data=foo;
run;
ods excel close;

export datasets into multiple sheets of one excel file in sas

I'm use this code
proc export data=goldsheet_invalid outfile="C:\Documents and Settings\sasadm\Desktop\gold.xls" dbms=xls replace;
sheet="gold";
run;
proc export data=platinumsheet_invalid outfile="C:\Documents and Settings\sasadm\Desktop\gold.xls" dbms=xls replace;
sheet="platinum";
run;
proc export data=titaniumsheet_invalid outfile="C:\Documents and Settings\sasadm\Desktop\gold.xls" dbms=xls replace;
sheet="titanium";
run;
Error:Statement is not valid or it is used out of proper order
Note:- already try dbms=xlsx or dbms=EXCELCS but not work
Instead of using a PROC EXPORT this can be accomplished with older versions of SAS using ODS (Output Delivery System) statements. Going this route is not as clean as the PROC EXPORT but if all you want is to get the data from these data sets to a single Excel workbook and have the results of each proc statement on a different worksheet this will do it.
In this case the code to accomplish what you are looking for would be:
ods tagsets.excelxp file='C:\temp\gold.xml' options(sheet_name = 'Gold' sheet_interval='proc');
proc print data=goldsheet_invalid;
run;
ods tagsets.excelxp options(sheet_name = 'Platinum');
proc print data=platinumsheet_invalid;
run;
ods tagsets.excelxp options(sheet_name = 'Titanium');
proc print data=titaniumsheet_invalid;
run;
ods tagsets.excelxp close;
You will notice that the file extension created is XML, this is a necessity. When you load the file in Excel is would appear as expected and feel free to update the file extension from there.
More details about SAS and ODS can be found at: https://support.sas.com/rnd/base/ods/odsmarkup/TipSheet_ods_xl_xp.pdf

SAS-Writing Multiple Tables to one XLSX Workbook w/ 2 tables per sheet

I am new to SAS and am having some issues exporting data. I have written a macro to generate some summary tables based on a certain ID. The macro creates two tables for each ID identified in a particular proc sql query. I can write out the last two tables but it overwrites all tables. I was wondering if there is a way to generate one sheet, containing the two summary tables, for each ID identified in my query. Below is the code I have to date for exporting data:
%macro output(x);
ods tagsets.excelxp file="W:\user\test.xls" options(sheet_interval='none');
proc print data=prov_&x;
run;
proc print data=prov_revcd_&x;
run;
ods tagsets.excelxp close;
%mend;
/*Run a loop for each IDcode. Each code will enter the document generation loop*/
%macro loopit(mylist);
%let else=;
%let n = %sysfunc(countw(&mylist)); /*let n=number of codes in the list*/
data
%do I=1 %to &n;
%let val = %scan(&mylist,&I); /*Let val= the ith code in the list*/
%end;
%do j=1 %to &n;
%let val = %scan(&mylist,&j); /*Let val= the jth code in the list*/
/*Run the macro loop to generate the required tables*/
%runtab(&val);
%output&val);
%end;
run;
%mend;
/*Run the macro loop over the list of significant procedure code values*/
%loopit(&varlist);
Any help for correcting this issue would be greatly appreciated! Thanks!
I would rewrite %output like so.
%macro output(x);
ods tagsets.excelxp options(sheet_interval='none' sheet_name="&x");
proc print data=prov_&x;
run;
proc print data=prov_revcd_&x;
run;
%mend;
Then as Reeza suggests put the original ods tagsets.excelxp file= ... and close outside the whole macro.
ods tagsets.excelxp file="c:\temp\test.xlsx";
%loopit(&varlist)
ods tagsets.excelxp close;
If you use PROC EXPORT, that does allow apending to a workbook without this step (and no ODS at al).
%macro output(x);
proc export data=prov_&x outfile="c:\temp\test.xlsx" dbms=excel replace;
sheet="&x._prov";
run;
%mend;
However, this only allows one dataset per sheet - so either you append them together first as a single dataset, or you use 2 sheets in this solution.
Move the ods tagsets.excelxp file= and ods tagsets.excelxp close to outside of the macro otherwise you're recreating the file each time.
You may want to explicitly name the sheets as well.