AFHood Group Blog The thoughtless yammerings of marketing junkies..

13May/0910

How to remove characters with SAS COMPRESS

Compress is a string function in SAS. This function doesnt make your characters smaller but it does take some of them out. Here is the basic example.

data _null_;

string=Here is a string;

new_str=compress(string,'e'); /*now new_str= Hr is a string */

run;

Pretty simple huh? But wait, in SAS v9 there are new features. A third function argument even. Example? Sure:

data _null_;

string=Here is a string;

new_str= compress(string,'eia',"k"); /* now new_str=eeiai */

run;

This third argument breaks down like so:

a - all upper and lowercase letters

d - all numeric digits

i - ignores case

k - keeps the listed characters instead of removing them

s - adds all blank space to the list (like tabs, spaces, carriage returns)

p - adds all punctuation to the list

u - adds all uppercase letters

l - adds all lowercase letters

Suddenly this function is more useful than ever.

If you have some SAS code you would like help on, we are always available for short and long term consulting jobs. No project is too large or small.

Comments (10) Trackbacks (0)
  1. This is really excellent..helped very much..please keep it up !!!

  2. Awesome Stuff!

  3. Extremely Important!

  4. Would it also be possible to remove ‘is’ from the string without removing the ‘i’ ‘string’?

    Best regards
    Kim

  5. Yes it is possible, but not by using compress without other logic. The compress function alone doesn’t support what you describe.

  6. This was very very helpful. Thanks

  7. Wonderful! It is really help sas users

  8. Hello,
    I have a problem by creating a table that consists nb day month year
    I was able to view all, but in column nb, I have the missing values.
    I know that the problem is about my database.This is because of space between numbers.
    Here are my database
    24/11/09, 878
    25/11/09, 1 * 143
    26/11/09, 1 * 124
    27/11/09 934
    28/11/09, 477
    29/11/09, 639
    30/11/09, 908
    So how could we use COMPRESS function in this case?
    Thanks you for your help!

  9. suppose my string as like this stirng= hi how are you & remove the ampresend in my string.
    which function is best

  10. i have solved my problem


Leave a comment

No trackbacks yet.