Comparison operator in SAS – The =: Operator

Most people who find themselves acquainted with programming SAS are aware of the colon operator ( =: ). There are a couple of different colon operators in SAS, however, on this post, I’m only talking about the comparison operator in SAS.

The equal colon operator works very similarly to the <a href="https://www.9to5sas.com/character-function-in-sas/">substr()</a> function. It is used to compare substrings for equality.

Here is a quick little example:

data null;
 x = 'abcdefg';
 if x =: 'abc'
  then put 'The substrings match.';
run;

As you may see when you run; the data step above the substring ‘abc’ matches in ‘abcdefg’.

A better approach to think about it’s that it “starts with” the substring. The same could also be achieved by this statement:

if substr(x,1,3) = 'abc'
 then put 'The substrings match.';

There is one major difference between the =: operator and using substr().

With the substr() function you tell SAS precisely how many characters to search for the substring.

In the example above it was 3. For the =: operator it has to determine what number of characters to look.

It does this by (considerably counter-intuitively) each side of the operator to search out the shortest length.

Here is an example:

data null;
 x = 'abcdefg';
 if x =: 'abc'
  then put 'The substrings match.';
run;

If you run the above data step you will notice that they match.

It appears to be slightly humorous because most of us assume that SAS is searching for ‘abcdefg’ within ‘abc’, however that is probably not what’s happening.

SAS uses the shortest string to determine what to search for, irrespective of which side of the equals signal it’s on.

Oh yeah, the =: operator additionally works in a list context similar to:

if x in: ('abc', 'xyz', 'def');

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap