SAS/IntrNet 9.1: Application Dispatcher |
Starting with SAS 9.1.3 Service Pack 4, you can use Application Dispatcher to upload one or more files to your Application Server. The upload process is usually initiated by an HTML page that contains an INPUT tag with the attribute TYPE set to "file"
:
<input type="file" name="myfile">
This tag enables you to specify the file that you want to upload. After the form data is submitted, the file you chose and any other name/value pairs that are contained in the HTML form are sent to the Application Server. Your SAS program can then use both the name/value pairs and the file that was uploaded.
The reserved SAS macro variables that are associated with uploading files all start with _WEBIN_
.
myfile
.mydata.sas7bdat
was uploaded, then _WEBIN_SASNAME_ORI would contain the value mydata
. A value is set for this macro variable only if a SAS table, view, or catalog was uploaded. All SAS data types are stored in the Work library. The type of SAS file that was uploaded is stored in the _WEBIN_SASTYPE macro variable. See also _WEBIN_SASNAME.If you are uploading more than one file, unique macro variables will be created for each file. This applies to all of the previous reserved macro variables except _WEBIN_FILE_COUNT. See Multiple Value Pairs for more information.
Note: For z/OS, the SAS server must be invoked with the FILESYSTEM=HFS option in order to be able to upload SAS file types.
The following figure shows an HTML page that can be used to upload a single file to the Application Server:
The HTML for performing the upload might look like this:
<form action="<BrokerURL>" method="post" enctype="multipart/form-data"> <input type="hidden" name="_service" value="<ServiceName>"> <input type="hidden" name="_program" value="<ProgramName>"> <table border="0" cellpadding="5"> <tr> <th>Choose a file to upload:</th> <td><input type="file" name="myfile"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="OK"></td> </tr> </table> </form>
In the previous lines of HTML, you must replace "<BrokerURL>" with the path to the SAS/IntrNet Application Broker. For example, on Windows, this path is usually http://YourServer/scripts/broker.exe
, where YourServer corresponds to the domain name of your Web server. Similarly, you will need to specify the service name and the program that you want to execute after the file has been uploaded. You should specify the exact values that are shown for the METHOD and ENCTYPE attributes of the FORM tag.
The INPUT tag in the previous lines of HTML is used to create the Browse button and text entry field in the previous figure. The appearance of this control might be different depending on which Web browser you use, but the functionality should be the same. Clicking the Browse button enables you to navigate to the file that you want to upload. You can choose any file that you have access to. This example uses the file readme.txt
, which resides in the Windows directory C:\temp
.
After you select a file and click OK, all form data is sent to the Application Broker, which in turn, forwards the data to the Application Server. As a result, the following SAS macro variables are created:
Variable Name | Value | Description |
---|---|---|
_WEBIN_CONTENT_LENGTH | 1465 | Specifies the size of the uploaded file in bytes (supplied automatically by the Web browser). |
_WEBIN_CONTENT_TYPE | text/plain | Specifies the content type that corresponds to the uploaded file (supplied automatically by the Web browser). |
_WEBIN_FILE_COUNT | 1 | Specifies the number of files that were uploaded. |
_WEBIN_FILEEXT | txt | Specifies the extension of the file that was uploaded. |
_WEBIN_FILENAME | C:\temp\README.txt | Specifies the name and original location of the file that was uploaded. |
_WEBIN_FILEREF | #LN00197 | Specifies the SAS FILEREF that you can use to access the uploaded file. This FILEREF is assigned for you by the SAS server. |
_WEBIN_NAME | myfile | Specifies the value that corresponds to the NAME attribute of the INPUT tag. |
Your SAS/IntrNet program has access to the uploaded file via the FILEREF that is stored in the value of the _WEBIN_FILEREF macro variable. The following code example returns the uploaded file to the client:
* Set the Content-type header; %let RV = %sysfunc(appsrv_header(Content-type, &_WEBIN_CONTENT_TYPE)); * Write the file back to the Web browser; data _null_; length data $1; infile &_WEBIN_FILEREF recfm=n; file _webout recfm=n; input data $char1. @@; put data $char1. @@; run;
The previous code shows how to use the _WEBIN_CONTENT_TYPE macro variable to set the content-type header. The previous code also shows how to use the _WEBIN_FILEREF macro variable to access the uploaded file.
The following figure shows an HTML page that can be used to upload multiple files to the Application Server:
The HTML for performing the upload might look like this:
<form action="<BrokerURL>" method="post" enctype="multipart/form-data"> <input type="hidden" name="_service" value="<ServiceName>"> <input type="hidden" name="_program" value="<ProgramName>"> <table border="0" cellpadding="5"> <tr> <th>Choose a file to upload:</th> <td><input type="file" name="firstfile"></td> </tr> <tr> <th>Choose another file to upload:</th> <td><input type="file" name="secondfile"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="OK"></td> </tr> </table> </form>
Refer to Example 1 for a basic discussion of the previous lines of HTML. This example uses the files readme.txt
and winter.jpg
, which reside in the Windows directory C:\temp
. Note that the two input files do not need to be in the same directory.
After you select a file and click OK, all form data is sent to the Application Broker, which in turn, forwards the data to the Application Server. As a result, the following SAS macro variables are created:
Variable Name | Value | Description |
---|---|---|
_WEBIN_CONTENT_LENGTH | 1465 | Specifies the size of the first uploaded file in bytes (supplied automatically by the Web browser). |
_WEBIN_CONTENT_LENGTH0 | 2 | Specifies the number of files that were uploaded. |
_WEBIN_CONTENT_LENGTH1 | 1465 | Specifies the size of the first uploaded file in bytes (supplied automatically by the Web browser). |
_WEBIN_CONTENT_LENGTH2 | 5367 | Specifies the size of the second uploaded file in bytes (supplied automatically by the Web browser). |
_WEBIN_CONTENT_TYPE | text/plain | Specifies the content type that corresponds to the first uploaded file (supplied automatically by the Web browser). |
_WEBIN_CONTENT_TYPE0 | 2 | Specifies the number of files that were uploaded. |
_WEBIN_CONTENT_TYPE1 | text/plain | Specifies the content type that corresponds to the first uploaded file (supplied automatically by the Web browser). |
_WEBIN_CONTENT_TYPE2 | image/pjpeg | Specifies the content type that corresponds to the second uploaded file (supplied automatically by the Web browser). |
_WEBIN_FILE_COUNT | 2 | Specifies the number of files that were uploaded. |
_WEBIN_FILEEXT | txt | Specifies the extension of the first file that was uploaded. |
_WEBIN_FILEEXT0 | 2 | Specifies the number of files that were uploaded. |
_WEBIN_FILEEXT1 | txt | Specifies the extension of the first file that was uploaded. |
_WEBIN_FILEEXT2 | jpg | Specifies the extension of the second file that was uploaded. |
_WEBIN_FILENAME | C:\temp\README.txt | Specifies the name and original location of the first file that was uploaded. |
_WEBIN_FILENAME0 | 2 | Specifies the number of files that were uploaded. |
_WEBIN_FILENAME1 | C:\temp\README.txt | Specifies the name and original location of the first file that was uploaded. |
_WEBIN_FILENAME2 | C:\temp\winter.jpg | Specifies the name and original location of the second file that was uploaded. |
_WEBIN_FILEREF | #LN00014 | Specifies the SAS FILEREF that you can use to access the first uploaded file. |
_WEBIN_FILEREF0 | 2 | Specifies the number of files that were uploaded. |
_WEBIN_FILEREF1 | #LN00014 | Specifies the SAS FILEREF that you can use to access the first uploaded file. |
_WEBIN_FILEREF2 | #LN00016 | Specifies the SAS FILEREF that you can use to access the second uploaded file. |
_WEBIN_NAME | firstfile | Specifies the value that corresponds to the NAME attribute of the first INPUT tag. |
_WEBIN_NAME0 | 2 | Specifies the number of files that were uploaded. |
_WEBIN_NAME1 | firstfile | Specifies the value that corresponds to the NAME attribute of the first INPUT tag. |
_WEBIN_NAME2 | secondfile | Specifies the value that corresponds to the NAME attribute of the second INPUT tag. |
After you have uploaded a CSV file, you can use the IMPORT procedure to import the file to a SAS table. The following sample code shows one way of achieving this:
%let CSVFILE=%sysfunc(pathname(&_WEBIN_FILEREF)); proc import datafile="&CSVFILE" out=work.mydata dbms=csv replace; getnames=yes; run; title 'First 10 records of CSV file after importing to a SAS table.'; ods html body=_webout style=Seaside path=&_tmpcat (url=&_replay) rs=none; proc print data=work.mydata(obs=10); run; quit; ods html close;
Because the IMPORT procedure requires a full path to the CSV file, you must first use the PATHNAME function to get the path to the file. The GETNAMES statement uses the data in the first row of the CSV file for the SAS column names. See the IMPORT procedure in the Base SAS Procedures Guide for further details.
An alternative method would be to write a DATA step to import the CSV file. This method would require only Base SAS. The following code is an example of how to do this:
data work.mydata; infile &_WEBIN_FILEREF dlm=',' dsd; * Your code to read the CSV file; run;
Starting with Excel XP (Excel 2002), a workbook can be saved as an XML file. This XML file can be read into SAS using the SAS XML LIBNAME Engine and a SAS XMLMap. Each worksheet in the workbook will be imported to a SAS table with the same name, and the column headings in the worksheets will be used for the column names in the SAS tables. The following code is an example of how to do this. Be sure to include the appropriate directory paths.
%let XMLFILE=%sysfunc(pathname(&_WEBIN_FILEREF)); * Include the XLXP2SAS macro; %include 'loadxl.sas'; * Import the workbook into SAS tables; %XLXP2SAS(excelfile=&XMLFILE, mapfile=excelxp.map);
The %INCLUDE statement makes the XLXP2SAS macro available to SAS. The %XLXP2SAS macro imports the data from all the worksheets into separate SAS tables with the help of a SAS XMLMap. For more details, see the paper Moving Data and Analytical Results between SAS and Microsoft Office. There are links available for you to download both the macro and the XMLMap.
When a SAS data type (table, view, or catalog) has been uploaded, additional reserved macro variables are created. For example, the following macro variables will be created if the file C:\temp\djia.sas7bdat
has been uploaded:
Variable Name | Value | Description |
---|---|---|
_WEBIN_SASNAME | _B3FF5FCAF39482D93793AEEF05BB15F | Specifies a unique name for the uploaded SAS table, which is stored in the Work library. |
_WEBIN_SASNAME_ORI | djia | Specifies the original name of the uploaded SAS table. |
_WEBIN_SASTYPE | DATA | Specifies the type of SAS file that was uploaded: DATA for a SAS table; VIEW for a SAS view. |
To print the SAS table or view that has been uploaded, use the following code:
title 'First 10 records of uploaded SAS data file.'; ods listing close; ods html body=_webout style=Seaside path=&_TMPCAT (url=&_REPLAY) rs=none; proc print data=&_WEBIN_SASNAME(obs=10); run; quit; ods html close;
You can use the following sample code to list the contents of a SAS catalog that has been uploaded:
ods listing close; ods html body=_webout style=Seaside path=&_TMPCAT (url=&_REPLAY) rs=none; proc catalog c=&_WEBIN_SASNAME; contents; run; quit; ods html close;
You can use the following sample code to make a permanent copy of an uploaded SAS table, view, or catalog and to retain the name of the original uploaded file:
proc datasets library=<YourLibrary>; copy in=work out=<YourLibrary> memtype=&_WEBIN_SASTYPE; select &_WEBIN_SASNAME; run; change &_WEBIN_SASNAME=&_WEBIN_SASNAME_ORI; run; quit;
In the previous lines of SAS code, you must replace <YourLibrary> with the name of the SAS library in which you want to store the SAS table, view, or catalog.
You can use the IMPORT procedure to import an uploaded Excel workbook file to a SAS table. The following sample code shows one way of achieving this:
%let XLSFILE=%sysfunc(pathname(&_WEBIN_FILEREF)); proc import datafile="&XLSFILE" out=work.mydata dbms=excel replace ; getnames=yes; run; quit; title 'First 10 records of Excel workbook after importing to a SAS table.'; ods listing close; ods html body=_webout style=Seaside path=&_tmpcat (url=&_replay) rs=none; proc print data=work.mydata(obs=10); run; quit; ods html close;
Because the IMPORT procedure requires a full path to the Excel workbook, you must first use the PATHNAME function to get the path to the file. The GETNAMES statement uses the data in the first row of the workbook for the SAS column names. See the IMPORT procedure in the Base SAS Procedures Guide for further details.
SAS/IntrNet 9.1: Application Dispatcher |