Previous Page | Next Page

Building a Web Application with SAS Stored Processes

Uploading Files


Overview of Uploading Files

You can use the SAS Stored Process Web Application to upload one or more files to your SAS Stored Process Server. The upload process is initiated by a custom input form 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. For more information, see Specifying Custom Input Forms. After the form data is submitted, the file you chose and any other name/value pairs that are contained in the custom input form are sent to the stored process server. Your stored process can then use both the name/value pairs and the file that was uploaded.

Note:   You cannot upload files to SAS Workspace Servers.  [cautionend]


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, then the value of this variable will be 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 stored process server, and will be 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.

_WEBIN_SASNAME

specifies a unique name for the SAS table, view, or catalog that was uploaded. 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 SAS table, view, or catalog that was uploaded. If a SAS table named mydata.sas7bdat was uploaded, then _WEBIN_SASNAME_ORI contains the value mydata. A value is set for this macro variable only if a SAS table, view, or catalog that 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.

_WEBIN_STREAM

specifies the name of the data source that was used to upload the file.

_WEBIN_STREAM_COUNT

specifies the number of files that were uploaded. If no files were uploaded, then the value of this variable will be set to zero.

If you are uploading more than one file, then unique macro variables will be created for each file. This applies to all of the previous reserved macro variables except _WEBIN_FILE_COUNT and _WEBIN_STREAM_COUNT.

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.  [cautionend]


Examples of How to Upload Files


Example 1: Uploading a Single File

The following figure shows a custom input form that can be used to upload a single file to the stored process server:

[Upload form for a single file]

Here is an HTML example for uploading a single file:

<form action="StoredProcessWebApplicationURL" method="post" enctype="multipart
/form-data"> <input type="hidden" name="_program" value="/Path/StoredProcessName"> <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 preceding HTML example, you must replace "StoredProcessWebApplicationURL" with the path to the SAS Stored Process Web Application. This path is usually http://YourServer:8080/SASStoredProcess/do , where YourServer corresponds to the domain name of your stored process server. Similarly, you need to specify the path and name of the stored process 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 preceding HTML example is used to create the Browse button and text entry field in the preceding 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 SAS Stored Process Web Application, which forwards the data to the stored process server. As a result, the following SAS macro variables are created:

SAS Macro Variables
Variable Name Value Description
_WEBIN_CONTENT_LENGTH 1465 Specifies the size of the file that was uploaded in bytes (supplied automatically by the Web browser).
_WEBIN_CONTENT_TYPE text/plain Specifies the content type that corresponds to the file that was uploaded (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 stored process has access to the uploaded file through 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(stpsrv_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 preceding code example shows how to use the _WEBIN_CONTENT_TYPE macro variable to set the content-type header. This 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 a custom input form that can be used to upload multiple files to the stored process server:

[Upload form for multiple files]

Here is an HTML example for uploading multiple files:

<form action="StoredProcessWebApplicationURL" method="post" enctype="multipart/form-data">
<input type="hidden" name="_program" value="/Path/StoredProcessName">
<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>

Example 2 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 SAS Stored Process Web Application, which forwards the data to the stored process server. As a result, the following SAS macro variables are created:

SAS Macro Variables
Variable Name Value Description
_WEBIN_CONTENT_LENGTH 1465 Specifies the size of the first file that was uploaded 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 file that was uploaded in bytes (supplied automatically by the Web browser).
_WEBIN_CONTENT_LENGTH2 5367 Specifies the size of the second file that was uploaded in bytes (supplied automatically by the Web browser).
_WEBIN_CONTENT_TYPE text/plain Specifies the content type that corresponds to the first file that was uploaded (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 file that was uploaded (supplied automatically by the Web browser).
_WEBIN_CONTENT_TYPE2 image/jpeg Specifies the content type that corresponds to the second file that was uploaded (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 file that was uploaded.
_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 file that was uploaded.
_WEBIN_FILEREF2 #LN00016 Specifies the SAS fileref that you can use to access the second file that was uploaded.
_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 comma-separated values (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.';

   %STPBEGIN;
      proc print data=work.mydata(obs=10); run; quit;
   %STPEND;

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. For more information, see the IMPORT procedure in the Base SAS Procedures Guide.

An alternative method is to write a DATA step to import the CSV file. This method requires 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 an XMLMap. Each worksheet in the workbook will be imported to a SAS table with the same name. 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 an XMLMap. For more information, see the paper "Creating AND Importing Multi-Sheet Excel Workbooks the Easy Way with SAS" at http://support.sas.com/rnd/papers. Links are 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 will be created if the file C:\temp\djia.sas7bdat has been uploaded:

SAS Macro Variables
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.';

  %STPBEGIN;
      proc print data=&_WEBIN_SASNAME(obs=10); run; quit;
   %STPEND;


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:

   %STPBEGIN;
      proc catalog c=&_WEBIN_SASNAME;
         contents;
      run; quit;
   %STPEND;


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 a SAS table, view, or catalog that has been uploaded 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 preceding example 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 Excel workbook file that has been uploaded 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.';

   %STPBEGIN;
      proc print data=work.mydata(obs=10); run; quit;
   %STPEND;

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. For more information, see the IMPORT procedure in the Base SAS Procedures Guide.

Previous Page | Next Page | Top of Page