Hello to all, my client asks to create a page that can be called up from visual analytics to give the user the possibility to upload excel sheets. At the moment I have created a small job that allows you to select the file:
code:
data _null_;
file _webout;
put "<html>";
put "<body>";
put'<form method="post" enctype="multipart/form-data">';
put"<div>";
put '<label for="file">Choose file to upload</label>';
put'<input type="file" id="file" name="file" multiple>';
put"</div>";
put" <div>";
put"<button>Submit</button>";
put" </div>";
put"</form>";
put"</body>";
put "</html>";
the problem I have is that I don't know how to save the file in a path of my server to be able to process it with SAS. Can anyone give me directions?
Thanks
When files are uploaded to SAS through either a Viya Job or STP, they are saved to a temporary storage location. You can access it using the macro variable _WEBIN_FILEREF. Since they can upload multiple files, multiple macro variables will be available, and the number of files uploaded is stored in _WEBIN_FILE_COUNT. For example, if they are uploading two files:
_WEBIN_FILE_COUNT = 2 _WEBIN_FILEREF1 = #LN0001 _WEBIN_FILEREF2 = #LN0002
However, if _WEBIN_FILE_COUNT = 1, you will only use _WEBIN_FILEREF:
_WEBIN_FILE_COUNT = 1 _WEBIN_FILEREF = #LN0001
If you want to only process these files, count the number of files using _WEBIN_FILE_COUNT, then run your import code just like any other SAS code:
/* Test values */ %let _WEBIN_FILE_COUNT = 1; %let _WEBIN_FILEREF = a; %let _WEBIN_FILEREF1 = b; %let _WEBIN_FILEREF2 = c; /* copy: Optional. if YES, copies the file to "outdir" outdir: Location to copy the uploaded file permanently */ %macro read_uploaded_files(copy=NO, outdir=/directory/here); %local i n; %let outdir = %superq(outdir); %let copy = %upcase(©); %put Total files: &_WEBIN_FILE_COUNT; /* Read in all the uploaded files and output sequentially to datasets
named excelout1, excelout2, etc.
*/ %do i = 1 %to &_WEBIN_FILE_COUNT; /* Add a suffix if more than one file was uploaded */ %if(&_WEBIN_FILE_COUNT > 1) %then %let n = &i; %put Importing fileref %nrbquote(&)_WEBIN_FILEREF&n.: &&_WEBIN_FILEREF&n.; /* Read the file in to SAS. Comment out for testing. */ proc import file = &&_WEBIN_FILEREF&n. out = excelout&i. dbms = xlsx replace; run; /* Copy the file somewhere to the server permanently */ %if(©. = YES) %then %do; filename outfile "&outdir./&&_WEBIN_FILENAME&n"; %let rc = %sysfunc(fcopy(&&_WEBIN_FILEREF&n., outfile) ); %end; %end; %mend; %read_uploaded_files;
When files are uploaded to SAS through either a Viya Job or STP, they are saved to a temporary storage location. You can access it using the macro variable _WEBIN_FILEREF. Since they can upload multiple files, multiple macro variables will be available, and the number of files uploaded is stored in _WEBIN_FILE_COUNT. For example, if they are uploading two files:
_WEBIN_FILE_COUNT = 2 _WEBIN_FILEREF1 = #LN0001 _WEBIN_FILEREF2 = #LN0002
However, if _WEBIN_FILE_COUNT = 1, you will only use _WEBIN_FILEREF:
_WEBIN_FILE_COUNT = 1 _WEBIN_FILEREF = #LN0001
If you want to only process these files, count the number of files using _WEBIN_FILE_COUNT, then run your import code just like any other SAS code:
/* Test values */ %let _WEBIN_FILE_COUNT = 1; %let _WEBIN_FILEREF = a; %let _WEBIN_FILEREF1 = b; %let _WEBIN_FILEREF2 = c; /* copy: Optional. if YES, copies the file to "outdir" outdir: Location to copy the uploaded file permanently */ %macro read_uploaded_files(copy=NO, outdir=/directory/here); %local i n; %let outdir = %superq(outdir); %let copy = %upcase(©); %put Total files: &_WEBIN_FILE_COUNT; /* Read in all the uploaded files and output sequentially to datasets
named excelout1, excelout2, etc.
*/ %do i = 1 %to &_WEBIN_FILE_COUNT; /* Add a suffix if more than one file was uploaded */ %if(&_WEBIN_FILE_COUNT > 1) %then %let n = &i; %put Importing fileref %nrbquote(&)_WEBIN_FILEREF&n.: &&_WEBIN_FILEREF&n.; /* Read the file in to SAS. Comment out for testing. */ proc import file = &&_WEBIN_FILEREF&n. out = excelout&i. dbms = xlsx replace; run; /* Copy the file somewhere to the server permanently */ %if(©. = YES) %then %do; filename outfile "&outdir./&&_WEBIN_FILENAME&n"; %let rc = %sysfunc(fcopy(&&_WEBIN_FILEREF&n., outfile) ); %end; %end; %mend; %read_uploaded_files;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
See how to use one filter for multiple data sources by mapping your data from SAS’ Alexandria McCall.
Find more tutorials on the SAS Users YouTube channel.