-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.qmd
748 lines (492 loc) · 16.5 KB
/
index.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
---
# title: "Introduction to testing in SAS"
# author: "Andrew Li"
format:
revealjs:
include-in-header: header.html
css: style.css
theme: simple
slide-number: true
code-link: true
preview-links: auto
#editor: visual
execute:
echo: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
## Introduction to Testing in SAS with `%check_rows` {style="margin-top: 2em; margin-bottom: 1em;"}
<!--NOTE: Do NOT render this file - go to render.R -->
::: {style="margin-top: 1em; margin-bottom: 1em; font-size: 1em"}
Andrew Li & Mike Atkinson
![](media/intro_meme.jpg){.absolute width="400" height="400" right="40" top="175"}
:::
## Unit testing
Unit tests compare the *expected* output with the *actual* output.
`{SASUnit}` and `{FUTS}` are popular frameworks for writing unit tests in SAS.
<br>
**"A unit test provides a strict, written contract that the piece of code must satisfy"**
\- Wikipedia
## Benefits of unit testing {.smaller}
As a SAS programmer/analyst, you should consider implementing tests in your workflow.
::: incremental
- Problems are found early in the development cycle
- Increased confidence when making changes - insures against unintentionally changing code behaviour
- changes can be tests any time easily
- Can document the functionality of the program
- Prevents re-introducing already fixed bugs
- etc.
:::
## When do we need tests? {.smaller}
There are two main applications of SAS:
. . .
:::: {.columns}
::: {.column width='50%'}
**One off Programs**
::: {.incremental}
- Tests are often informal and not reproducible
- Checking the log
- Comparing results with what we need
- Tracking tables
:::
:::
::: {.column width='50%'}
**Standardized and Reusable Programs **
::: {.incremental}
- Macros or Programs used in different reports or studies
- It needs to work with different data and parameters
- *In this case, tests should be included!*
:::
:::
::::
. . .
# Introduction to `%check_rows();`
Perform a check on table(s) row counts using the expression provided.
<br>
. . .
```{eval=FALSE, class='SAS'}
%check_rows(expr, severity = error);
```
. . .
## Expression {.smaller}
The expression `{expr}` parameter captures what you expect the rows to actually be.
| expected | actual | tests |
| :--------: | :------: | :-----: |
| {{< fa regular file-lines size=2xl >}} | {{< fa regular file-lines size=2xl >}} | {{< fa regular circle-check size=2xl >}} |
| {{< fa regular file-lines size=2xl >}} | {{< fa regular file-code size=2xl >}} | {{< fa regular circle-xmark size=2xl >}} |
. . .
<br>
`%check_rows` compares the expected value with actual value of row counts.
| description | expected | actual | tests |
|:--------: | :---: | :---: | :---: |
| number of observations in `mylib.clinics` | 20 | 20 | {{< fa regular circle-check size=2xl >}} |
| number of observations in `mylib.clincs` | 20 | 19 | {{< fa regular circle-xmark size=2xl >}} |
. . .
## Expression rules {.smaller}
::: {.callout-tip}
## Valid expressions
- The expression parameter must contain a comparison operator and only table names.
- Accepted comparison operators: `=, <>, >=, <=, >, <`
- The "left hand side" of the operator allows for a table name with optional schema prefix followed by an optional valid arithmetic operator and another table name with optional schema prefix.
- Accepted arithmetic operators: `+, -, *`
- The "right hand side" of the operator follows same regular expression rules.
```{eval=FALSE, class='SAS'}
%check_rows(%str(table_1 = table_2));
%check_rows(%str(table_1 + table_2 = table_3));
%check_rows(%str(table_1 + table_2 = table_3 + table_4));
%check_rows(%str(mylib.all = details + mylib2.summary));
```
:::
. . .
:::{.callout-important}
## Invalid Expressions
- Is not a valid library name or table name.
- Contains more then one or is missing a comparison operator.
```{eval=FALSE, class='SAS'}
%check_rows(%str(1_table_1 = table_2)); /* Invalid table name */
%check_rows(%str(table_1 <= 2mylib.sales)); /* Invalid library name */
%check_rows(%str(table_1 0)); /* Missing comparison operator */
%check_rows(%str(table_1 >= 50 // 3)); /* Invalid arithmetic operator */
```
:::
. . .
## Sending mixed signals {.smaller}
:::: {.columns}
::: {.column width='10%'}
<br>
```{r, eval=TRUE, echo=FALSE}
knitr::include_graphics("media/warning.png")
```
:::
::: {.column width='90%'}
:::{.callout-note}
## Types of messages/exceptions
| Condition | Severity | Meaning |
| :-------: | :------------: | :------------------------------------------------------: |
| `note` | low | execution was successful but returns additional information |
| `warning` | medium | execution encountered some problem but the job was run |
| `error` | high | execution was stopped as there was no way to continue |
| `abend` | very high | execution was stopped and aborted |
:::
:::
::::
- Notes are the most innocuous messages in SAS - they simply inform you of the status of the program.
- Warnings are less dire than errors but goes ahead and runs the program anyways. Some warnings are harmless, but some indicate grave problems.
- Errors bring programs to a halt and you *must* attend to them.
- Abend is not actually an SAS message but it is a possible parameter in the macro. This will throw an error *and* abort the program.
## Generate example tables
```{eval=FALSE, class='SAS'}
data one
two
bad_records
good_records;
do i = 1 to 89;
if (i <= 20) then output one;
if (i <= 20) then output two;
if (i <= 2) then output bad_records;
if (i <= 100) then output good_records;
end;
stop;
run;
/* table `one` has 20 rows */
/* table `two` has 20 rows */
/* table `bad_records` has 2 rows */
/* table `good_records` has 89 rows */
```
## Severity parameter {.smaller}
The default value for severity param is to throw an error.
:::: {.columns}
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
/* example of a passed test */
/* severity set to default */
%check_rows(%str(one = two));
```
:::
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
NOTE: check_rows: Success!
NOTE: check_rows: one = two
NOTE: check_rows: 20 = 20
```
:::
::::
. . .
. . .
<br>
The code can continue if you set `severity = warning`.
:::: {.columns}
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
/* example of a failed test with a warning */
%check_rows(%str(bad_records = 0),
severity = warning);
```
:::
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
WARNING: check_rows: Expression Failed!
WARNING: check_rows: Not True: bad_records = 0
WARNING: check_rows: Not True: 2 = 0
```
:::
::::
. . .
. . .
<br>
The same test case will fail if the severity param is set to the default value.
:::: {.columns}
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
/* example of a failed test with an error */
/* severity set to default */
%check_rows(%str(bad_records = 0));
```
:::
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
ERROR: check_rows: Expression Failed!
ERROR: check_rows: Not True: bad_records = 0
ERROR: check_rows: Not True: 2 = 0
```
:::
::::
. . .
## Severity set to abend {.smaller}
Although `abend` is not a real SAS message, it is an option for `%check_rows();`
. . .
:::{.callout-important}
## Be careful when using abend
If severity is set to abend and the test fails, it will stop processing immediately and terminate your SAS Enterprise Guide/Studio.
Thus, it is **crucial** that you are careful when using abend.
:::
. . .
. . .
:::{.callout-tip}
## Use abend selectively
It is worthwhile to use abend in automated SAS programs becuase you want the failure to grab your attention.
:::
. . .
## A simple example {.smaller}
In this example, we get excel data from clinics to update our database.
```{eval=FALSE, class='SAS'}
%let clinic_number = 123 456 789 888;
%macro read_excel;
%do i = 1 %to %sysfunc(countw(&clinic_number));
%let num = %scan(&clinic_number, &i);
proc import dbms = xlsx
out = clinic_&num.
file = "data/clinic_&num..xlsx"
REPLACE;
run;
%end;
%mend;
%read_excel;
/* make sure the datasets imported are good */
%check_rows(%str(clinic_123 > 100), severity = warning);
%check_rows(%str(clinic_456 > 100), severity = warning);
```
```{eval=FALSE, class='SAS'}
/* concatenate the newly imported tables */
data all_clinics;
set clinic_123 clinic_456 clinic_789 clinic_888;
run;
```
. . .
Now, we can include tests to make sure that `all_clinics` is what we think it is.
```{eval=FALSE, class='SAS'}
%check_rows(%str(clinic_123 + clinic_456 + clinic_789 + clinic_888 = all_clinics));
%check_rows(%str(all_clinics = 0), severity = abend);
```
. . .
## Another simple example {.smaller}
We need to divide $25,000 between practitioners based on total complexity.
```{eval=FALSE, class='SAS'}
%let tot_funds = 25000;
proc sql;
create table prac_tot as
select sum(total_complexity) as tot_total_complexity
from prac_list;
quit;
/* Calculate total for each practitioner */
data prac_funding;
drop tot_total_complexity;
if (_n_ = 1) then set prac_tot;
set prac_list;
prac_funding = round(product(tot_funds, total_complexity / tot_total_complexity), 0.01);
run;
```
:::: {.columns}
::: {.column width='50%'}
`prac_list`
```{r eval=TRUE, echo=FALSE}
data.frame(pracnum = c("A5234", "B2519", "C6868", "D5261", "E1801", "F9182", "G8123", "H6010"),
total_complexity = c(1317.7, 1457.2, 1145.2, 1004.5, 1258.7, 1332.2, 994.1, 1011.0))
```
:::
::: {.column width='50%'}
`prac_funding`
```{r eval=TRUE, echo=FALSE}
data.frame(pracnum = c("A5234", "B2519", "C6868", "D5261", "E1801", "F9182", "G8123", "H6010"),
total_complexity = c(1317.7, 1457.2, 1145.2, 1004.5, 1258.7, 1332.2, 994.1, 1011.0),
prac_funding = c(3460.13, 3826.44, 3007.16, 2637.7, 3305.2, 3498.2, 2610.39, 2654.77))
```
:::
::::
. . .
*But, there is a not so obvious problem*
. . .
## The not so obvious problem {.smaller}
The previous workflow resulted in a very minor but important error. The total allocation does *not* equal to $25,000. Here is a rewrite of the previous code that includes a check.
```{eval=FALSE, class='SAS'}
%let tot_funds = 25000;
data prac_funding (drop=check_prac_tot)
total_not_match (keep=check_prac_tot);
drop tot_total_complexity;
retain check_prac_tot 0;
if (_n_ = 1) then set prac_tot;
set prac_list end=eof;
prac_funding = round(product(tot_funds, total_complexity / tot_total_complexity), 0.01);
check_prac_tot + prac_funding;
output prac_funding;
if (eof) then do;
if (check_prac_tot ne &tot_funds) then output total_not_match;
end;
run;
%check_rows(%str(total_not_match = 0));
```
. . .
:::: {.columns}
::: {.column width='50%'}
`total_not_match`
```{r eval=TRUE, echo=FALSE}
data.frame(check_prac_tot = 24999.99)
```
:::
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
ERROR: check_rows: Expression Failed!
ERROR: check_rows: Not True: total_not_match = 0
ERROR: check_rows: Not True: 1 = 0
```
:::
::::
. . .
## Missing rows for by variable in merge {.smaller}
Combining datasets is a very common step. Consider the following tables:
:::: {.columns}
::: {.column width='50%'}
```{r eval=TRUE, echo=FALSE}
data.frame(pracnum = c("A5234", "B2519", "C6868", "D5261"),
complexity_score = c(78, 90, 85, 40))
```
:::
::: {.column width='50%'}
```{r eval=TRUE, echo=FALSE}
data.frame(pracnum = c("A5234", "B2519", "C6868", "D5261", "E1801", "F9182"),
rate_score = c(1317.7, 1457.2, 1145.2, 1004.5, 1258.7, 1332.2))
```
:::
::::
. . .
When we merge the two datasets the resulting table will have missing values. This may cause unexpected results *and* SAS does not let you know this occured.
:::: {.columns}
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
/* Merge the two data sets */
data final;
merge complexity_table rate_table;
by pracnum;
run;
```
:::
::: {.column width='50%'}
```{r eval=TRUE, echo=FALSE}
data.frame(pracnum = c("A5234", "B2519", "C6868", "D5261", "E1801", "F9182"),
complexity_score = c("78", "90", "85", "40", ".", "."),
rate_score = c(1317.7, 1457.2, 1145.2, 1004.5, 1258.7, 1332.2))
```
:::
::::
. . .
. . .
Consider adding a test for this common pitfall.
```{eval=FALSE, class='SAS'}
%check_rows(%str(final = complexity_table), severity = warning);
```
```{eval=FALSE, class='SAS'}
WARNING: check_rows: Expression Failed!
WARNING: check_rows: Not True: final = complexity_table
WARNING: check_rows: Not True: 6 = 4
```
. . .
## Duplicate by values {.smaller}
The `complexity_table` has duplicate values.
:::: {.columns}
::: {.column width='50%'}
```{r eval=TRUE, echo=FALSE}
data.frame(pracnum = c("A5234", "B2519", "B2519", "B2519"),
complexity_score = c(78, 90, 85, 40))
```
:::
::: {.column width='50%'}
```{r eval=TRUE, echo=FALSE}
data.frame(pracnum = c("A5234", "B2519"),
rate_score = c(1317.7, 1457.2))
```
:::
::::
. . .
When you merge these two datasets, we get additional observations. Again, this may cause unexpected results *and* SAS does not let you know this occured.
:::: {.columns}
::: {.column width='50%'}
```{eval=FALSE, class='SAS'}
/* Merge the two data sets */
data combined_table;
merge complexity_table rate_table;
by pracnum;
run;
```
:::
::: {.column width='50%'}
```{r eval=TRUE, echo=FALSE}
data.frame(pracnum = c("A5234", "B2519", "B2519", "B2519"),
complexity_score = c(78, 90, 85, 40),
rate_score = c(1317.7, 1457.2, 1457.2, 1457.2))
```
:::
::::
. . .
. . .
Again, consider adding tests for this common pitfall.
```{eval=FALSE, class='SAS'}
%check_rows(%str(combined_table = rate_table));
```
```{eval=FALSE, class='SAS'}
ERROR: check_rows: Expression Failed!
ERROR: check_rows: Not True: combined_table = rate_table
ERROR: check_rows: Not True: 4 = 3
```
. . .
## Missing values {.smaller}
Getting the sum when a variable is missing leads to the resultant being missing as well. Again, SAS does not let you know this occured.
```{eval=FALSE, class='SAS'}
data total_num_prac;
interior_prac = .; fraser_health_prac = 8790; northern_health_prac = 9896;
total_prac = interior_prac + fraser_health_prac + northern_health_prac;
run;
```
```{r eval=TRUE, echo=FALSE}
data.frame(interior_prac = ".",
fraser_health_prac = "8790",
northern_health_prac = "9896",
total_prac = ".")
```
. . .
In this case, we can create a new table that only contains missing values and then perform a test on this new table.
```{eval=FALSE, class='SAS'}
proc sql;
create table missing_values_tbl as
select total_prac
from total_num_prac
where total_prac is missing;
quit;
%check_rows(%str(missing_values_tbl = 0))
```
```{eval=FALSE, class='SAS'}
ERROR: check_rows: Expression Failed!
ERROR: check_rows: Not True: missing_values_tbl = 0
ERROR: check_rows: Not True: 1 = 0
```
. . .
## Installation {.smaller}
If you are with the Ministry of Health, you already have access to this macro via SAS without installation!
If you are not a Ministry of Health, you will need to install it via [GitHub](https://github.com/andr3wli/testmacrolib).
```{zsh}
# clone the forked repo into your computer
git clone https://github.com/andr3wli/testmacrolib.git
```
<br>
After you have downloaded the repo locally (somewhere SAS can access), update the sasautos to the correct path.
```{eval=FALSE, class='SAS'}
options insert=(sasautos="/Desktop/testmacrolib/check");
```
#
Source code for the **slides** can be found on [GitHub](https://github.com/andr3wli/sas_testing-check_rows)
Source code for the **macro** can be found on [Github](https://github.com/andr3wli/testmacrolib/blob/main/check/check_rows.sas)
# Find me here 👇
<!-- Need to install fontawesome extension for quartro: https://github.com/quarto-ext/fontawesome -->
<!-- Note: will need at least quarto version 1.2.198 -->
{{< fa solid envelope >}} [Email](mailto:andrew.li@gov.bc.ca){target="_blank"}
{{< fa brands twitter >}} [Twitter](http://twitter.com/drewroc6){target="_blank"}
{{< fa brands linkedin >}} [LinkedIn](https://ca.linkedin.com/in/andrew-li-781011240){target="_blank"}
{{< fa brands github >}} [GitHub](https://github.com/andr3wli){target="_blank"}
{{< fa solid link >}} [Website](https://www.andrewcli.com/){target="_blank"}
# Thank You!
Questions? 😊
## Session information {.smaller}
```{r eval=TRUE}
quarto::quarto_version()
sessioninfo::session_info(include_base = TRUE)
```