Loading...
Searching...
No Matches
loadfile.sas
Go to the documentation of this file.
1/**
2 @file loadfile.sas
3 @brief Loads a file
4 @details
5
6 mp_binarycopy is declared for the SAS 9.3 and earlier fallback in the copy
7 step below: the %mp_binarycopy call is commented out on 9.4 and later, so
8 nothing in the file calls it.
9
10 @sasjslint {"noUnusedMacros": false}
11
12 <h4> SAS Macros </h4>
13 @li mddl_sas_cntlout.sas
14 @li mp_abort.sas
15 @li mf_getplatform.sas
16 @li mf_getuser.sas
17 @li mf_getvarlist.sas
18 @li mf_mkdir.sas
19 @li mf_verifymacvars.sas
20 @li mf_wordsinstr1butnotstr2.sas
21 @li dc_assignlib.sas
22 @li mpe_getgroups.sas
23 @li mp_lockfilecheck.sas
24 @li mpe_loader.sas
25 @li mp_cleancsv.sas
26 @li mp_binarycopy.sas
27 @li mp_validatecol.sas
28 @li mpeinit.sas
29
30 @version 9.2
31 @author 4GL Apps Ltd
32 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
33 and may not be re-distributed or re-sold without the express permission of
34 4GL Apps Ltd.
35
36**/
37
38%global table dlm;
39%mpeinit(fetch=NO)
40
41%global _WEBIN_FILENAME1 _WEBIN_FILENAME2
42 _WEBIN_FILEREF _WEBIN_FILEREF1 _WEBIN_FILEREF2;
43%macro load();
44%if %mf_getplatform()=SASVIYA %then %do;
45 %global _webin_fileuri _webin_fileuri1 _webin_fileuri2;
46 %let _webin_fileuri1=%sysfunc(coalescec(&_webin_fileuri1,&_webin_fileuri));
47 %if "&_webin_fileuri1" ne "" %then %do;
48 %put &=_webin_fileuri1;
49 filename sjfref1 filesrvc "&_webin_fileuri1";
50 %let _WEBIN_FILEREF1=sjfref1;
51 %end;
52 %if "&_webin_fileuri2" ne "" %then %do;
53 %put &=_webin_fileuri2;
54 filename sjfref2 filesrvc "&_webin_fileuri2";
55 %let _WEBIN_FILEREF2=sjfref2;
56 %end;
57%end;
58%mend load;
59%load()
60
61%let _WEBIN_FILENAME1=%sysfunc(coalescec(&_WEBIN_FILENAME1,&_WEBIN_FILENAME));
62%let _WEBIN_FILEREF1=%sysfunc(coalescec(&_WEBIN_FILEREF1,&_WEBIN_FILEREF));
63
64%let abort=0;
65/* we do not know if the excel file will be first or second fileref */
66data _null_;
67 ext1=upcase(scan(symget('_WEBIN_FILENAME1'),-1,'.'));
68 ext2=upcase(scan(symget('_WEBIN_FILENAME2'),-1,'.'));
69 if ext1='CSV' then do;
70 csvname=symget('_WEBIN_FILENAME1');
71 csvref=symget('_WEBIN_FILEREF1');
72 xlsname=symget('_WEBIN_FILENAME2');
73 xlsref=symget('_WEBIN_FILEREF2');
74 end;
75 else if ext2='CSV' then do;
76 csvname=symget('_WEBIN_FILENAME2');
77 csvref=symget('_WEBIN_FILEREF2');
78 xlsname=symget('_WEBIN_FILENAME1');
79 xlsref=symget('_WEBIN_FILEREF1');
80 end;
81 else call symputx('abort',1);
82
83 call symputx('csvname',csvname);
84 call symputx('csvref',csvref);
85 call symputx('xlsname',xlsname);
86 call symputx('xlsref',coalescec(xlsref,'0'));
87run;
88
89%mp_abort(iftrue= (&abort=1)
90 ,mac=&_program
91 ,msg=%str(File "&csvname" or "&xlsname" must be a CSV!
92 (Comma separated with .csv extension))
93)
94
95
96%let user=%mf_getuser();
97
98%mp_abort(
99 iftrue=(%mf_verifymacvars(table)=0)
100 ,mac=&_program
101 ,msg=%str(Missing: table)
102)
103
104%let table=%upcase(%trim(&table));
105
106/* load parameters */
107data _null_;
108 libds=upcase(symget('table'));
109 call symputx('orig_libds',libds);
110 call symputx('orig_lib',scan(libds,1,'.'));
111 call symputx('orig_ds',scan(libds,2,'.'));
112 is_fmt=0;
113 if substr(cats(reverse(libds)),1,3)=:'CF-' then do;
114 libds=scan(libds,1,'-');
115 putlog "Format Catalog Captured";
116 libds='work.fmtextract';
117 call symputx('libds',libds);
118 call execute('%mddl_sas_cntlout(libds=work.fmtextract)');
119 is_fmt=1;
120 end;
121 else call symputx('libds',libds);
122 call symputx('is_fmt',is_fmt);
123 /* validate libds to prevent code injection */
124 %mp_validatecol(LIBDS,LIBDS,is_libds)
125 if is_libds=0 then do;
126 putlog 'ERR' 'OR: Invalid libds:' libds;
127 call symputx('bad_libds',1);
128 end;
129 else call symputx('bad_libds',0);
130 putlog (_all_)(=);
131run;
132
133%mp_abort(iftrue= (&bad_libds=1)
134 ,mac=&_program
135 ,msg=%str(Invalid libds supplied)
136)
137
138/* check that the user has the requisite access */
139%mpe_getgroups(user=&user,outds=groups)
140
141proc sql;
142create table accesscheck as
143select * from groups
144 where groupname="&mpeadmins"
145 or groupname in (select sas_group from &mpelib..mpe_security
146 where &dc_dttmtfmt. lt tx_to
147 and access_level="EDIT"
148 and (
149 (libref="&orig_lib" and dsn="&orig_ds")
150 or (libref="&orig_lib" and dsn="*ALL*")
151 or (libref="*ALL*" and dsn="*ALL*")
152 or (libref="*ALL*" and dsn="&orig_ds")
153 ));
154%let nobs=;
155select count(*) into: nobs from &syslast;
156%mp_abort(iftrue= (&nobs=0)
157 ,mac=&sysmacroname
158 ,msg=%str(&user not authorised to load &orig_libds per &mpelib..mpe_security)
159)
160
161%dc_assignlib(WRITE,&orig_lib)
162
163%mp_abort(iftrue= (&syscc ge 4)
164 ,mac=loadfile
165 ,msg=%str(Issue assigning library &orig_lib)
166)
167
168%global txfrom txto processed rk;
169
170data _null_;
171 set &mpelib..MPE_TABLES;
172 where libref="&orig_lib" and dsn="&orig_ds";
173 call symputx('txfrom',var_txfrom);
174 call symputx('txto',var_txto);
175 call symputx('processed',var_processed);
176 if not missing(RK_UNDERLYING) then call symputx('rk',buskey);
177run;
178
179%mp_lockfilecheck(libds=&orig_libds)
180
181data compare;
182 set &libds(drop=&txfrom &txto &processed &rk);
183 stop;
184run;
185
186%mp_abort(iftrue= (&syscc ne 0)
187 ,mac=&_program..sas
188 ,msg=%str(syscc=&syscc line 80)
189)
190
191/* get line terminator, assume it's the first cr, lf, or crlf */
192data _null_;
193 length text $32767 term $4;
194 call missing (of _all_);
195 fid=fopen("&csvref",'I',32767,'b');
196 rc=fread(fid);
197 rc2=fget(fid,text,32767);
198 cr=find(text,'0D'x );
199 lf=find(text,'0A'x );
200 crlf=find(text,'0D0A'x);
201 rc=fclose(fid);
202 if crlf>0 & cr<crlf then term='CR';
203 else if crlf>0 & crlf<lf then term='CRLF';
204 else if lf>0 & cr>0 & lf<cr then term='LF';
205 else if lf>0 then term='LF';
206 else term='CR';
207 call symputx('termstr',term);
208run;
209
210data _null_;
211 infile &csvref lrecl=32000 dsd termstr=&termstr;
212 input;
213 length incols_unsorted $32000 dlm $1;
214 incols_unsorted=compress(upcase(_infile_),"'"!!'"');
215 /* dlm has length 1 so will be the first non alpha / digit char */
216 /* expectation is that there will not be any crazy characters in first col! */
217 dlm=compress(incols_unsorted,'_ ','ad');
218 incols_unsorted=compress(incols_unsorted,dlm!!'_','kado');
219 incols_unsorted=tranwrd(incols_unsorted,dlm,' ');
220 call symputx('incols_unsorted',incols_unsorted);
221 call symputx('dlm',dlm);
222 putlog incols_unsorted=;
223 putlog dlm=;
224 stop;
225run;
226
227%mp_abort(iftrue= (&syscc ne 0)
228 ,mac=&_program..sas
229 ,msg=%str(syscc=&syscc line 99)
230)
231
232%let basecols=%upcase(%mf_getvarlist(work.compare,dlm=%str( )));
233
234%let missing_cols=%trim(
235 %mf_wordsInStr1ButNotStr2(
236 Str1=&basecols
237 ,Str2=&incols_unsorted
238 ));
239
240%let msg=
241 Expected cols: <b>&basecols</b>
242 </br>Received cols: <b>&incols_unsorted</b>
243 </br>Missing cols: <b>&missing_cols</b>
244;
245%mp_abort(iftrue= (%length(%trim(&missing_cols)) > 1 or &syscc ne 0)
246 ,mac=mpestp_loadfile.sas
247 ,msg=%superq(msg)
248)
249%let msg=0;
250
251
252
253PROC FORMAT;
254 picture yymmddhhmmss other='%0Y%0m%0d_%0H%0M%0S' (datatype=datetime);
255RUN;
256
257/* create a dataset key (datetime plus 6 digit random number plus PID) */
258%let mperef=DC%left(%sysfunc(datetime(),B8601DT19.3))_%substr(
259 %sysfunc(ranuni(0)),3,6)_%substr(%str(&sysjobid ),1,4);
260
261/* Create package folder and redirect the log */
262%let dir=&mpelocapprovals/&mperef;
263%mf_mkdir(&dir)
264
265/* clean embedded line breaks and force CRLF line endings */
266%mp_cleancsv(in=&csvref, out=&dir/&orig_libds..csv)
267%mp_abort(iftrue= (&syscc ne 0)
268 ,mac=&_program..sas
269 ,msg=%str(issue in mp_cleancsv)
270)
271
272%put; %put; %put log is being redirected;
273%let url=_program=%substr(&_program
274 ,1,%length(&_program)-8)getlog%nrstr(&)table=&mperef;
275%put to retrieve, visit this url:; %put;%put;
276%put &url;
277%put;
278
279/* proc printto log="&dir/weblog.txt";run; */
280
281options mprint;
282%put &=mperef;
283%put &=termstr;
284%put &=dlm;
285
286%mpe_loader(mperef=&mperef
287 ,submitted_reason_txt=%quote(File upload: %superq(csvname))
288 ,dlm=%superq(dlm)
289 ,url=%superq(url)
290 ,termstr=CRLF
291 ,dc_dttmtfmt=&dc_dttmtfmt
292)
293/* mp_abort cannot exit cleanly from inside a %include, so an abort there
294 (eg in a hook script run via mp_include) only records the failure in
295 work.mp_abort_errds. mode=INCLUDE picks that up and aborts properly here. */
296%mp_abort(mode=INCLUDE)
297
298%mp_abort(
299 iftrue= (%sysfunc(fileexist(%sysfunc(pathname(work))/mf_abort.error)) ne 0)
300 ,mac=&_program
301 ,msg=%nrstr(Problem occurred in &sysmacroname (abort marker file found))
302)
303
304%mp_abort(iftrue= (&syscc ne 0)
305 ,mac=mpestp_loadfile.sas
306 ,msg=%str(syscc=&syscc)
307)
308
309filename outref "&dir/BKP_&xlsname";
310
311data _null_;
312 if "&xlsref" ne "0" then do;
313 rc=fcopy("&xlsref","outref");
314 end;
315run;
316
317/**
318 * if running 9.3 or older, delete step above and enable macro below
319%mp_binarycopy(iftrue=("&xlsref" ne "0"),inref=&xlsref,outref=outref)
320 */
321
322%mp_abort(iftrue= (&syscc ne 0)
323 ,mac=&sysmacroname
324 ,msg=%str(syscc=&syscc when backing up source file &xlsname)
325)
326
327data sasparams;
328 STATUS='SUCCESS';
329 DSID="&mperef";
330run;
331
332%webout(OPEN)
333%webout(OBJ,sasparams)
334%webout(CLOSE)
335
336
337%mpeterm()