-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path3030_2019.sas
200 lines (182 loc) · 6.32 KB
/
3030_2019.sas
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
/***************************************/
/* SAS Global Forum 2019 */
/* Data Quality Programming Techniques */
/* with SAS Cloud Analytic Services */
/* Paper SAS3030-2019 */
/* Nicolas Robert (SAS) */
/***************************************/
/* Provide more messages in the log */
options msglevel=i ;
/* Create a CAS session */
cas mySession sessopts=(messagelevel=all metrics=true) ;
/* Create a CASLIB */
caslib mydata datasource=(srctype=path) path="/gelcontent/demo/DM/data/SAMPLE" ;
/* Assign a CAS engine library */
libname mylib cas caslib="mydata" ;
/* Load the customers table from the DM caslib to the new dataproc caslib */
proc casutil ;
load casdata="customersSGF.sas7bdat" incaslib="mydata" outcaslib="mydata" casout="mycustomers" copies=0 replace ;
quit ;
/* Profile data using the profile CAS action */
proc cas ;
dataDiscovery.profile /
algorithm="PRIMARY"
table={caslib="mydata" name="mycustomers"}
columns={"id","name","address","city","state","zip","updatedate"}
cutoff=20
frequencies=10
outliers=5
casOut={caslib="mydata" name="mycustomers_profiled" replace=true replication=0} ;
quit ;
/* Profile data and Identity analysis using the profile CAS action */
proc cas ;
dataDiscovery.profile /
algorithm="PRIMARY"
table={caslib="mydata" name="mycustomers"}
multiIdentity=true
locale="ENUSA"
qkb="QKB CI 29"
identities= {
{pattern=".*", type="*", definition="Field Content", prefix="QKB_"}
}
cutoff=20
frequencies=10
outliers=5
casOut={caslib="mydata" name="mycustomers_profiled" replace=true replication=0} ;
quit ;
/* Fetch the output table in the Results and observe metric #1028 */
proc cas ;
table.fetch /
table={
caslib="mydata" name="mycustomers_profiled"} to=200 ;
quit ;
/* Short use case to understand multiIdentity */
data mylib.sample ;
length var $ 20 ;
var="New York" ; output ;
var="Washington" ; output ;
var="California" ; output ;
run ;
/* multiIdentity=true */
/* observe metric #1028 */
proc cas;
dataDiscovery.profile /
algorithm="PRIMARY"
table={caslib="mydata" name="sample"}
columns={"var"}
multiIdentity=true
locale="ENUSA"
qkb="QKB CI 29"
identities= {
{pattern=".*", type="*", definition="Field Content", prefix="QKB_"}
}
cutoff=20
frequencies=10
outliers=5
casOut={caslib="mydata" name="sample_profiled" replace=true replication=0} ;
table.fetch /
table={
caslib="mydata" name="sample_profiled"} to=200 ;
quit ;
/* multiIdentity=false */
/* observe metric #1028 */
proc cas;
dataDiscovery.profile /
algorithm="PRIMARY"
table={caslib="mydata" name="sample"}
columns={"var"}
multiIdentity=false
locale="ENUSA"
qkb="QKB CI 29"
identities= {
{pattern=".*", type="*", definition="Field Content", prefix="QKB_"}
}
cutoff=20
frequencies=10
outliers=5
casOut={caslib="mydata" name="sample_profiled" replace=true replication=0}
;
table.fetch /
table={
caslib="mydata" name="sample_profiled"} to=200 ;
quit ;
/* Transform, cleanse data using DQ data step functions */
data mylib.mycustomers_dq ;
length gender $1 mcName mcAddress parsedValue
tokenNames lastName firstName stateStd varchar(100) ;
set mylib.mycustomers ;
gender=dqGender(name,'Name','ENUSA') ;
mcName=dqMatch(name,'Name',95,'ENUSA') ;
mcAddress=dqMatch(address,'Address (Street Only)',95,'ENUSA') ;
parsedValue=dqParse(name,'Name','ENUSA') ;
tokenNames=dqParseInfoGet('Name','ENUSA') ;
if _n_=1 then put tokenNames= ;
lastName=dqParseTokenGet(parsedValue,'Family Name','Name','ENUSA') ;
firstName=dqParseTokenGet(parsedValue,'Given Name','Name','ENUSA') ;
stateStd=dqStandardize(state,'State/Province (Abbreviation)','ENUSA') ;
run ;
/* Cluster records with fuzzy and exact matching rules */
proc cas ;
entityRes.match /
clusterId="clusterID"
inTable={caslib="mydata",name="mycustomers_dq"}
columns={"id","firstName","lastName","gender","address","city","zip","stateStd","updateDate","mcName","mcAddress"}
matchRules={{
rule={{
columns={"mcName","mcAddress","stateStd"}
}}
}}
nullValuesMatch=false
emptyStringIsNull=true
outTable={caslib="mydata",name="mycustomers_clustered",replace=true} ;
quit ;
/* Output only multi-rows clusters */
data mylib.mycustomers_clustered_dups ;
set mylib.mycustomers_clustered ;
by clusterId ;
if first.clusterId and last.clusterId then delete ;
run ;
/* De-duplicate records using data step and a basic rule (most recent record) */
data mylib.mycustomers_dedup ;
set mylib.mycustomers_clustered ;
by clusterID updateDate ;
if last.clusterID then output ;
run ;
/* De-duplicate using the groubyinfo CAS action and a basic rule (first record) */
proc cas ;
simple.groupByInfo /
table={caslib="mydata",name="mycustomers_clustered",groupBy={"clusterID"}}
copyVars={"id","firstName","lastName","gender","address","city","zip","stateStd","updateDate"}
casOut={caslib="mydata",name="mycustomers_dedup",replace=true}
position=1
generatedColumns={"FREQUENCY","GROUPID","POSITION"}
details=true ;
quit ;
/* sample code to de-duplicate records using advanced rules */
/* This example is incomplete, all the fields are not populated */
data mylib.mycustomers_dedup ;
length new_id 8 new_firstName new_lastName varchar(36) new_gender $ 1
new_address varchar(92) new_city varchar(72) new_zip $ 10 new_state $ 2 ;
retain max_date new_id new_zip ;
set mylib.mycustomers_clustered ;
by clusterID ;
/* initialization */
if first.clusterID then do ;
max_date=updateDate ;
new_id=id ;
new_zip=zip ;
end ;
/* rule 1: get the customer id of the most recently updated record */
if updateDate>max_date then do ;
max_date=updateDate ;
new_id=id ;
end ;
/* rule 2: get a ZIP+4 when available */
if dqPattern(zip,'CHARACTER','ENUSA')="99999*9999" then do ;
new_zip=zip ;
end ;
/* output the "golden" record when the last cluster record is read */
if last.clusterId then output ;
run ;
/* Terminate the CAS session */
cas mysession terminate ;