Finally, options on output data sets are evaluated left to right within the DATA
statement. DROP= and KEEP= options are applied before the RENAME= option.
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
Encrypting Variable Values 55
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.
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

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.