Macros for SAS Application Developers
https://github.com/sasjs/core
mp_md5.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Generates an md5 expression for hashing a set of variables
4 @details This is the same algorithm used to hash records in
5 [Data Controller for SAS](https://datacontroller.io) (free for up
6 to 5 users).
7
8 It is not designed to be efficient - it is designed to be effective,
9 given the range of edge cases (large floating points, special missing
10 numerics, thousands of columns, very wide columns).
11
12 It can be used only in data step, eg as follows:
13
14 data _null_;
15 set sashelp.class;
16 hashvar=%mp_md5(cvars=name sex, nvars=age height weight);
17 put hashvar=;
18 run;
19
20 Unfortunately it will not run in SQL - it fails with the following message:
21
22 > The width value for HEX is out of bounds. It should be between 1 and 16
23
24 The macro will also cause errors if the data contains (non-special) missings
25 and the (undocumented) `options dsoptions=nonote2err;` is in effect.
26
27 This can be avoided in two ways:
28
29 @li Global option: `options dsoptions=nonote2err;`
30 @li Data step option: `data YOURLIB.YOURDATASET /nonote2err;`
31
32 @param cvars= Space seperated list of character variables
33 @param nvars= Space seperated list of numeric variables
34
35 <h4> Related Programs </h4>
36 @li mp_init.sas
37
38 @version 9.2
39 @author Allan Bowe
40**/
41
42%macro mp_md5(cvars=,nvars=);
43%local i var sep;
44put(md5(
45 %do i=1 %to %sysfunc(countw(&cvars));
46 %let var=%scan(&cvars,&i,%str( ));
47 &sep put(md5(trim(&var)),$hex32.)
48 %let sep=!!;
49 %end;
50 %do i=1 %to %sysfunc(countw(&nvars));
51 %let var=%scan(&nvars,&i,%str( ));
52 /* multiply by 1 to strip precision errors (eg 0 != 0) */
53 /* but ONLY if not missing, else will lose any special missing values */
54 &sep put(md5(trim(put(ifn(missing(&var),&var,&var*1),binary64.))),$hex32.)
55 %let sep=!!;
56 %end;
57),$hex32.)
58%mend mp_md5;