Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_getpk.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_getpk.sas macro
4
5 <h4> SAS Macros </h4>
6 @li mp_getpk.sas
7 @li mp_assert.sas
8 @li mp_assertdsobs.sas
9
10 <h4> Related Macros </h4>
11 @li mp_getpk.sas
12
13**/
14
15/* ensure PK arrives in corrrect order */
16proc sql;
17create table work.example1(
18 TX_FROM float format=datetime19.,
19 DD_TYPE char(16),
20 DD_SOURCE char(2048),
21 DD_SHORTDESC char(256),
22 constraint pk primary key(tx_from, dd_type,dd_source),
23 constraint unq unique(tx_from, dd_type),
24 constraint nnn not null(DD_SHORTDESC)
25);
26%mp_getpk(work,ds=example1,outds=test1)
27
28data _null_;
29 set work.test1;
30 call symputx('test1',pk_fields);
31run;
32
33%mp_assert(
34 iftrue=("&test1"="TX_FROM DD_TYPE DD_SOURCE"),
35 desc=mp_getpk gets regular PK values in correct order,
36 outds=work.test_results
37)
38
39/* unique key with NOT NULL captured */
40proc sql;
41create table work.example2(
42 TX_FROM float format=datetime19.,
43 DD_TYPE char(16),
44 DD_SOURCE char(2048),
45 DD_SHORTDESC char(256),
46 constraint unq1 unique(tx_from, dd_type),
47 constraint unq2 unique(tx_from, dd_type, dd_source),
48 constraint nnn not null(tx_from),
49 constraint nnnn not null(dd_type)
50);
51%mp_getpk(work,ds=example2,outds=test2)
52
53data _null_;
54 set work.test2;
55 call symputx('test2',pk_fields);
56run;
57
58%mp_assert(
59 iftrue=("&test2"="TX_FROM DD_TYPE"),
60 desc=mp_getpk gets unique constraint with NOT NULL in correct order
61)
62
63/* unique key without NOT NULL NOT captured */
64proc sql;
65create table work.example3(
66 TX_FROM float format=datetime19.,
67 DD_TYPE char(16),
68 DD_SOURCE char(2048),
69 DD_SHORTDESC char(256),
70 constraint unq1 unique(tx_from, dd_type),
71 constraint unq2 unique(tx_from, dd_type, dd_source),
72 constraint nnn not null(tx_from)
73);
74%mp_getpk(work,ds=example3,outds=test3)
75
76data _null_;
77 set work.test3;
78 call symputx('test3',pk_fields);
79run;
80
81%mp_assert(
82 iftrue=("&test3 "=" "),
83 desc=mp_getpk does not capture unique constraint without NOT NULL,
84 outds=work.test_results
85)
86
87/* constraint capture at library level is functional - uses first 2 tests */
88%mp_getpk(work,outds=test4)
89
90%mp_assertdsobs(work.test4,test=ATLEAST 2)
91
92/* unique & not null INDEX captured */
93proc sql;
94create table work.example5(
95 TX_FROM float format=datetime19.,
96 DD_TYPE char(16),
97 DD_SOURCE char(2048),
98 DD_SHORTDESC char(256)
99);
100proc datasets lib=work noprint;
101 modify example5;
102 index create tx_from /nomiss unique;
103quit;
104%mp_getpk(work,ds=example5,outds=test5)
105data _null_;
106 set work.test5;
107 call symputx('test5',pk_fields);
108run;
109%mp_assert(
110 iftrue=("&test5"="TX_FROM"),
111 desc=mp_getpk captures single column not null unique index,
112 outds=work.test_results
113)
114
115/* unique & not null COMPOSITE INDEX captured */
116proc sql;
117create table work.example6(
118 TX_FROM float format=datetime19.,
119 DD_TYPE char(16),
120 DD_SOURCE char(2048),
121 DD_SHORTDESC char(256)
122);
123proc datasets lib=work noprint;
124 modify example6;
125 index create pk_6=(tx_from dd_type) /nomiss unique;
126quit;
127%mp_getpk(work,ds=example6,outds=test6)
128data _null_;
129 set work.test6;
130 call symputx('test6',pk_fields);
131run;
132%mp_assert(
133 iftrue=("&test6"="TX_FROM DD_TYPE"),
134 desc=mp_getpk captures multiple column not null unique index,
135 outds=work.test_results
136)