BookmarkSubscribeRSS Feed
Solph
Pyrite | Level 9

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;

6 REPLIES 6
Haikuo
Onyx | Level 15

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

Solph
Pyrite | Level 9

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?

Astounding
PROC Star

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-10;) 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.

Solph
Pyrite | Level 9

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.

Astounding
PROC Star

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).

robby_beum
Quartz | Level 8

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';

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1028 views
  • 0 likes
  • 4 in conversation