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

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

Noob
Fluorite | Level 6

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.

Reeza
Super User

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. 

FreelanceReinh
Jade | Level 19

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)

Reeza
Super User

@FreelanceReinh were your ears burning 😉

Astounding
PROC Star

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.

Loko
Barite | Level 11

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;

Reeza
Super User

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 😞

Noob
Fluorite | Level 6

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 9 replies
  • 1751 views
  • 3 likes
  • 6 in conversation