restore.sas
Go to the documentation of this file.
1 /**
2  @file restore.sas
3  @brief Restores a data version
4  @details Only applies if the history is stored in the audit table
5 
6  <h4> SAS Macros </h4>
7  @li dc_assignlib.sas
8  @li mf_nobs.sas
9  @li mp_abort.sas
10  @li mp_ds2csv.sas
11  @li mp_stripdiffs.sas
12  @li mpeinit.sas
13  @li mpe_checkrestore.sas
14  @li mpe_loader.sas
15 
16  <h4> Service Inputs </h4>
17  <h5> restore_in </h5>
18 
19  |LOAD_REF:$32|
20  |---|
21  |DCXXXXXX|
22 
23  @version 9.2
24  @author 4GL Apps Ltd
25  @copyright 4GL Apps Ltd. This code may only be used within Data Controller
26  and may not be re-distributed or re-sold without the express permission of
27  4GL Apps Ltd.
28 
29 **/
30 
31 %mpeinit()
32 
33 %let loadref=;
34 data _null_;
35  set work.restore_in;
36  call symputx('loadref',load_ref);
37 run;
38 
39 /**
40  * Check if user has basic access permission to RESTORE the table
41  */
42 %put checking access;
43 %global allow_restore reason;
44 %mpe_checkrestore(&loadref,outresult=ALLOW_RESTORE,outreason=REASON)
45 
46 %mp_abort(iftrue= (&ALLOW_RESTORE ne YES)
47  ,mac=&_program..sas
48  ,msg=%str(Cannot restore because: &reason)
49 )
50 
51 /* grab the base DS */
52 proc sql noprint;
53 select cats(base_lib,'.',base_ds) into: tgtds
54  from &mpelib..mpe_submit
55  where TABLE_ID="&loadref";
56 
57 /* find the audit table */
58 select coalescec(audit_libds,"&mpelib..MPE_AUDIT"), loadtype, var_txto
59  into: difftable, :loadtype, :txto
60  from &mpelib..MPE_TABLES
61  where libref="%scan(&tgtds,1,.)"
62  & dsn="%scan(&tgtds,2,.)"
63  & &dc_dttmtfmt<tx_to;
64 %mp_abort(iftrue= ("&difftable"="0")
65  ,mac=&_program..sas
66  ,msg=%str(No audit table configured for &tgtds)
67 )
68 
69 /* assign the library */
70 %dc_assignlib(READ,%scan(&tgtds,1,.))
71 
72 /* if the target is an SCD2 table, create a view to get current snapshot */
73 %let fltr=%sysfunc(ifc(&loadtype=TXTEMPORAL,&dc_dttmtfmt<&txto,1=1));
74 
75 /* extract the differences to be applied */
76 %mp_stripdiffs(&tgtds,&loadref,&difftable
77  ,outds=work.mp_stripdiffs
78  ,filtervar=fltr
79  ,mdebug=&sasjs_mdebug
80 )
81 
82 /* abort if any issues */
83 %mp_abort(iftrue= (&syscc>0)
84  ,mac=&_program..sas
85  ,msg=%str(syscc=&syscc after stripdiffs)
86 )
87 %mp_abort(iftrue= (%mf_nobs(work.mp_stripdiffs)=0)
88  ,mac=&_program..sas
89  ,msg=%str(THERE ARE NO DIFFERENCES TO APPLY)
90 )
91 
92 /* create a new load ref */
93 %let mperef=DC%left(%sysfunc(datetime(),B8601DT19.3))_%substr(
94  %sysfunc(ranuni(0)),3,6)_%substr(%str(&sysjobid ),1,4);
95 
96 /* Create package folder */
97 %let dir=&mpelocapprovals/&mperef;
98 %mf_mkdir(&dir)
99 options notes mprint;
100 libname approve "&dir";
101 
102 /* take copy of macvars */
103 data _null_;
104  file "&dir/macvars.sas";
105  set sashelp.vmacro;
106  where scope='GLOBAL';
107  put '%let ' name '=' value ';';
108 run;
109 
110 /* copy the diffs dataset */
111 data approve.jsdset;
112  length _____DELETE__THIS__RECORD_____ $3;
113  if 0 then call missing(_____DELETE__THIS__RECORD_____);
114  set work.mp_stripdiffs;
115 run;
116 
117 /* export to csv */
118 %mp_ds2csv(approve.jsdset
119  ,dlm=COMMA
120  ,outfile="&dir/%trim(&tgtds).csv"
121  ,outencoding="UTF-8"
122  ,headerformat=NAME
123  ,termstr=CRLF
124 )
125 
126 %mp_abort(iftrue= (&syscc ne 0)
127  ,mac=&_program
128  ,msg=%str(syscc=&syscc when writing the CSV)
129 )
130 
131 %mpe_loader(mperef=&mperef
132  ,submitted_reason_txt=Restoring &loadref
133  ,dc_dttmtfmt=&dc_dttmtfmt
134 )
135 %mp_abort(mode=INCLUDE)
136 
137 %mp_abort(
138  iftrue=(%sysfunc(fileexist(%sysfunc(pathname(work))/mf_abort.error))=1)
139  ,mac=&_program..sas
140  ,msg=%str(mf_abort.error=1)
141 )
142 
143 %mp_abort(iftrue= (&syscc ne 0)
144  ,mac=&_program..sas
145  ,msg=%str(syscc=&syscc)
146 )
147 
148 /* send relevant SUCCESS values */
149 data work.restore_out;
150  loadref="&mperef";
151 run;
152 
153 
154 %webout(OPEN)
155 %webout(OBJ,restore_out)
156 %webout(CLOSE)
157