The SAS Output Delivery System and reporting techniques

Strip Function Results

Accepted Solution Solved
Reply
Occasional Contributor bzh
Occasional Contributor
Posts: 7
Accepted Solution

Strip Function Results

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!


Accepted Solutions
Solution
‎12-21-2015 11:44 PM
Respected Advisor
Posts: 4,649

Re: Strip Function Results

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


All Replies
Solution
‎12-21-2015 11:44 PM
Respected Advisor
Posts: 4,649

Re: Strip Function Results

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.

Occasional Contributor bzh
Occasional Contributor
Posts: 7

Re: Strip Function Results

[ Edited ]

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

Respected Advisor
Posts: 4,649

Re: Strip Function Results

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.

Grand Advisor
Posts: 16,419

Re: Strip Function Results

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
Post a Question
Discussion Stats
  • 4 replies
  • 465 views
  • 0 likes
  • 3 in conversation