Macros for SAS Application Developers
https://github.com/sasjs/core
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
28 @param [in] access_token_var= The global macro variable to contain the access
29 token, if using authorization_code grant type.
30 @param [in] grant_type= (sas_services) Valid values are:
31 @li password
32 @li authorization_code
33 @li sas_services
34
35 @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
36
37 @version VIYA V.03.05
38 @author Allan Bowe, source: https://github.com/sasjs/core
39
40 <h4> SAS Macros </h4>
41 @li mf_getuniquefileref.sas
42 @li mf_isblank.sas
43 @li mp_abort.sas
44 @li mp_base64copy.sas
45 @li mp_binarycopy.sas
46 @li mv_createfolder.sas
47
48**/
49
50%macro mv_createfile(path=
51 ,name=
52 ,inref=
53 ,intype=BINARY
54 ,contentdisp=inline
55 ,access_token_var=ACCESS_TOKEN
56 ,grant_type=sas_services
57 ,mdebug=0
58 );
59%local dbg;
60%if &mdebug=1 %then %do;
61 %put &sysmacroname entry vars:;
62 %put _local_;
63%end;
64%else %let dbg=*;
65
66%local oauth_bearer;
67%if &grant_type=detect %then %do;
68 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
69 %else %let grant_type=sas_services;
70%end;
71%if &grant_type=sas_services %then %do;
72 %let oauth_bearer=oauth_bearer=sas_services;
73 %let &access_token_var=;
74%end;
75
76%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
77 and &grant_type ne sas_services
78 )
79 ,mac=&sysmacroname
80 ,msg=%str(Invalid value for grant_type: &grant_type)
81)
82
83%mp_abort(iftrue=(%mf_isblank(&path)=1 or %length(&path)=1)
84 ,mac=&sysmacroname
85 ,msg=%str(path value must be provided)
86)
87%mp_abort(iftrue=(%mf_isblank(&name)=1 or %length(&name)=1)
88 ,mac=&sysmacroname
89 ,msg=%str(name value with length >1 must be provided)
90)
91
92/* create folder if it does not already exist */
93%mv_createfolder(path=&path
94 ,access_token_var=&access_token_var
95 ,grant_type=&grant_type
96 ,mdebug=&mdebug
97)
98
99/* create file with relevant options */
100%local fref;
101%let fref=%mf_getuniquefileref();
102filename &fref filesrvc
103 folderPath="&path"
104 filename="&name"
105 cdisp="&contentdisp"
106 lrecl=1048544;
107
108%if &intype=BINARY %then %do;
109 %mp_binarycopy(inref=&inref, outref=&fref)
110%end;
111%else %if &intype=BASE64 %then %do;
112 %mp_base64copy(inref=&inref, outref=&fref, action=DECODE)
113%end;
114
115filename &fref clear;
116
117%local base_uri; /* location of rest apis */
118%let base_uri=%mf_getplatform(VIYARESTAPI);
119
120%put &sysmacroname: File &name successfully created in &path;
121%put &sysmacroname:;%put;
122%put &base_uri/SASJobExecution?_file=&path/&name;%put;
123%put &sysmacroname:;
124
125%mend mv_createfile;