Uploading Files

Overview of Uploading Files

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 that 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.

Reserved Macro Variables

The reserved SAS macro variables that are associated with uploading files all start with _WEBIN_.
_WEBIN_CONTENT_LENGTH
specifies the length, in bytes, of the file that was uploaded.
_WEBIN_CONTENT_TYPE
specifies the content type that is associated with the file.
_WEBIN_FILE_COUNT
specifies the number of files that were uploaded. If no files were uploaded, the value of this variable is set to zero.
_WEBIN_FILEEXT
specifies the extension of the file that was uploaded.
_WEBIN_FILENAME
specifies the original location of the file.
_WEBIN_FILEREF
specifies the SAS FILEREF that is automatically assigned to the uploaded file. You can use this FILEREF to access the file. The uploaded file is stored in a temporary location on the Application Server, and is deleted when the request is completed. Be sure to copy the file to a permanent location if you need to access it at a later date.
_WEBIN_NAME
specifies the value that is specified in the NAME attribute of the INPUT tag. In the example above, the value would be myfile.
_WEBIN_SASNAME
specifies a unique name for the uploaded SAS table, view, or catalog. 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_ORI.
_WEBIN_SASNAME_ORI
specifies the original name of the uploaded SAS table, view, or catalog. If a SAS table named 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.
_WEBIN_SASTYPE
specifies the type of SAS file that was uploaded: DATA for SAS tables, VIEW for SAS views, and CATALOG for SAS catalogs. A value is set for this macro variable only if a SAS table, view, or catalog was uploaded. The name of the uploaded file is stored in the _WEBIN_SASNAME macro variable.
If you are uploading more than one file, unique macro variables are 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.

Examples of How to Upload Files

Example 1: Uploading a Single File

The following figure shows an HTML page that can be used to upload a single file to the Application Server:
Sample HTML page for uploading a single file
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 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.

Example 2: Uploading Multiple Files

The following figure shows an HTML page that can be used to upload multiple files to the Application Server:
Sample HTML page for uploading multiple files
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.

Examples of How to Use Uploaded Files

Example 3: Uploading a CSV File to a SAS Table

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

Example 4: Uploading an Excel XML Workbook to Multiple SAS Tables

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 is imported to a SAS table with the same name, and the column headings in the worksheets are 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 at support.sas.com/rnd/papers. There are links available for you to download both the macro and the XMLMap.

Example 5: Uploading a SAS Table or View

When a SAS data type (table, view, or catalog) has been uploaded, additional reserved macro variables are created. For example, the following macro variables are 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;

Example 6: Uploading a SAS Catalog

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;

Example 7: Uploading a SAS Table, View, or Catalog and Saving a Permanent Copy

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.

Example 8: Uploading an Excel Workbook to a SAS Table

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.