The PUT function converts the numeric value to a character value.
The first DO loop uses the SUBSTR function to split the value into five separate values
and assigns each to the old ARRAY. The second DO loop translates each value by using
the INDEXC function to find the original number in the from ARRAY and, if found,
translates the value using the
from ARRAY, and rotates through the list of elements
every third time. The encrypted value is created by using the CATS function to
concatenate the five translated values.
If you compare the two DATA steps in the example below, you can see that the values in
the to and from arrays are reversed. This is because the second DATA step reverses the
encryption done in the first DATA step, converting the values back to their original
values.
The same process that is used to encrypt the values is also used to decrypt the values.
The only differences are that the encrypted variable is passed to the SUBSTR function,
and the final decrypted variable is passed to the INPUT function following the CATS
function. This is done so that the final values are numeric values.
Example Code 4.5 Using Different Functions to Encrypt Numeric Values into Character
Strings
data sample3;
input num;
array from(3) $ 10 from1-from3 ('0123456789','0123456789','0123456789');
array to(3) $ 10 to1-to3 ('ABCDEFGHIJ','KLMNOPQRST','UVWXYZABCD');
array old(5) $ old1-old5;
array new(5) $ new1-new5;
char_num=put(num,5.);
do i = 1 to 5;
old(i)=substr(char_num,i,1);
end;
j=1;
do k = 1 to 5;
if indexc(old(k),from(j)) > 0 then do;
new(k)=translate(old(k),to(j),from(j));
j+1;
if j=4 then j=1;
end;
end;
encrypt_num=cats(of new1-new5);
keep num encrypt_num;
datalines;
12345
70707
99
1111
;
run;
data sample3;
set sample3;
array to(3) $ 10 to1-to3 ('0123456789','0123456789','0123456789');
array from(3) $ 10 from1-from3 ('ABCDEFGHIJ','KLMNOPQRST','UVWXYZABCD');
array old(5) $ old1-old5;
array new(5) $ new1-new5;
do i = 1 to 5;
old(i)=substr(encrypt_num,i,1);
Encrypting Variable Values 59