Help using Base SAS procedures

Compress function for text variables

Reply
Frequent Contributor
Posts: 101

Compress function for text variables

I've a question about how SAS handles extra spaces in a variable. I was hoping to provide an example (below) with some of the data with 2 extra spaces before or after the text "abc" but somehow SAS strips the spaces off after reading in the data and becomes one value abc for all observations. But my question is quite simple. If I've data with spaces before or after some text for a variable, do I need to use the compress function to make sure I'm selecting only the text part?

If I've 5 observations with text = "abc",  "  abc",  "abc  ",  "  abc  ", or "abcdef", I don't care about the extra spaces, and I want all 4 cases with only abc in it (I don't want the "abcdef" case),

can I use just

   IF text='abc';

or I need to use the compression function:

  IF COMPRESS(text)='abc'

I read somewhere that SAS's default is compressing spaces with statements such as the following. Is it correct? If so, I can just use IF text='abc' as it is redundant to use IF COMPESS(text)='abc';

   if compress (var, '');

   new_variable=compress (variable, '');

Thanks,

-----------Example - though it seems SAS automatically compresses the data once reading in the data,

data aa; input text $ 1-10;

datalines;

abc

abc 

  abc

  abc  

;

run;

data aa1; set aa;

if text='abc' then v1=1;

proc print; run;

Respected Advisor
Posts: 3,124

Re: Compress function for text variables

Well, you didn't mention what if the blank is in the middle of your char value, such as: 'ab c', do you still want to remove it? if yes, compress() can do it for sure. if no, then use strip() to take care of heading or trailing blanks.

Haikuo

Frequent Contributor
Posts: 101

Re: Compress function for text variables

Thanks. So,

if I've values such as "ab c", then I should use compress()

if I want to distinguish "ab c" from "abc" (that is I only want "abc") then I use strip() to handle heading or trailing blanks. But can I NOT even use strip() for such data selection, using just if text="abc" to achieve the goal?

Super User
Posts: 5,085

Re: Compress function for text variables

Hai.kuo gave you the right answer.  Here's a little more detail.

You can use if text="abc" if you have trailing blanks, but not if you have leading blanks.  For leading blanks, you will need the strip function.  Your sample program does not give you the result that you think you are getting.  The INPUT instructions (input text $ 1-10Smiley Wink will left-hand justify the characters found in columns 1-10.  So you actually do not have any leading blanks once the INPUT statement has run.

Frequent Contributor
Posts: 101

Re: Compress function for text variables

Ok strip it is based on your expertise from all. Off the topic, how can I create a sample data with leading blanks to test on it? Indeed I noticed SAS would automatically left adjust. Is there a way for me to make SAS not to do it so that I have leading blank data to be able to compare using strip vs not using strip?

Thanks.

Super User
Posts: 5,085

Re: Compress function for text variables

Change the instructions on the INPUT statement:

input @1 text $char10.;

The $CHAR family of informats preserves the leading blanks it finds (and is also faster even if you have no leading blanks).

Contributor
Posts: 72

Re: Compress function for text variables

You stated "If I've data with spaces before or after some text for a variable, do I need to use the compress function to make sure I'm selecting only the text part?"

If that's the case and you don't care about spaces in between (i.e., you don't want the 'a bc'), you only want ' abc', 'abc ', 'abc' then you can just use the STRIP function.

IF STRIP(text)='abc';

Ask a Question
Discussion stats
  • 6 replies
  • 236 views
  • 0 likes
  • 4 in conversation