BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
willy06251
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
dkb
Quartz | Level 8 dkb
Quartz | Level 8

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

3 REPLIES 3
dkb
Quartz | Level 8 dkb
Quartz | Level 8

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;

hbi
Quartz | Level 8 hbi
Quartz | Level 8

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;

 

Doc_Duke
Rhodochrosite | Level 12
Character substitution is incredibly easy to break, so don't use this for a public space.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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