-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtextid.sas
116 lines (95 loc) · 4.17 KB
/
textid.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
%macro TextID(in,var,id=ID,out=,test=N) / des='Create a numeric ID for a text field that will always be the same';
/********************************************************************************
BEGIN MACRO HEADER
********************************************************************************
Name: TextID
Author: Chris Swenson
Created: 2011-10-17
Purpose: Convert a text field to an ID that will be the same when re-run.
The macro converts each part of the field into its binary
representation and then sums up each character.
Arguments: in - input data set
var - variable to assign an ID to
id - ID variable name, defaulted to ID
out= - output data set, defaulted to the input data set if blank
test= - Y/N flag to indicate whether to run the macro in test mode
Revisions
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Date Author Comments
¯¯¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯
2011-12-05 CAS Corrected some issues during test mode.
YYYY-MM-DD III Please use this format and insert new entries above
********************************************************************************
END MACRO HEADER
********************************************************************************/
/* Check for blank arguments */
%if %superq(IN)=%str() %then %do;
%put %str(E)RROR: No argument specified for IN.;
%return;
%end;
%if %superq(VAR)=%str() %then %do;
%put %str(E)RROR: No argument specified for VAR.;
%return;
%end;
/* Check for argument values in (Y N) */
%let TEST=%substr(%upcase(&TEST), 1, 1);
%if %index(*Y*N*,*&TEST*)=0 %then %do;
%put %str(E)RROR: %str(I)nvalid argument specified for TEST.;
%put %str(E)RROR- Please use one of the following: Y or N.;
%return;
%end;
/* Set the default for the output variable */
%if "&OUT"="" %then %do;
%if &TEST=N %then %let out=∈
%else %let out=&IN._test;
%end;
/* Set up variable to check for numeric values */
%local num;
%let num=N;
/* Convert the text to an ID */
data &OUT(drop=_i_
%if &TEST=Y %then %do;
step_:
%end;
)
%if &TEST=Y %then %do;
test(keep=&VAR &ID step_:)
%end;
;
format &ID 8.;
set ∈
if compress(&VAR, '', 'kd') ne ''
then call symputx('num', 'Y');
/* Steps in creating master ID */
/* 1. substr(VAR, _i_) extracts the first character */
/* 2. put(..., $binary8.) converts the character to binary */
/* 3. input(..., binary8.) converts the binary to numeric */
/* 4. ID + ... adds the numeric value to the master code */
/* 5. The process is repeated for each character, adding each value to the */
/* previous, creating a unique master ID */
&ID=.;
do _i_=1 to length(&VAR);
&ID + input(put(substr(&VAR, _i_, 1), $binary8.), binary8.);
%if &TEST=Y %then %do;
step_1=substr(&VAR, _i_, 1);
step_2=put(substr(&VAR, _i_), $binary8.);
step_3=input(put(substr(&VAR, _i_), $binary8.), binary8.);
step_4=sum(step_4, input(put(substr(&VAR, _i_, 1), $binary8.), binary8.));
retain step_4;
label step_1='Step 1: Extract 1 character'
step_2='Step 2: Convert character to binary'
step_3='Step 3: Convert binary to numeric'
step_4='Step 4: Add the numeric value to the total'
;
output test;
%end;
end;
%if &TEST=Y %then %do;
output &OUT;
%end;
run;
%if &NUM=Y %then %do;
%put %str(W)ARNING: The input variable (&VAR) contains numeric values.;
%put %str(W)ARNING- It is possible the output ID (&ID) may not be distinct.;
%end;
%mend TextID;