BookmarkSubscribeRSS Feed
SAScert
Calcite | Level 5
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!
5 REPLIES 5
Reeza
Super User
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;
SAScert
Calcite | Level 5
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
Reeza
Super User
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;
SAScert
Calcite | Level 5
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 5 replies
  • 1608 views
  • 0 likes
  • 3 in conversation