Loading files to SAS with CURL

A useful feature of SAS Stored Processes is the ability to load multiple files in a single request. This could be a bunch of CSVs for processing, or even a .sas program that you want to %include along with some associated data (deliberate SAS injection). This is particularly useful when you are running a build process, eg for a web application, and you need to send across some SAS programs that should be compiled into Stored Processes and re-downloaded as a distributable SPK. If you send your executable .sas program as the first file, to a ‘master’ STP that has just one line of code (%inc &_webin_fileref1/source2;), then this becomes the only entry point you need when running from a non-SAS environment!

Anyway, here is the sample code:

curl -v -L -b cookiefile -c cookiefile \
    -H "Content-Type: multipart/form-data" \
    -F "myprogram=@./runme.sas" \
    -F "somefile1=@./processme.csv" \
    -F "somefile2=@./processmeaswell.csv" \
    "$SERVER?_program=$STP&_username=$USER&_password=$PASS"
    --output "/tmp/out.txt"

Things to note:

curl options

  • -v = verbose
  • -L = follow the redirects
  • -b / -c = cookie in / out
  • -H = http header
  • -F = Files for upload
  • --output = output filename

Substitutions

  • $SERVER = your SAS server, eg: https://yourserver.com/SASStoredProcess/do
  • $STP = full metadata path to STP, eg:  /my/meta/path/to/my/service
  • $USER = sas username, including @saspw suffix if an internal account
  • $PASS = sas password
SAS Considerations

The following automatically assigned macro variables will be helpful when accessing the files you have uploaded:

  • _WEBIN_FILE_COUNT - contains a count of the number of files that were uploaded
  • _WEBIN_FILEREF - a unique fileref that can be used to access the file
  • _WEBIN_FILENAME - the location in which the file is saved in SAS (eg in WORK)
  • _WEBIN_NAME - the name given for the file on upload (eg somefile1 / somefile2 in the example)
The above variables are actually macro variable arrays - so _WEBIN_FILENAME1 will be the first,  _WEBIN_FILENAME2 will be the second, and so on.

Further resources

If you want more background on building web applications with SAS, check out the following resources!
  • MacroCore - library for SAS Application Development
  • SASjs - Adapter for SAS-JS communication
  • sasjs-cli - utility for building SAS Apps
  • https://sasjs.io - guide to building web apps on SAS
Data Controller is also a super useful tool for SAS data capture and data quality, as well as EUC / Power BI integration and is available as a free 30 day demo version.

Happy coding!