DATA Step, Macro, Functions and more

Finding exact value of a character variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Finding exact value of a character variable

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

VirginiaYes
GeorgiaYes
South Carolina.

 

 


Accepted Solutions
Solution
‎04-27-2016 12:38 PM
Trusted Advisor
Posts: 1,117

Re: Finding exact value of a character variable

[ Edited ]

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)

View solution in original post


All Replies
Super User
Super User
Posts: 7,948

Re: Finding exact value of a character variable

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.  

Occasional Contributor
Posts: 7

Re: Finding exact value of a character variable

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.

Super User
Posts: 19,789

Re: Finding exact value of a character variable

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 @FreelanceReinhard 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. 

Solution
‎04-27-2016 12:38 PM
Trusted Advisor
Posts: 1,117

Re: Finding exact value of a character variable

[ Edited ]

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)

Super User
Posts: 19,789

Re: Finding exact value of a character variable

Posted in reply to FreelanceReinhard

@FreelanceReinhard were your ears burning Smiley Wink

Super User
Posts: 5,503

Re: Finding exact value of a character variable

[ Edited ]

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.

Super Contributor
Posts: 308

Re: Finding exact value of a character variable

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;

Super User
Posts: 19,789

Re: Finding exact value of a character variable

Although both @loko & 

@FreelanceReinhard 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 Smiley Sad

Occasional Contributor
Posts: 7

Re: Finding exact value of a character variable

@Loko @Reeza @FreelanceReinhard 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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 387 views
  • 3 likes
  • 6 in conversation