How to encrypt group ID with values from a dataset

Reply
New Contributor
Posts: 3

How to encrypt group ID with values from a dataset

Hi,
I have a task of encrypting group IDs using random values - either alphabet or number. I've created a format to map each of the 26 alphabets to a two digit random number, and each single digit number (0-9) to a two digit alpha-numeric random value, such that none of the values appear more than once.
Now, the question is, how do I change the 9 digits of group ID to 18 encrypted digits based on the format values?

e.g. proc format ;
value $xwalk
'A' = '10'
'B' = '19'
'C' = '26'
'D' = '18'
'E' = '35'
'1' = 'A0'
'2' = 'B3'
'3' = 'E8'
'4' = 'Q9'
'5' = 'I2'
;

In this case, if my group ID is 'AD245', it should become '1018B3Q9I2' after encryption.

I've tried using tranwrd function with do loop, but that doesn't allow me to read crosswalk values from a dataset or format.

Please suggest what code I can use to get there.

Thanks!
Grand Advisor
Posts: 16,334

Re: How to encrypt group ID with values from a dataset

Look at the put function...
You need to build in the loop and the code to concatenate the strings together rather than output like I did, but hopefully you get the idea.

You can create a proc format from a dataset with cntlin option if you need to.

HTH,
Reeza


ie.
data want;
groupid='AD245';

length new_j $2. ;
do i=1 to 5;
j=substr(groupid, i,1);
new_j=compress(put(j, $xwalk.));
output;

end;


run;
New Contributor
Posts: 3

Re: How to encrypt group ID with values from a dataset

Thanks Reeza!
This makes a lot of sense. In fact, I'd tried something similar though not same, trying to use put function for using user defined format. However, it is the part where I need to concatenate all substrings together that is throwing me off. Coz even if I use lag function to retain the last value of j or new_j, it will still give value for only last 2 characters, and not a combined string of all previous characters.

Any suggestions on that one, please?

Thanks again.
Super Contributor
Super Contributor
Posts: 3,174

Re: How to encrypt group ID with values from a dataset

Suggest sharing your SAS code that is not working. Also, you likely need to look at using both a LENGTH and a RETAIN statement -- mostly the RETAIN is needed if your DATA step iterates (bottom back to the top) instead of having a DO/END loop with a SET / INPUT statement code-paragraph for data manipulation.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

data step retain statement site:sas.com

retain statement documentation site:sas.com
Grand Advisor
Posts: 16,334

Re: How to encrypt group ID with values from a dataset

If your ID's are always the same length, ie 9 as in your description the following works. If not then you'll need a different solution, using retain and length statement,where you basically append the new read value onto the string after encoding it.


data want;
groupid='AD245CD32';

length code: $2. j: $1.;

array code(9) $ code1-code9;
array temp(9) $ j1-j9;


do i=1 to 9;
temp=substr(groupid, i, 1);
code=put(temp, $xwalk.);


end;
new_code=cats(of code1-code9);

run;
New Contributor
Posts: 3

Re: How to encrypt group ID with values from a dataset

Yes, my IDs are always of the same length, and this solution worked! Thanks so much for your help!

I used the following code, only thing different was, instead of cats function, I used concatenation with array values within the do loop -

data want;
groupid='AD245CD32';

length code: $2. j: $1. new_code $9.;
retain new_code '';

array code(9) $ code1-code9;
array temp(9) $ j1-j9;

do i=1 to 9;
temp(i)=substr(groupid, i, 1);
code(i)=put(temp(i), $xwalk.);

new_code=compress(new_code || code(i));

end;

run;
Post a Question
Discussion Stats
  • 5 replies
  • 559 views
  • 0 likes
  • 3 in conversation