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