BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bzh
Fluorite | Level 6 bzh
Fluorite | Level 6

In the SAS 9.4 documentation for Strip, under Details, I noticed this sentence:

 

If the value that is trimmed is shorter than the length of the receiving variable, SAS pads the value with new trailing blanks.

 

Can someone show me this in action?  I would like to avoid this since it seems to go against what the Strip function is for.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

A simple example:

 

if gender='F' then desc = 'Female';

else desc='Male';

desc = strip(desc);

 

The first statement defines DESC as being 6 characters long.  If the last statement encounters "Male", that won't change the length of DESC.  It will contain 6 characters:  "Male" plus two trailing blanks.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

A simple example:

 

if gender='F' then desc = 'Female';

else desc='Male';

desc = strip(desc);

 

The first statement defines DESC as being 6 characters long.  If the last statement encounters "Male", that won't change the length of DESC.  It will contain 6 characters:  "Male" plus two trailing blanks.

bzh
Fluorite | Level 6 bzh
Fluorite | Level 6

Astounding - in your example, if I were to make a Where clause for Male, would it be

 

where name='Male'

OR

where name='Male  '

 

I may be confused on whether or not these trailing blanks just take up storage or if they affect functionality.  If one has a varchar type column with strings of varying length, it sounds like the shorter values will always have trailing blanks in a SAS data set because the Length must be set on the longest value (to avoid truncation).

Astounding
PROC Star

You could use either.  The software recognizes when it is comparing strings of unequal length.  Without changing any of the values of your variables, it will automatically pad the shorter string with blanks as needed in order to make the comparison.  Conveniently, you don't have to add the blanks but you will get the same results.

Reeza
Super User

It depends on what you're trying to do...certain things exclude trailing blanks by default, the length function is a good example. Note the differences between the length and lengthc variable.

 

data have;
length var1 $12. var2 $8.;

var2='Cat';
var2_lengthc=lengthc(var2);
var2_length=length(var2);
var1=strip(var2);
var1_lengthc=lengthc(var1);
var1_length=length(var1);
run;

proc print data=have;run;

Results:

 

                              var2_      var2_     var1_      var1_
                                 Obs    var1    var2    lengthc    length    lengthc    length

                                  1     Cat     Cat        8          3         12         3

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 1572 views
  • 0 likes
  • 3 in conversation