Hello,
I have found on occasional problem that I run into in SAS that I can't quite figure out. Is there a way to get the exact reponse to a character variable in SAS? I usually use proc freq, but sometimes this does not work. I suspect it has to do with leading or trailing blanks. I could use a SAS character function to supress trailing or leading blanks, but is there a way to actually see where those blanks are? Or is there another reason why this might occur? Any insight would be much appreciated!
if state in ( "Virginia", "Georgia", "South Carolina" ) then south = "Yes";
state south
Virginia | Yes |
Georgia | Yes |
South Carolina | . |
Hi @Noob,
Display the character variable in question with the $HEXw. format, where width w is at least twice the length of the string contained in the character variable (or equal to twice the defined length of the character variable, to see the full picture). The result might look cryptic, but it tells you exactly what's in the character variable. You can see leading and trailing blanks, non-standard blanks, characters which would otherwise be invisible or hard to distinguish from one another (e.g. hyphen vs. dash) -- all in two-digit hexadecimal codes (which you can look up in an ASCII or similar table).
Example:
data have;
c=cats('ABC','A0'x,'DEF');
d='ABC DEF';
proc print;
run; /* The two strings look perfectly equal. */
data test;
set have;
if c=d then put 'equal'; /* This "equal" will not appear! */
put c $hex14.;
put d $hex14.; /* Here you see why. */
run;
Edit: When copying non-breakable spaces from a post into SAS they seem to be replaced with ordinary spaces. The edited example above does not suffer from this issue.
(Edit 2: improved wording)
I would always use:
if strip(state) in (...;
Or in fact, I would upper case them so as to avoid differences in case:
if upcase(strip(state)) in (...;
Generally speaking you should really know your data, so stripping off leading and training blanks should not be a problem.
Thanks RW,
Is there a way to see the actual value as it is entered? While the strip function will often solve the problem, it still doesn't give me the exact response. I will still never know if the value is "South Carolina " or " South Carolina" or something else entirely.
The typical way to see if you have leading or trailing spaces is to display the value with a hex format.
'09'x are tabs and there are some other characters that are spaces. I can't remember all of them. Hopefully someone like @FreelanceReinh can explain it better than I can. I know it exists but not how to actually use it and don't have time to mock up an example now.
Hi @Noob,
Display the character variable in question with the $HEXw. format, where width w is at least twice the length of the string contained in the character variable (or equal to twice the defined length of the character variable, to see the full picture). The result might look cryptic, but it tells you exactly what's in the character variable. You can see leading and trailing blanks, non-standard blanks, characters which would otherwise be invisible or hard to distinguish from one another (e.g. hyphen vs. dash) -- all in two-digit hexadecimal codes (which you can look up in an ASCII or similar table).
Example:
data have;
c=cats('ABC','A0'x,'DEF');
d='ABC DEF';
proc print;
run; /* The two strings look perfectly equal. */
data test;
set have;
if c=d then put 'equal'; /* This "equal" will not appear! */
put c $hex14.;
put d $hex14.; /* Here you see why. */
run;
Edit: When copying non-breakable spaces from a post into SAS they seem to be replaced with ordinary spaces. The edited example above does not suffer from this issue.
(Edit 2: improved wording)
@FreelanceReinh were your ears burning 😉
Trailing blanks won't cause a problem, but leading blanks could. Try checking what is actually there:
proc freq;
tables state;
format state $char25,.;
run;
The FORMAT statement will preserve any leading blanks in the table.
As far as I know in operator excludes trimming blanks from comparison therefore i believe you have other characters within South Carolina value.
Consider the following example:
data _null_;
a='a ';/*you have a tab here*/
if a in ('a') then put '1 ' a=;
a='a ';/*you have a space here*/
if a in ('a') then put '2 ' a=;
a='a';
if a in ('a') then put '3 ' a=;
run;
Although both @loko &
@FreelanceReinh examples are correct, it seems the browser changes them all to the same thing so it doesn't work as expected if you copy the code and try and run it 😞
@Loko @Reeza @FreelanceReinh Thank you all very much!
The issue in my particular case was not with leading or trailing blanks but with an extra hidden space in between 'South' and 'Carolina'. Your examples were enlightening and should help me again in the future.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: