DATA Step, Macro, Functions and more

encryption of primary key using replace function - how about white spaces?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

encryption of primary key using replace function - how about white spaces?

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


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Contributor dkb
Contributor
Posts: 53

Re: encryption of primary key using replace function - how about white spaces?

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;

View solution in original post


All Replies
Solution
‎09-25-2015 06:23 AM
Contributor dkb
Contributor
Posts: 53

Re: encryption of primary key using replace function - how about white spaces?

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;

Contributor hbi
Contributor
Posts: 66

Re: encryption of primary key using replace function - how about white spaces?

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;

 

Trusted Advisor
Posts: 2,113

Re: encryption of primary key using replace function - how about white spaces?

Character substitution is incredibly easy to break, so don't use this for a public space.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 252 views
  • 2 likes
  • 4 in conversation