Uploading from SAS to Dropbox

Last updated: 2022-08-26 18:18

Go to the following URL (ensure you are logged in and your name is displayed in the top right corner): https://www.dropbox.com/developers/apps/create

  • Select “Scoped Access” for Step 1
  • Select “Full Dropbox” for Step 2
  • Name your API at Step 3 in line with the branding guidelines that can be found here: https://www.dropbox.com/developers/reference/branding-guide
  • Click “Create App”
  • Go to the “Permissions” tab for your API and ensure the following boxes are ticked: files.content.write, files.content.read, files.metadata.read
  • Then click "Submit" and save the changes
Image
  • Go to the “Settings” tab, scroll down and click “Generate" under "Generated Access Token"
  • This MUST be done every time you change the settings
Image
  • In SAS, convert your SAS dataset to an xlsx file using PROC EXPORT
  • The OUTFILE statement ensures it is stored in the WORK folder
PROC EXPORT 
  DATA=lib.sas_data_to_upload
  DBMS=xlsx 
  OUTFILE="%sysfunc(pathname(work))\upload to dropbox.xlsx";
RUN;
  • In the “FILENAME input” statement, repeat the same path as in the previous step
  • Replace the ***** after “Bearer” with your access token
  • Replace “Dropbox_directory/upload.xlsx” with the filepath where you want the Excel to be uploaded
FILENAME input "%sysfunc(pathname(work))\upload_to_dropbox.xlsx";
FILENAME resp TEMP;
FILENAME headout TEMP;

PROC HTTP 
  URL="https://content.dropboxapi.com/2/files/upload" 
  METHOD="post" 
  IN=input 
  OUT=resp 
  HEADEROUT=headout
;
  HEADERS 
    "Content-Type"="application/octet-stream"
    "Authorization" = "Bearer ***** " 
    "Dropbox-API-Arg" = '{"path": "/Dropbox_directory/upload.xlsx", "mode": "overwrite", 
                        "autorename": false, "mute": false, "strict_conflict": false}'
;
RUN;

DATA _null_;
  INFILE resp;
  INPUT;
  PUT _infile_;
RUN;

DATA _null_;
  INFILE headout;
  INPUT;
  PUT _infile_;
RUN;

FILENAME resp CLEAR;
FILENAME headout CLEAR;
FILENAME input CLEAR;