mv_createfile.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Creates a file in SAS Drive
4  @details Creates a file in SAS Drive and adds the appropriate content type.
5  If the parent folder does not exist, it is created.
6 
7  Usage:
8 
9  filename myfile temp;
10  data _null_;
11  file myfile;
12  put 'something';
13  run;
14  %mv_createfile(path=/Public/temp,name=newfile.txt,inref=myfile)
15 
16 
17  @param [in] path= The parent folder in which to create the file
18  @param [in] name= The name of the file to be created
19  @param [in] inref= The fileref pointing to the file to be uploaded
20  @param [in] intype= (BINARY) The type of the input data. Valid values:
21  @li BINARY File is copied byte for byte using the mp_binarycopy.sas macro.
22  @li BASE64 File will be first decoded using the mp_base64.sas macro, then
23  loaded byte by byte to SAS Drive.
24  @param [in] contentdisp= (inline) Content Disposition. Example values:
25  @li inline
26  @li attachment
27  @param [in] ctype= (0) Set a default HTTP Content-Type header to be returned
28  with the file when the content is retrieved from the Files service.
29  @param [in] access_token_var= The global macro variable to contain the access
30  token, if using authorization_code grant type.
31  @param [in] grant_type= (sas_services) Valid values are:
32  @li password
33  @li authorization_code
34  @li sas_services
35 
36  @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
37 
38  @version VIYA V.03.05
39  @author Allan Bowe, source: https://github.com/sasjs/core
40 
41  <h4> SAS Macros </h4>
42  @li mf_getuniquefileref.sas
43  @li mf_isblank.sas
44  @li mp_abort.sas
45  @li mp_base64copy.sas
46  @li mp_binarycopy.sas
47  @li mv_createfolder.sas
48 
49 **/
50 
51 %macro mv_createfile(path=
52  ,name=
53  ,inref=
54  ,intype=BINARY
55  ,contentdisp=inline
56  ,ctype=0
57  ,access_token_var=ACCESS_TOKEN
58  ,grant_type=sas_services
59  ,mdebug=0
60  );
61 %local dbg;
62 %if &mdebug=1 %then %do;
63  %put &sysmacroname entry vars:;
64  %put _local_;
65 %end;
66 %else %let dbg=*;
67 
68 %local oauth_bearer;
69 %if &grant_type=detect %then %do;
70  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
71  %else %let grant_type=sas_services;
72 %end;
73 %if &grant_type=sas_services %then %do;
74  %let oauth_bearer=oauth_bearer=sas_services;
75  %let &access_token_var=;
76 %end;
77 
78 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
79  and &grant_type ne sas_services
80  )
81  ,mac=&sysmacroname
82  ,msg=%str(Invalid value for grant_type: &grant_type)
83 )
84 
85 %mp_abort(iftrue=(%mf_isblank(&path)=1 or %length(&path)=1)
86  ,mac=&sysmacroname
87  ,msg=%str(path value must be provided)
88 )
89 %mp_abort(iftrue=(%mf_isblank(&name)=1 or %length(&name)=1)
90  ,mac=&sysmacroname
91  ,msg=%str(name value with length >1 must be provided)
92 )
93 
94 /* create folder if it does not already exist */
95 %mv_createfolder(path=&path
96  ,access_token_var=&access_token_var
97  ,grant_type=&grant_type
98  ,mdebug=&mdebug
99 )
100 
101 /* create file with relevant options */
102 %local fref;
103 %let fref=%mf_getuniquefileref();
104 filename &fref filesrvc
105  folderPath="&path"
106  filename="&name"
107  cdisp="&contentdisp"
108 %if "&ctype" ne "0" %then %do;
109  ctype="&ctype"
110 %end;
111  lrecl=1048544;
112 %if &intype=BINARY %then %do;
113  %mp_binarycopy(inref=&inref, outref=&fref)
114 %end;
115 %else %if &intype=BASE64 %then %do;
116  %mp_base64copy(inref=&inref, outref=&fref, action=DECODE)
117 %end;
118 
119 filename &fref clear;
120 
121 %local base_uri; /* location of rest apis */
122 %let base_uri=%mf_getplatform(VIYARESTAPI);
123 
124 %put &sysmacroname: File &name successfully created in &path;
125 %put &sysmacroname:;%put;
126 %put &base_uri/SASJobExecution?_file=&path/&name;%put;
127 %put &sysmacroname:;
128 
129 %mend mv_createfile;