Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mv_createfile.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Creates a file in SAS Drive using the API method
4 @details Creates a file in SAS Drive using the API interface.
5 If the parent folder does not exist, it is created.
6 The API approach is more flexible than using the filesrvc engine of the
7 filename statement, as it provides more options.
8
9 SAS docs: https://developer.sas.com/rest-apis/files/createNewFile
10
11 Usage:
12
13 filename myfile temp;
14 data _null_;
15 file myfile;
16 put 'something';
17 run;
18 %mv_createfile(path=/Public/temp,name=newfile.txt,inref=myfile)
19
20
21 @param [in] path= The parent (SAS Drive) folder in which to create the file
22 @param [in] name= The name of the file to be created
23 @param [in] inref= The fileref pointing to the file to be uploaded
24 @param [in] intype= (BINARY) The type of the input data. Valid values:
25 @li BINARY File is copied byte for byte using the mp_binarycopy.sas macro.
26 @li BASE64 File will be first decoded using the mp_base64.sas macro, then
27 loaded byte by byte to SAS Drive.
28 @param [in] contentdisp= (attchment) Content Disposition. Example values:
29 @li inline
30 @li attachment
31 @param [in] ctype= (0) The actual MIME type of the file (if blank will be
32 determined based on file extension))
33 @param [in] access_token_var= The global macro variable to contain the access
34 token, if using authorization_code grant type.
35 @param [in] grant_type= (sas_services) Valid values are:
36 @li password
37 @li authorization_code
38 @li sas_services
39 @param [out] outds= (_null_) Output dataset with the uri of the new file
40
41 @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
42
43 <h4> SAS Macros </h4>
44 @li mf_getplatform.sas
45 @li mf_getuniquefileref.sas
46 @li mf_getuniquename.sas
47 @li mf_isblank.sas
48 @li mf_mimetype.sas
49 @li mp_abort.sas
50 @li mp_base64copy.sas
51 @li mv_createfolder.sas
52
53 <h4> Related Macros</h4>
54 @li mv_createfile.sas
55
56**/
57
58%macro mv_createfile(path=
59 ,name=
60 ,inref=
61 ,intype=BINARY
62 ,contentdisp=attachment
63 ,ctype=0
64 ,access_token_var=ACCESS_TOKEN
65 ,grant_type=sas_services
66 ,mdebug=0
67 ,outds=_null_
68 );
69%local dbg;
70%if &mdebug=1 %then %do;
71 %put &sysmacroname entry vars:;
72 %put _local_;
73%end;
74%else %let dbg=*;
75
76%local oauth_bearer;
77%if &grant_type=detect %then %do;
78 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
79 %else %let grant_type=sas_services;
80%end;
81%if &grant_type=sas_services %then %do;
82 %let oauth_bearer=oauth_bearer=sas_services;
83 %let &access_token_var=;
84%end;
85
86%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
87 and &grant_type ne sas_services
88 )
89 ,mac=&sysmacroname
90 ,msg=%str(Invalid value for grant_type: &grant_type)
91)
92
93%mp_abort(iftrue=(%mf_isblank(&path)=1 or %length(&path)=1)
94 ,mac=&sysmacroname
95 ,msg=%str(path value must be provided)
96)
97%mp_abort(iftrue=(%mf_isblank(&name)=1 or %length(&name)=1)
98 ,mac=&sysmacroname
99 ,msg=%str(name value with length >1 must be provided)
100)
101
102/* prep the source file */
103%local fref;
104%let fref=%mf_getuniquefileref();
105
106%if %upcase(&intype)=BINARY %then %let fref=&inref;
107%else %if %upcase(&intype)=BASE64 %then %do;
108 %mp_base64copy(inref=&inref, outref=&fref, action=DECODE)
109%end;
110%else %put %str(ERR)OR: invalid value for intype: &intype;
111
112
113%if &mdebug=1 %then %do;
114 data _null_;
115 infile &fref lrecl=32767;
116 input;
117 put _infile_;
118 run;
119%end;
120
121
122/* create folder if it does not already exist */
123%local folderds parenturi;
124%let folderds=%mf_getuniquename(prefix=folderds);
125%mv_createfolder(path=&path
126 ,access_token_var=&access_token_var
127 ,grant_type=&grant_type
128 ,mdebug=&mdebug
129 ,outds=&folderds
130)
131data _null_;
132 set &folderds;
133 call symputx('self_uri',self_uri,'l');
134run;
135
136
137options noquotelenmax;
138%local base_uri; /* location of rest apis */
139%let base_uri=%mf_getplatform(VIYARESTAPI);
140
141%local url mimetype;
142%let url=&base_uri/files/files?parentFolderUri=&self_uri;
143
144/* fetch job info */
145%local fname1;
146%let fname1=%mf_getuniquefileref();
147proc http method='POST' out=&fname1 &oauth_bearer in=&fref
148 %if "&ctype" = "0" %then %do;
149 %let mimetype=%mf_mimetype(%scan(&name,-1,.));
150 ct="&mimetype"
151 %end;
152 %else %do;
153 ct="&ctype"
154 %end;
155 %if "&mimetype"="text/html" %then %do;
156 url="&url%str(&)typeDefName=file";
157 %end;
158 %else %do;
159 url="&url";
160 %end;
161
162 headers "Accept"="application/json"
163 %if &grant_type=authorization_code %then %do;
164 "Authorization"="Bearer &&&access_token_var"
165 %end;
166 "Content-Disposition"= "&contentdisp filename=""&name""; name=""&name"";";
167run;
168%put &=SYS_PROCHTTP_STATUS_CODE;
169%put &=SYS_PROCHTTP_STATUS_PHRASE;
170%mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 201)
171 ,mac=&sysmacroname
172 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
173)
174%local libref2;
175%let libref2=%mf_getuniquelibref();
176libname &libref2 JSON fileref=&fname1;
177%put Grabbing the follow on link ;
178data &outds;
179 set &libref2..links end=last;
180 if rel='createChild' then do;
181 call symputx('href',quote(cats("&base_uri",href)),'l');
182 &dbg put (_all_)(=);
183 end;
184run;
185
186%put &sysmacroname: File &name successfully created:;%put;
187%put &base_uri%mfv_getpathuri(&path/&name);%put;
188%put &base_uri/SASJobExecution?_file=&path/&name;%put;
189%put &sysmacroname:;
190
191%mend mv_createfile;