Hello,
We have several STP programs that uses the following output in Excel:
%if &Valg. = EXCEL %then %do;
data _null_;
/* Set HTTP headers */
rc = stpsrv_header('Content-type','application/vnd.ms-excel');
/* Prompt to SAVE or OPEN the attachment file named test.xls using Excel */
rc = stpsrv_header('Content-disposition','attachment; filename=filename.xls');
run;
%End;
What do I have to do to get a prompt to SAVE ot OPEN for XLSX?
Regards
Morten Fredrik Amundsen
Hi
Have a look here https://communities.sas.com/t5/SAS-Stored-Processes/ODS-EXCEL-in-Web-StoredProcess/td-p/286083
Basicaly you will use the follwoing code:
data _null_;
rc = stpsrv_header('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
rc = stpsrv_header('Content-disposition','attachment; filename=try3.xlsx');
run;
ods excel file = _webout style=Plateau;
proc print data=sashelp.cars;
run;
ods excel close;
make sure you do not have the %stpbegin and %stpend somewhere in your code.
Bruno
Hi,
Yes, of course there is more. 🙂 I'm sorry that my information was'nt acurate enough.
I am using ODSDEST override, and %STPBEGIN and %STPEND. I invoke the STP using the Delivery Portal.
Regards
Morten Fredrik Amundsen
And for the ODSDEST override, your destination is??? MSOFFICE2K? TAGSETS.EXCELXP? CSV?
I don't understand the rest of your question: "What do I have to do to get a prompt to SAVE ot OPEN for XLSX?" When I run a stored process that creates Excel output using TAGSETS.EXCELXP for example after I supply the prompts, the SP runs and I get prompted by the browser to either Open or Save the file. SAS doesn't control that popup window, it comes from the browser. I have some examples of code we used in the Stored Process class -- but they used TAGSETS.EXCELXP because that version of the class was before ODS EXCEL was available.
cynthia
My suggestion is that you try each of these SPs separately, as simple stored processes, outside of your fancy macro code. These only do a PROC PRINT with SASHELP.CLASS, no LIBNAME needed. You should be able to simply register these and then run them to see which one works best. (I like method #1 with _webout because if I have suboptions to pass in I think it is easier to use this method then %stpbegin.)
** Test 1: TAGSETS.EXCELXP with _Webout;
data _null_;
rc = stpsrv_header('Content-type','application/vnd.ms-excel;');
rc = stpsrv_header('Content-disposition','attachment; filename=try1.xls');
run;
ods listing close;
ods tagsets.excelxp body=_webout style=sasweb;
proc print data=sashelp.class noobs;
title 'test1 excelxp mimetype=application/vnd.ms-excel';
run;
ods tagsets.excelxp close;
** Test 2: TAGSETS.EXCELXP with %stpbegin;
%global _odsdest _odsstyle _odsstylesheet;
%let _odsdest=tagsets.excelxp;
%let _odsstyle=sasweb;
%let _odsstylesheet=;
data _null_;
rc = stpsrv_header('Content-type','application/vnd.ms-excel;');
rc = stpsrv_header('Content-disposition','attachment; filename=try2.xls');
run;
*ProcessBody;
%stpbegin;
proc print data=sashelp.class noobs;
title 'test2 excelxp mimetype=application/vnd.ms-excel';
run;
%stpend;
** Test 3: ODS EXCEL (if you have 9.4) with %stpbegin;
%global _odsdest _odsstyle _odsstylesheet;
%let _odsdest=excel;
%let _odsstyle=sasweb;
%let _odsstylesheet=;
data _null_;
rc = stpsrv_header('Content-type','application/vnd.ms-excel;');
rc = stpsrv_header('Content-disposition','attachment; filename=try3.xlsx');
run;
*ProcessBody;
%stpbegin;
proc print data=sashelp.class noobs;
title 'test3 ods excel mimetype=application/vnd.ms-excel';
run;
%stpend;
Hello,
When I am running your example Try3, I get prompted by the browser to eiter open or save the excel file. I choose save. When I then try to open it, I get an error message that says that either the file format or filetype is Invalid. Why do I get this message?
Regards
Morten Fredrik
Hello again,
I have tryed all three examples that you did post me. Both Example1 and Example2 where successful, but Example3, in which the excel file Try3 was created, was unsuccessful.
The excel file Try3 is created, but it is impossible to open. As I mentioned earlier, I get an error message which tells me that either the file format or file type is invalid. How come?
Regards Morten Fredrik
Hi
Have a look here https://communities.sas.com/t5/SAS-Stored-Processes/ODS-EXCEL-in-Web-StoredProcess/td-p/286083
Basicaly you will use the follwoing code:
data _null_;
rc = stpsrv_header('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
rc = stpsrv_header('Content-disposition','attachment; filename=try3.xlsx');
run;
ods excel file = _webout style=Plateau;
proc print data=sashelp.cars;
run;
ods excel close;
make sure you do not have the %stpbegin and %stpend somewhere in your code.
Bruno
Hi,
Thanks for all help.
Regards
Morten Fredrik
I'm trying to do something similar in Viya, but the stpsrv_header function doesn't exist there. This code will produce an xlsx file in my directory, but there is no mechanism for download, as far as I can tell:
%let _ODSDEST = Excel;
%let _ODSSTYLE = HTMLBlue;
data _null_;
%let Report_Date = %sysfunc(date(),yymmddn8.)_%sysfunc(compress(%sysfunc(time(),time8.),%str(:)));
%let Outfile = &Report_Date._REPORT.xlsx;
%let _CONTTYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
%let _CONTENTDISP = "attachment; filename=&Outfile";
run;
ods excel file = "/home/&sysuserid/&Outfile"
options(sheet_name='Class' tab_color='purple' frozen_headers='yes');
proc print data = sashelp.class;
run;
ods excel options(sheet_name='Cars');
proc print data = sashelp.cars;
run;
...waiting message...
how to post a waiting message in STP?
Displaying the "Please Wait" message for a long-running stored process
hi Morten,
my SAS code is similar...is:
ods excel file=_webout style=plateau;
data _null_;
rc = stpsrv_header('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
rc = stpsrv_header("Content-disposition","attachment; filename=&_name_report._&_dt_report_ymd..XLS");
run;
proc print data =&_flnm. noobs label ;run;
ods excel close;
hi chintia,
the following solution it's wrong! because print the xls on the webpage...
data _null_; file _webout; put '<html>'; put '<body>'; put '<div id="pleasewait">'; put '<h2>Please Wait. Your request is being processed.</h2>'; put '</div>'; do i = 1 to 10; put " "; end; run;
data _null_;
rc = stpsrv_header('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
rc = stpsrv_header('Content-disposition','attachment; filename=try3.xlsx');
run;
ods excel file = _webout style=Plateau;
proc print data=sashelp.cars;
run;
ods excel close;
this is the result:
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.