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!
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.
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.
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).
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.