diff --git a/tests/viya/mv_createfile.test.sas b/tests/viya/mv_createfile.test.sas new file mode 100644 index 0000000..52ea2fc --- /dev/null +++ b/tests/viya/mv_createfile.test.sas @@ -0,0 +1,42 @@ +/** + @file + @brief Testing mv_createfile macro + +

SAS Macros

+ @li mf_uid.sas + @li mfv_existfile.sas + @li mp_assert.sas + @li mv_createfile.sas + + +**/ + +options mprint; + +%let file=%mf_uid(); + +%put TEST 1 - basic file upload ; +filename somefile temp; +data _null_; + file somefile; + put 'hello testings'; +run; +%mv_createfile(path=&mcTestAppLoc/temp, name=&file..txt,inref=somefile) + +%mp_assert( + iftrue=(%mfv_existfile(&mcTestAppLoc/temp/&file..txt)=1), + desc=Check if created file exists +) + +%put TEST 2 - dataset upload ; +data temp; +x=1; +run; +filename ds "%sysfunc(pathname(work))/temp.sas7bdat"; + +%mv_createfile(path=&mcTestAppLoc/temp, name=&file..sas7bdat,inref=ds) + +%mp_assert( + iftrue=(%mfv_existfile(&mcTestAppLoc/temp/&file..sas7bdat)=1), + desc=Check if created dataset exists +) \ No newline at end of file diff --git a/viya/mv_createfile.sas b/viya/mv_createfile.sas new file mode 100644 index 0000000..7382baf --- /dev/null +++ b/viya/mv_createfile.sas @@ -0,0 +1,97 @@ +/** + @file + @brief Creates a file in SAS Drive + @details Creates a file in SAS Drive and adds the appropriate content type. + If the parent folder does not exist, it is created. + + Usage: + + filename myfile temp; + data _null_; + file myfile; + put 'something'; + run; + %mv_createfile(path=/Public/temp,name=newfile.txt,inref=myfile) + + + @param [in] path= The parent folder in which to create the file + @param [in] name= The name of the file to be created + @param [in] inref= The fileref pointing to the file to be uploaded + @param [in] access_token_var= The global macro variable to contain the access + token, if using authorization_code grant type. + @param [in] grant_type= (sas_services) Valid values are: + @li password + @li authorization_code + @li sas_services + + @param [in] mdebug= (0) Set to 1 to enable DEBUG messages + + @version VIYA V.03.05 + @author Allan Bowe, source: https://github.com/sasjs/core + +

SAS Macros

+ @li mf_getuniquefileref.sas + @li mf_isblank.sas + @li mp_abort.sas + @li mp_binarycopy.sas + @li mv_createfolder.sas + +**/ + +%macro mv_createfile(path= + ,name= + ,inref= + ,access_token_var=ACCESS_TOKEN + ,grant_type=sas_services + ,mdebug=0 + ); +%local dbg; +%if &mdebug=1 %then %do; + %put &sysmacroname entry vars:; + %put _local_; +%end; +%else %let dbg=*; + +%local oauth_bearer; +%if &grant_type=detect %then %do; + %if %symexist(&access_token_var) %then %let grant_type=authorization_code; + %else %let grant_type=sas_services; +%end; +%if &grant_type=sas_services %then %do; + %let oauth_bearer=oauth_bearer=sas_services; + %let &access_token_var=; +%end; + +%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password + and &grant_type ne sas_services + ) + ,mac=&sysmacroname + ,msg=%str(Invalid value for grant_type: &grant_type) +) + +%mp_abort(iftrue=(%mf_isblank(&path)=1 or %length(&path)=1) + ,mac=&sysmacroname + ,msg=%str(path value must be provided) +) +%mp_abort(iftrue=(%mf_isblank(&name)=1 or %length(&name)=1) + ,mac=&sysmacroname + ,msg=%str(name value with length >1 must be provided) +) + +/* create folder if it does not already exist */ +%mv_createfolder(path=&path + ,access_token_var=&access_token_var + ,grant_type=&grant_type + ,mdebug=&mdebug +) + +/* create file with relevant options */ +%local fref; +%let fref=%mf_getuniquefileref(); +filename &fref filesrvc folderPath="&path" filename="&name"; + +%mp_binarycopy(inref=&inref, outref=&fref) + +filename &fref clear; + +%mend mv_createfile; \ No newline at end of file