Hi all.
So i got to know how to mask string-values primary keys using replace function as below:
data test;
format e_STATSLINKAGE $8.;
STATSLINKAGE="ABCDE FG";
do i = 1 to length(STATSLINKAGE);
e_STATSLINKAGE=e_STATSLINKAGE||translate(substr(STATSLINKAGE,i,1),'HIJKLMNOPQRSTUVWXYZABCDEFG4567890123 ','ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ');
end;
run;
It works really well...but until I came across a white space showing up inside these keys.
At the moment, the code strips any leading or trailing blanks as it genereates the encrpyted variable thus the white space gets stripped. But I want to keep the white space as it is.
How would you achieve this without expanding the current code too much?
SAS experts, please advise
Thanks
You could use SUBSTR to overwrite one character at a time:
data test;
format e_STATSLINKAGE $8.;
STATSLINKAGE="ABCDE FG";
do i = 1 to length(STATSLINKAGE);
substr(e_STATSLINKAGE,i,1)=translate(substr(STATSLINKAGE,i,1),
'HIJKLMNOPQRSTUVWXYZABCDEFG4567890123 ',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ');
end;
run;
You could use SUBSTR to overwrite one character at a time:
data test;
format e_STATSLINKAGE $8.;
STATSLINKAGE="ABCDE FG";
do i = 1 to length(STATSLINKAGE);
substr(e_STATSLINKAGE,i,1)=translate(substr(STATSLINKAGE,i,1),
'HIJKLMNOPQRSTUVWXYZABCDEFG4567890123 ',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ');
end;
run;
I do not think the "do" loop is necessary. I have changed your example slightly to replace " " with "_" which will avoid the leading space and trailing space problem.
DATA test;
format e_STATSLINKAGE $8.;
STATSLINKAGE="ABCDE FG";
if index(STATSLINKAGE, "_") > 0 then do;
put "ERROR: sorry ... bad replacement character found in input data";
abort;
end;
/* change space to underscore */
e_STATSLINKAGE=translate(STATSLINKAGE,
'HIJKLMNOPQRSTUVWXYZABCDEFG4567890123_',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ');
RUN;
There is another way. If you do not have a need to ever decrypt" the data back to its original value, you could do an MD5 hash.
DATA test2;
fake_ssn = "000000000"; output;
fake_ssn = "111111111"; output;
fake_ssn = "999999999"; output;
RUN;
/* note that if you change your primary key to an MD5, it is a one-way
hash, and there is no way to decrypt it and convert it back to its
original value */
DATA test2_hashed;
SET test2;
length ssn_hashed $40;
ssn_hashed = put(md5(fake_ssn),$hex32.);
RUN;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.