Macros for SAS Application Developers
https://github.com/sasjs/core
ms_createfile.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Creates a file on SASjs Drive
4  @details Creates a file on SASjs Drive. To use the file as a Stored Program,
5  it must have a ".sas" extension.
6 
7  Example:
8 
9  filename stpcode temp;
10  data _null_;
11  file stpcode;
12  put '%put hello world;';
13  run;
14  %ms_createfile(/some/stored/program.sas, inref=stpcode)
15 
16  @param [in] driveloc The full path to the file in SASjs Drive
17  @param [in] inref= (0) The fileref containing the file to create.
18  @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
19 
20  <h4> SAS Macros </h4>
21  @li mf_getuniquefileref.sas
22  @li mf_getuniquename.sas
23  @li mp_abort.sas
24  @li ms_deletefile.sas
25 
26 **/
27 
28 %macro ms_createfile(driveloc
29  ,inref=0
30  ,mdebug=0
31  );
32 
33 /* first, delete in case it exists */
34 %ms_deletefile(&driveloc,mdebug=&mdebug)
35 
36 %local fname0 fname1 fname2 boundary fname statcd msg optval;
37 %let fname0=%mf_getuniquefileref();
38 %let fname1=%mf_getuniquefileref();
39 %let fname2=%mf_getuniquefileref();
40 %let boundary=%mf_getuniquename();
41 
42 /* avoid sending bom marker to API */
43 %let optval=%sysfunc(getoption(bomfile));
44 options nobomfile;
45 
46 data _null_;
47  file &fname0 termstr=crlf lrecl=32767;
48  infile &inref end=eof lrecl=32767;
49  if _n_ = 1 then do;
50  put "--&boundary.";
51  put 'Content-Disposition: form-data; name="filePath"';
52  put ;
53  put "&driveloc";
54  put "--&boundary";
55  put 'Content-Disposition: form-data; name="file"; filename="ignore.sas"';
56  put "Content-Type: text/plain";
57  put ;
58  end;
59  input;
60  put _infile_; /* add the actual file to be sent */
61  if eof then do;
62  put ;
63  put "--&boundary--";
64  end;
65 run;
66 
67 data _null_;
68  file &fname1 lrecl=1000;
69  infile "&_sasjs_tokenfile" lrecl=1000;
70  input;
71  if _n_=1 then put "Content-Type: multipart/form-data; boundary=&boundary";
72  put _infile_;
73 run;
74 
75 %if &mdebug=1 %then %do;
76  data _null_;
77  infile &fname0 lrecl=32767;
78  input;
79  put _infile_;
80  data _null_;
81  infile &fname1 lrecl=32767;
82  input;
83  put _infile_;
84  run;
85 %end;
86 
87 proc http method='POST' in=&fname0 headerin=&fname1 out=&fname2
88  url="&_sasjs_apiserverurl/SASjsApi/drive/file";
89 %if &mdebug=1 %then %do;
90  debug level=1;
91 %end;
92 run;
93 
94 %let statcd=0;
95 data _null_;
96  infile &fname2;
97  input;
98  putlog _infile_;
99  if _infile_='{"status":"success"}' then call symputx('statcd',1,'l');
100  else call symputx('msg',_infile_,'l');
101 run;
102 
103 %mp_abort(
104  iftrue=(&statcd=0)
105  ,mac=ms_createfile.sas
106  ,msg=%superq(msg)
107 )
108 
109 /* reset options */
110 options &optval;
111 
112 %mend ms_createfile;