Removing Blanks or Other Characters from a String - SAS Programming

You may want to remove certain characters from a string (such as blanks) before you test, process, or store the string in a data set. A very useful function, COMPRESS, makes this job almost trivial.To demonstrate blank removal,suppose that you want to merge two files, keying on last names.Unfortunately, the data entry clerks were not consistent in their use of blanks. For example, you might have 'Mc Lauglin' in one file and 'McLauglin' in the other.Create a new MASTER file by removing all blanks from the variable LASTNAME.

Example

DATA NEWDATA;
SET MASTER;
LASTNAME=COMPRESS (LASTNAME);
RUNj

The first argument of the COMPRESS function is the variable to operate on.The second argument contains the characters to be compressed out of the string. Using the COMPRESS function with only a single argument removes all blanks (the default compressable character) from the first argument.If you want to remove characters other than blanks, you place all characters to be removed as the second argument in quotes. For example to remove parentheses, dashes,and blanks from telephone numbers you would write:

TELEPHON=COMPRESS (TELEPHON,' ()-');

If you like to live mildly dangerously, you could modify the values of LASTNAME in the original data set (MASTER) without having to create a new data set. By using the same data set name on the DATA and SET statements, you can accomplish this somewhat risky operation:

DATA MASTER;
SET MASTER;
LASTNAME=COMPRESS (LASTNAME);
RUN;

Remember that recovery from such a program may be difficult or impossible.A related function, COMPBL,converts occurrences of multiple contiguous blanks into single blanks.If a variable ADDRESS contained the value '123 SESAME ST.

KIDSTOWN, NY 10101', the code COMPBL (ADDRESS); would yield, '123 SESAME ST. KIDSTOWN, NY 10101' with one blank between each set of non-blank characters.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics