Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_loadformat.test.2.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_loadformat.sas macro for multilabel formats
4 @details Multilabel records can be complete duplicates!! Also, the order is
5 important.
6
7 The provided formats create a table as follows:
8
9
10|TYPE:$1.|FMTNAME:$32.|START:$10000.|END:$10000.|LABEL:$32767.|MIN:best.|MAX:best.|DEFAULT:best.|LENGTH:best.|FUZZ:best.|PREFIX:$2.|MULT:best.|FILL:$1.|NOEDIT:best.|SEXCL:$1.|EEXCL:$1.|HLO:$13.|DECSEP:$1.|DIG3SEP:$1.|DATATYPE:$8.|LANGUAGE:$8.|
11|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
12|`C `|`GENDERML `|` `|` `|`Total people `|`1 `|`40 `|`12 `|`12 `|`0 `|` `|`0 `|` `|`0 `|`N `|`N `|`M `|` `|` `|` `|` `|
13|`C `|`GENDERML `|`1 `|`1 `|`Male `|`1 `|`40 `|`12 `|`12 `|`0 `|` `|`0 `|` `|`0 `|`N `|`N `|`M `|` `|` `|` `|` `|
14|`C `|`GENDERML `|`1 `|`1 `|`Total people `|`1 `|`40 `|`12 `|`12 `|`0 `|` `|`0 `|` `|`0 `|`N `|`N `|`M `|` `|` `|` `|` `|
15|`C `|`GENDERML `|`2 `|`2 `|`Female `|`1 `|`40 `|`12 `|`12 `|`0 `|` `|`0 `|` `|`0 `|`N `|`N `|`M `|` `|` `|` `|` `|
16|`C `|`GENDERML `|`2 `|`2 `|`Female `|`1 `|`40 `|`12 `|`12 `|`0 `|` `|`0 `|` `|`0 `|`N `|`N `|`M `|` `|` `|` `|` `|
17|`C `|`GENDERML `|`2 `|`2 `|`Thormale `|`1 `|`40 `|`12 `|`12 `|`0 `|` `|`0 `|` `|`0 `|`N `|`N `|`M `|` `|` `|` `|` `|
18|`C `|`GENDERML `|`2 `|`2 `|`Total people `|`1 `|`40 `|`12 `|`12 `|`0 `|` `|`0 `|` `|`0 `|`N `|`N `|`M `|` `|` `|` `|` `|
19|`N `|`AGEMLA `|`1 `|`4 `|`Preschool `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
20|`N `|`AGEMLA `|`1 `|`18 `|`Children `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
21|`N `|`AGEMLA `|`19 `|`120 `|`Adults `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
22|`N `|`AGEMLB `|`1 `|`4 `|`Preschool `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
23|`N `|`AGEMLB `|`1 `|`18 `|`Children `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
24|`N `|`AGEMLB `|`19 `|`120 `|`Adults `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
25|`N `|`AGEMLC `|`1 `|`18 `|`Children `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
26|`N `|`AGEMLC `|`1 `|`4 `|`Preschool `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
27|`N `|`AGEMLC `|`19 `|`120 `|`Adults `|`1 `|`40 `|`9 `|`9 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`SM `|` `|` `|` `|` `|
28
29
30 <h4> SAS Macros </h4>
31 @li mf_nobs.sas
32 @li mp_cntlout.sas
33 @li mp_loadformat.sas
34 @li mp_assert.sas
35 @li mp_assertdsobs.sas
36 @li mp_getformats.sas
37 @li mp_ds2md.sas
38
39
40**/
41
42/* prep format catalog */
43libname perm (work);
44
45/* create some multilabel formats */
46%let cat1=perm.test1;
47proc format library=&cat1;
48 value $genderml (multilabel notsorted)
49 '1'='Male'
50 '2'='Female'
51 '2'='Female'
52 '2'='Farmale'
53 '1','2',' '='Total people';
54 value agemla (multilabel)
55 1-4='Preschool'
56 1-18='Children'
57 19-120='Adults';
58 value agemlb (multilabel)
59 19-120='Adults'
60 1-18='Children'
61 0-1='Preschool'
62 1-2='Preschool'
63 2-3='Preschool'
64 1-4='Preschool';
65 value agemlc (multilabel notsorted)
66 19-120='Adults'
67 1-18='Children'
68 1-4='Preschool';
69run;
70
71%mp_cntlout(libcat=&cat1,cntlout=work.cntlout1)
72%mp_assertdsobs(work.cntlout1,
73 desc=Has 19 records,
74 test=EQUALS 19
75)
76
77data work.stagedata3;
78 set work.cntlout1;
79 if fmtname='AGEMLA' and label ne 'Preschool' then deleteme='Yes';
80 if fmtname='AGEMLB' and label = 'Preschool' then label='Kids';
81 if fmtname='GENDERML' and label='Farmale' then do;
82 output;
83 fmtrow=101; output;
84 end;
85 else output;
86run;
87
88
89%mp_loadformat(&cat1
90 ,work.stagedata3
91 ,loadtarget=YES
92 ,auditlibds=perm.audit
93 ,locklibds=0
94 ,delete_col=deleteme
95 ,outds_add=add_test1
96 ,outds_del=del_test1
97 ,outds_mod=mod_test1
98 ,mdebug=1
99)
100
101%mp_assert(
102 iftrue=(%mf_nobs(del_test1)=2),
103 desc=Test 1 - deleted obs,
104 outds=work.test_results
105)
106%mp_assert(
107 iftrue=(%mf_nobs(mod_test1)=4),
108 desc=Test 1 - mod obs,
109 outds=work.test_results
110)
111%mp_assert(
112 iftrue=(%mf_nobs(add_test1)=1),
113 desc=Test 1 - add obs,
114 outds=work.test_results
115)
116
117/* now check the order of the notsorted format */
118%mp_cntlout(libcat=&cat1,cntlout=work.cntlout2)
119
120%let check1=0;
121%let check2=0;
122%let check3=0;
123data test;
124 set work.cntlout2;
125 where fmtname='GENDERML';
126 putlog fmtrow= label=;
127 if _n_=4 and label='Farmale' then call symputx('check1',1);
128 if _n_=5 and label ne 'Farmale' then call symputx('check2',1);
129 if _n_=8 and label = 'Farmale' then call symputx('check3',1);
130run;
131%mp_assert(
132 iftrue=(&check1=1 and &check2=1 and &check3=1),
133 desc=Ensuring Farmale values retain their order,
134 outds=work.test_results
135)
136
137/**
138 * completely delete a format and make sure it is removed
139 */
140
141/* first, make sure these three formats exist */
142options insert=(fmtsearch=(&cat1));
143%mp_getformats(fmtlist=AGEMLA AGEMLB AGEMLC $GENDERML,outsummary=work.fmtdels)
144
145%let fmtlist=NONE;
146proc sql;
147select distinct cats(fmtname) into: fmtlist separated by ' ' from work.fmtdels;
148
149%mp_assert(
150 iftrue=(%mf_nobs(fmtdels)=4),
151 desc=Deletion test 1 - ensure formats exist for deletion (&fmtlist found),
152 outds=work.test_results
153)
154
155/* deltest1 - deleting every record */
156%mp_cntlout(libcat=&cat1,cntlout=work.cntloutdel1)
157data work.stagedatadel1;
158 set work.cntloutdel1;
159 if fmtname='AGEMLA';
160 deleteme='Yes';
161run;
162%mp_loadformat(&cat1
163 ,work.stagedatadel1
164 ,loadtarget=YES
165 ,auditlibds=perm.audit
166 ,locklibds=0
167 ,delete_col=deleteme
168 ,outds_add=add_testdel1
169 ,outds_del=del_testdel1
170 ,outds_mod=mod_testdel1
171 ,mdebug=1
172)
173%mp_getformats(fmtlist=AGEMLA,outsummary=work.fmtdel1)
174%mp_assert(
175 iftrue=(%mf_nobs(fmtdel1)=0),
176 desc=Deletion test 1 - ensure AGEMLA format was fully deleted,
177 outds=work.test_results
178)
179
180/* deltest2 - deleting every record except 1 */
181data work.stagedatadel2;
182 set work.cntloutdel1;
183 if fmtname='AGEMLB';
184 x+1;
185 if x>1 then deleteme='Yes';
186run;
187%mp_loadformat(&cat1
188 ,work.stagedatadel2
189 ,loadtarget=YES
190 ,auditlibds=perm.audit
191 ,locklibds=0
192 ,delete_col=deleteme
193 ,outds_add=add_testdel2
194 ,outds_del=del_testdel2
195 ,outds_mod=mod_testdel2
196 ,mdebug=1
197)
198%mp_getformats(fmtlist=AGEMLB,outsummary=work.fmtdel2)
199%mp_assert(
200 iftrue=(%mf_nobs(fmtdel2)=1),
201 desc=Deletion test 2 - ensure AGEMLB format was not fully deleted,
202 outds=work.test_results
203)
204
205
206/* deltest3 - deleting every record, and adding a new one */
207data work.stagedatadel3;
208 set work.cntloutdel1;
209 if fmtname='GENDERML';
210 deleteme='Yes';
211run;
212data work.stagedatadel3;
213 set work.stagedatadel3 end=last;
214 output;
215 if last then do;
216 deleteme='No';
217 /* must be a new fmtrow (key value) if adding new row in same load! */
218 fmtrow=1000;
219 start='Mail';
220 end='Mail';
221 output;
222 end;
223run;
224
225%mp_loadformat(&cat1
226 ,work.stagedatadel3
227 ,loadtarget=YES
228 ,auditlibds=perm.audit
229 ,locklibds=0
230 ,delete_col=deleteme
231 ,outds_add=add_testdel2
232 ,outds_del=del_testdel2
233 ,outds_mod=mod_testdel2
234 ,mdebug=1
235)
236%mp_getformats(fmtlist=$GENDERML,outsummary=work.fmtdel3)
237%mp_assert(
238 iftrue=(%mf_nobs(fmtdel3)=1),
239 desc=Deletion test 3 - ensure GENDERML format was not fully deleted,
240 outds=work.test_results
241)
242
243%mp_ds2md(work.fmtdel3)