Comparison operator in SAS - The =: Operator

Comparison operator in SAS – The =: Operator

  • Post author:
  • Post category:Base SAS
  • Post comments:0 Comments
  • Reading time:3 mins read

Many programmers who work with SAS are familiar with the colon operator (=:). There are various colon operators in SAS, but in this post, I will only discuss the comparison operator. The equal colon operator is very similar to the substr() function and is used to compare substrings for equality.

Here is a brief example for you to consider:

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 way to think about it is to ” start with” the substring. This statement could also achieve the same:

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 must determine the number of characters to look for.

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

You will notice that they match if you run the above data step..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

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

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