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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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