Examples of Dropping, Keeping, and Renaming Variables
The following examples show specific ways to handle dropping, keeping, and renaming
variables:
This example uses the DROP= and RENAME= data set options and the INPUT
function to convert the variable PopRank from character to numeric. The name
PopRank is changed to TempVar before processing so that a new variable PopRank
can be written to the output data set. Note that the variable TempVar is dropped from
the output data set and that the new name TempVar is used in the program
statements.
data newstate(drop=tempvar);
length poprank 8;
set state(rename=(poprank=tempvar));
poprank=input(tempvar,8.);
run;
This example uses the DROP statement and the DROP= data set option to control the
output of variables to two new SAS data sets. The DROP statement applies to both
data sets, Corn and Bean. You must use the RENAME= data set option to rename the
output variables BeanWt and CornWt in each data set.
data corn(rename=(cornwt=yield) drop=beanwt)
bean(rename=(beanwt=yield) drop=cornwt);
set harvest;
if crop='corn' then output corn;
else if crop='bean' then output bean;
drop crop;
run;
This example shows how to use data set options in the DATA statement and the
RENAME statement together. Note that the new name QTRTOT is used in the
DROP= data set option.
data qtr1 qtr2 ytd(drop=qtrtot);
set ytdsales;
if qtr=1 then output qtr1;
else if qtr=2 then output qtr2;
else output ytd;
rename total=qtrtot;
run;
Encrypting Variable Values
Customized Encryption and Decryption Algorithms for SAS
Variables
SAS provides encryption for SAS data sets with the ENCRYPT= data set option, but this
option is typically used to encrypt data at the data set level. To encrypt data at the SAS
variable level, you can use a combination of DATA step functions and logic to create
your own encryption and decryption algorithms. However, if you create your own
algorithms, it is important that you create a program that not only is secure and hidden
from public view, but that also contains methods to both encrypt and decrypt the data.
Encrypting Variable Values 55
This section provides sample programs that use the DATA step with different functions
and methods to encrypt and decrypt variables.
Example 1: A Simple 1-Byte-to-1-Byte Swap Using the TRANSLATE
Function
The first sample program shows how to use a simple 1-byte-to-1-byte swap with the
TRANSLATE function. Because this method is not complicated, you might assume that
it is not as secure. However, because you are designing the pattern of characters, special
characters, or values for the TRANSLATE arguments from and to, you are creating
your own unique encryption algorithm.
The values that are listed for both the TRANSLATE from and the TRANSLATE to
arguments do not have to be in any sequential, alphabetical, or numerical order.
In the following sample code, the DATA step reads a name that is 8 or fewer characters
in length and uses a DO loop to process the TRANSLATE function and SUBSTR
function 1 byte at a time.
The first DO loop creates the encrypted value and the second DO loop creates the
decrypted value by reversing the order and returning the original value.
Example Code 4.3 A Simple 1-Byte-to-1-Byte Swap Using the TRANSLATE Function
data sample1;
input @1 name $;
length encrypt decrypt $ 8;
/*ENCRYPT*/
do i = 1 to 8;
encrypt=strip(encrypt)||translate(substr(name,i,1),
'0123456789!@#$%^&*()-=,./?<','ABCDEFGHIJKLMNOPQRSTUVWXYZ');
end;
/*DECRYPT*/
do j = 1 to 8;
decrypt=strip(decrypt)||translate(substr(encrypt,j,1),
'ABCDEFGHIJKLMNOPQRSTUVWXYZ','0123456789!@#$%^&*()-=,./?<');
end;
drop i j;
datalines;
ROBERT
JOHN
GREG
;
proc print;
run;
The following output shows the results of the PROC PRINT for Example 1:
56 Chapter 4 SAS Variables
Example 2: Using a 1-Byte-to-2-Byte Swap with the TRANWRD
Function
This sample program shows how to encrypt values using a 1-byte-to-2-byte swap with
the TRANWRD function. Each 1-byte character is replaced with a 2-digit number. To
change values that go from 1-byte to many bytes, or many bytes to 1 byte, you need to
use the TRANWRD function and you must assign the resulting variable the same name
as the variable that is being translated. The values that are listed for the from values do
not have to be in any special order.
In the following sample code, the DATA step reads an ID that is 6 or fewer characters in
length. However, the variable is assigned a length of 12 to double the character length,
because this is a 1-byte-to-2-byte exchange. A DO loop processes the TRANWRD
function 1 byte at a time.
The values that are being changed are the letters A, B, C, D, E, and F, but they are listed
in a random order as the from_1 value. The to_1 value is assigned a starting value of
21. However, this can be any other 2-digit number, such as 11 or 38, as long as the last
value that is assigned does not go over 99, which then turns into a 3-digit number.
The first DO loop creates the encrypted value and the second DO loop creates the
decrypted value by reversing the order and returning the original value. New variables
are assigned to the ID variable before the TRANWRD function to avoid overwriting the
original ID variable.
Example Code 4.4 Using a 1-Byte-to-2-Byte Swap with the TRANWRD Function
data sample2;
input @1 id $12.;
/*ENCRYPT*/
encrypt=id;
i=21;
do from_1 = "C","F","E","A","D","B";
to_1=put(i,2.);
encrypt=tranwrd(encrypt,from_1,to_1);
i+1;
end;
/*DECRYPT*/
decrypt=encrypt;
j=21;
do to_2 = "C","F","E","A","D","B";
from_2=put(j,2.);
decrypt=tranwrd(decrypt,from_2,to_2);
j+1;
Encrypting Variable Values 57
end;
drop i j to_1 from_1 to_2 from_2;
datalines;
ABCDEF
FEDC
ACE
BDFA
CAFDEB
BADCF
ABC
;
proc print;
run;
The following output shows the results of the PROC PRINT for Example 2:
Example 3: Using Different Functions to Encrypt Numeric Values as
Character Strings
This sample program shows you how to encrypt a numeric value to create a character
value using a different character every third time. This method uses the PUT, SUBSTR,
INDEXC, TRANSLATE, CATS, and INPUT functions, as well as array processing.
The program uses two DATA steps: one to encrypt the values and the other to reverse the
process and decrypt the values. You can merge the encrypt and decrypt DATA steps into
a single DATA step, if needed.
The first DATA step reads numeric values that are 5 digits or fewer. The numeric
variable is converted to a character variable and is split into five separate values.
Four ARRAY statements are used: the first array sets up the from values; the second
sets up the
to values; the third holds the five separate numeric values; and the fourth
holds the five new, separate encrypted values.
The from and to arrays are each created with three elements. The from ARRAY is
assigned the same string of numbers for all three elements, and the
to ARRAY is
assigned a different string of letters for each of the three elements to build the every-
third-time rotating pattern.
58 Chapter 4 SAS Variables
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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.93