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

Can someone explain why this:

data have;
	set sashelp.class;
	where name='Alice';
run;

gives the same results as this (space after Alice)?

data have;
	set sashelp.class;
	where name='Alice ';
run;

 

I would think the blank space would cause SAS to not evaluate it as equivalent.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS stores character strings as fixed length padded with spaces.

So when it compares two strings it ignores the trailing spaces.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

SAS stores character strings as fixed length padded with spaces.

So when it compares two strings it ignores the trailing spaces.

Reeza
Super User

Since trailing blanks are ignored in a comparison, 'fox ' is equivalent to 'fox'. However, because blanks at the beginning and in the middle of a character value are significant to SAS, ' fox' is not equivalent to 'fox'.

 

https://documentation.sas.com/doc/en/lrcon/9.4/p00iah2thp63bmn1lt20esag14lh.htm#p1vu0ts68u42xun141p7...

 

Character Comparisons

You can perform comparisons on character operands, but the comparison always yields a numeric result (1 or 0). Character operands are compared character by character from left to right. Character order depends on the collating sequence, usually ASCII or EBCDIC, used by your computer.

For example, in the EBCDIC and , G is greater than A. Therefore, this expression is true:

'Gray'>'Adams'

Two- of unequal length are compared as if blanks were attached to the end of the shorter value before the comparison is made. A blank, or missing character value, is smaller than any other printable character value. For example, because . is less than h, this expression is true:

'C. Jones'<'Charles Jones'

Since trailing blanks are ignored in a comparison, 'fox ' is equivalent to 'fox'. However, because blanks at the beginning and in the middle of a character value are significant to SAS, ' fox' is not equivalent to 'fox'.

You can compare only a specified prefix of a character expression by using a colon (:) after the comparison operator. SAS truncates the longer value to the length of the shorter value during the comparison. In the following example, the colon modifier after the equal sign tells SAS to look at only the first character of values of the variable LastName and to select the observations with names beginning with the letter S:

if lastname=:'S';

Because printable characters are greater than blanks, both of the following statements select observations with values of LastName that are greater than or equal to the letter S:

  • if lastname>='S';
  • if lastname>=:'S';
Note: If you compare a zero-length character value with any other character value in either an IN: comparison or an EQ: comparison, the two-character values are not considered equal. The result always evaluates to 0, or false.

The operations that are discussed in this section show you how to compare entire character strings and the beginnings of character strings. Several SAS  enable you to search for and extract values from within character strings. See SAS Functions and CALL Routines: Reference for complete descriptions of all SAS functions.

SAShole
Pyrite | Level 9
Thank you for the detailed response Reeza, very helpful!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 619 views
  • 1 like
  • 3 in conversation