Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_loadformat.test.1.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_loadformat.sas macro
4 @details first test regular formats, then informats
5
6 <h4> SAS Macros </h4>
7 @li mddl_dc_difftable.sas
8 @li mp_aligndecimal.sas
9 @li mp_cntlout.sas
10 @li mp_loadformat.sas
11 @li mp_assert.sas
12 @li mp_assertscope.sas
13
14**/
15
16/* prep format catalog */
17libname perm (work);
18
19%mddl_dc_difftable(libds=perm.audit)
20
21/* set up regular formats */
22data work.loadfmts;
23 /* matching start / end lengths (to baseds) are important */
24 length fmtname $32 start end $10000;
25 eexcl='Y';
26 type='N';
27 do i=1 to 10;
28 fmtname=cats('SASJS_',put(i,z4.),'X');
29 do j=1 to 20;
30 start=cats(j);
31 end=cats(j+1);
32 %mp_aligndecimal(start,width=16)
33 %mp_aligndecimal(end,width=16)
34 label= cats('Numeric Format ',start);
35 output;
36 end;
37 end;
38run;
39proc format cntlin=work.loadfmts library=perm.testcat;
40run;
41
42/*
43 use actual format data as test baseline, as proc format adds attributes eg
44 min/max etc
45*/
46%mp_cntlout(libcat=perm.testcat,cntlout=work.loadfmts2)
47
48/* make some test data */
49data work.stagedata;
50 set work.loadfmts2 end=lastobs;
51 by type fmtname;
52
53 if lastobs then do;
54 output;
55 fmtname='NEWFMT'!!cats(_n_,'x'); /* 1 new record */
56 start=cats(_n_);
57 end=cats(_n_+1);
58 %mp_aligndecimal(start,width=16)
59 %mp_aligndecimal(end,width=16)
60 label='newval'!!cats(_N_,'X');
61 output;
62 stop;
63 end;
64 else if last.fmtname then deleteme='Yes'; /* 9 deletions */
65 else if first.fmtname then label='modified '!!cats(_n_); /* 10 changes */
66
67 output;
68run;
69
70/* load the above */
71%mp_assertscope(SNAPSHOT)
72%mp_loadformat(perm.testcat
73 ,work.stagedata
74 ,loadtarget=YES
75 ,auditlibds=perm.audit
76 ,locklibds=0
77 ,delete_col=deleteme
78 ,outds_add=add_test1
79 ,outds_del=del_test1
80 ,outds_mod=mod_test1
81 ,mdebug=1
82)
83%mp_assertscope(COMPARE)
84
85%mp_assert(
86 iftrue=(%mf_nobs(del_test1)=9),
87 desc=Test 1 - delete obs,
88 outds=work.test_results
89)
90%mp_assert(
91 iftrue=(%mf_nobs(add_test1)=1),
92 desc=Test 1 - add obs,
93 outds=work.test_results
94)
95%mp_assert(
96 iftrue=(%mf_nobs(mod_test1)=10),
97 desc=Test 1 - mod obs,
98 outds=work.test_results
99)
100%mp_assert(
101 iftrue=(%mf_nobs(perm.audit)=440),
102 desc=Test 1 - audit table updated,
103 outds=work.test_results
104)
105data work.difftest;
106 set perm.audit;
107 where is_diff=1;
108run;
109%mp_assert(
110 iftrue=(%mf_nobs(work.difftest)>0),
111 desc=Test 1 - diffs were found,
112 outds=work.test_results
113)
114
115/* set up a mix of formats */
116data work.loadfmts3;
117 length fmtname $32 start end $10000;
118 eexcl='Y';
119 type='J';
120 do i=1 to 3;
121 fmtname=cats('SASJS_CI_',i,'X');
122 do j=1 to 4;
123 start=cats(j);
124 end=start;
125 label= cats('Char INFORMAT ',start);
126 output;
127 end;
128 end;
129 type='I';
130 do i=1 to 3;
131 fmtname=cats('SASJS_NI_',i,'X');
132 do j=1 to 4;
133 start=cats(j);
134 end=cats(j+1);
135 %mp_aligndecimal(start,width=16)
136 %mp_aligndecimal(end,width=16)
137 label= cats(ranuni(0));
138 output;
139 end;
140 end;
141 type='N';
142 do i=1 to 3;
143 fmtname=cats('SASJS_NF_',i,'X');
144 do j=1 to 4;
145 start=cats(j);
146 end=cats(j+1);
147 %mp_aligndecimal(start,width=16)
148 %mp_aligndecimal(end,width=16)
149 label= cats('Numeric Format ',start);
150 output;
151 end;
152 end;
153 type='C';
154 do i=1 to 3;
155 fmtname=cats('SASJS_CF_',i,'X');
156 do j=1 to 4;
157 start=cats(j);
158 end=start;
159 label= cats('Char Format ',start);
160 output;
161 end;
162 end;
163 drop i j;
164run;
165proc format cntlin=work.loadfmts3 library=perm.testcat3;
166run;
167%mp_cntlout(libcat=perm.testcat3,cntlout=work.loadfmts4)
168
169/* make some test data */
170data work.stagedata3;
171 set work.loadfmts4;
172 where type in ('I','J');
173 by type fmtname notsorted;
174 if type='I' then do;
175 if last.fmtname then do;
176 deleteme='Yes'; /* 3 deletions */
177 output;
178 end;
179 else if fmtrow le 3 then do; /* 9 changed values */
180 z=ranuni(0)*1000000;
181 start=cats(z);
182 end=cats(z+1);
183 %mp_aligndecimal(start,width=16)
184 %mp_aligndecimal(end,width=16)
185 output;
186 end;
187 end;
188 else do;
189 if last.fmtname then do;
190 output; /* 6 new records */
191 x=_n_;
192 x+1;start=cats("mod",x);end=start;label='newlabel1';fmtrow=fmtrow+1;
193 output;
194 x+1;start=cats("mod",x);end=start;label='newlabel2';fmtrow=fmtrow+2;
195 output;
196 end;
197 else if fmtrow le 3 then do; /* 9 more changed values */
198 start= cats("mod",_n_);
199 end=start;
200 label= "mod "||cats(ranuni(0)*100);
201 output;
202 end;
203 end;
204run;
205
206%mp_loadformat(perm.testcat3
207 ,work.stagedata3
208 ,loadtarget=YES
209 ,auditlibds=perm.audit
210 ,locklibds=0
211 ,delete_col=deleteme
212 ,outds_add=add_test2
213 ,outds_del=del_test2
214 ,outds_mod=mod_test2
215 ,mdebug=1
216)
217
218%mp_assert(
219 iftrue=(%mf_nobs(del_test2)=3),
220 desc=Test 2 - delete obs,
221 outds=work.test_results
222)
223%mp_assert(
224 iftrue=(%mf_nobs(mod_test2)=18),
225 desc=Test 2 - mod obs,
226 outds=work.test_results
227)
228%mp_assert(
229 iftrue=(%mf_nobs(add_test2)=6),
230 desc=Test 2 - add obs,
231 outds=work.test_results
232)
233