I create a hash for my ID variable with sha256 and apply the $hex64. format for readability. But when the ID is missing then it gets a hash value of 20202020... The receiver of my data will only receive the hash_ID, and I want to prevent they assume this ID was known and can be found by me,or that everyone with this hash is the same ID. Is there a way to get these missings to be displayed as missing also in the $hex64. format?
data test;
input file ID $5.;
datalines;
1 02156
2 00369
3 45896
4
5 78954
6
7 78954
;
run;
proc sql;
create table hash as
select ID,
(case when ID ne "" then sha256(cat("XXX",ID)) else "" end) as hash_ID format = $hex64.
from test;
quit;
A good case for creating your own format, slightly different from hex
data test;
input file ID $5.;
datalines;
1 02156
2 00369
3 45896
4
5 78954
6
7 78954
;
run;
proc format;
value $ myhex
' ' = ' '
other = [$hex64.]
;
run;
proc sql;
create table hash as
select ID,
(case when ID ne "" then sha256(cat("XXX",ID)) else "" end) as hash_ID format = $myhex.
from test;
quit;
Result:
ID hash_ID 02156 6098C1ED4949D1E7A4EA72C986640C40735CD5D1F201E9FC5DEE94435DE676F6 00369 C79B09E1F340DCCC54FD1816BB9CD4A21C2AD7795674E5156784368C2EFB1A40 45896 563FF6307A5DA67B9BBB3D383F6A12BCBDC54AEA03BFFB3358FDC1E6A75DDF6B 78954 F47211D85F84F5E03620727912C4382D601B064D52F3A8D2D38EDFD9B0DE5D7F 78954 F47211D85F84F5E03620727912C4382D601B064D52F3A8D2D38EDFD9B0DE5D7F
A good case for creating your own format, slightly different from hex
data test;
input file ID $5.;
datalines;
1 02156
2 00369
3 45896
4
5 78954
6
7 78954
;
run;
proc format;
value $ myhex
' ' = ' '
other = [$hex64.]
;
run;
proc sql;
create table hash as
select ID,
(case when ID ne "" then sha256(cat("XXX",ID)) else "" end) as hash_ID format = $myhex.
from test;
quit;
Result:
ID hash_ID 02156 6098C1ED4949D1E7A4EA72C986640C40735CD5D1F201E9FC5DEE94435DE676F6 00369 C79B09E1F340DCCC54FD1816BB9CD4A21C2AD7795674E5156784368C2EFB1A40 45896 563FF6307A5DA67B9BBB3D383F6A12BCBDC54AEA03BFFB3358FDC1E6A75DDF6B 78954 F47211D85F84F5E03620727912C4382D601B064D52F3A8D2D38EDFD9B0DE5D7F 78954 F47211D85F84F5E03620727912C4382D601B064D52F3A8D2D38EDFD9B0DE5D7F
Do this:
case
when ID ne ""
then put(sha256(cat("XXX",ID)),$hex64.)
else ""
end as hash_ID
or, in a data step
data hash;
set test;
length hash_id $64;
if id > "" then hash_id = put(sha256(cat("XXX",ID)),$hex64.);
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.