Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » ADVANCED SAS » SAS Macros » How to use the IN operator in SAS macro?

How to use the IN operator in SAS macro?

BySubhro Posted onAugust 15, 2020February 2, 2023 Last Updated:February 2, 2023
3 Comments

IN operator is used in the data step to replace multiple OR conditions. However, if you try to use IN Operator in SAS Macro then you will find that the IN operator is not available

IN Operator in SAS Macro

IN operator is used in the data step to replace multiple OR conditions. However, if you try to use IN Operator in SAS Macro then you will find that the IN operator is not available.

%macro test(names);
	%if &names in('Alice','James','John','Judy') %then
		%do;
			%put &names;
		%end;

%mend;
%test(names=Alice);

Upon attempting to execute this macro, an error message will appear along with the macro being terminated.

ERROR: Required operator not found in expression: &names in(‘Alice’,’James’,’John’,’Judy’) ERROR: The macro TEST will stop executing.

Page Contents

  • Using the IN (#) Operator in SAS Macro
    • Example 1: Using the IN Operator in the SAS macro
    • Example 2: Using the IN Operator with a comma-separated list of values
  • Difference between the DATA STEP IN and MACRO IN Operator
    • Using NOT in with the IN operator in Macro
    • Example: 3 Check if a macro value is not any of the values from the list

Using the IN (#) Operator in SAS Macro

In order to use the IN operator inside a macro, there are 2 system options you need to specify.

  • MINOPERATOR- Enabling this option, you can use IN (#) operator with the list of values. If the delimiter in the match list is not space, it must be specified with the MINDELIMITER= option.
  • MINDELIMITER– It is used to specify the delimiter for the list of values used in the IN operator. By default, this option is set to a single space.

Example 1: Using the IN Operator in the SAS macro

The following example uses the macro IN operator to search a character string:

options minoperator;
%macro test(names);
	%if %eval(%upcase(&names) IN ALICE JAMES JOHN JUDY) %then
		%do;
			%put &names exists in the list;
		%end;

%mend;

%test(names=ALICE);

Any of the below statements also works fine once you enable the MINOPERATOR system options.

%if %eval(%upcase(&names) # ALICE JAMES JOHN JUDY) %then
%if %upcase(&names) IN ALICE JAMES JOHN JUDY %then
%if %upcase(&names) # ALICE JAMES JOHN JUDY %then

Use the MINOPERATOR system option or in the %MACRO statement if you want to use the <strong>IN</strong> (<strong>#</strong>) as operators in expressions:

options minoperator;

To use IN or # as operators in expressions evaluated during the execution of a specific macro, use the MINOPERATOR keyword on the definition of the macro.

%macro macroname / minoperator;

Example 2: Using the IN Operator with a comma-separated list of values

The following is an example using a specified delimiter in an IN operator:

%macro test(names)/ minoperator mindelimiter=',';
	%if %upcase(&names) # ALICE,JAMES,JOHN,JUDY %then
		%do;
			%put &names;
		%end;
	run;
%mend;
%test(names=Alice);

Difference between the DATA STEP IN and MACRO IN Operator

The macro IN operator is similar to the DATA step IN operator, but there are a few differences that you need to know.

  • The macro IN operator cannot search arrays.
  • The colon operator (:) cannot be used as a shorthand notation to specify a range.
  • The default delimiter for list elements is a blank.
  • Both operands must contain a value.
%put %eval(names IN ALICE JAMES JOHN JUDY); /*Both operands are present. */

If an operand contains a null value, an error is generated.

%put %eval( IN ALICE JAMES JOHN JUDY); /*Missing first operand. */

or

%put %eval( names IN ); /*Missing first operand. */

Whether the first or second operand contains a null value, the same error is written to the SAS log:

ERROR: Operand missing for # operator in argument to %EVAL function.

Using NOT in with the IN operator in Macro

The IN operator compares the value of the operand on the left side of the equal sign against the list of values in the operand on the right side and returns a Boolean value.

If a match is found in the right-hand operand 1 is returned otherwise it returns 0. You can use the NOT operator to negate the return value.

Example: 3 Check if a macro value is not any of the values from the list

The following example uses the macro IN operator to search a character string.

%macro test(names)/ minoperator;
	%if not(%UPCASE(&names) in ALICE JAMES JOHN JUDY) %then
		%put &names is not in list;
	%else %put &names is in list;
%mend test;

%test(names=Ali);

LOG results:

Ali is not in list

Key Takeaway

So, this is the way you can use the IN Operator in a SAS Macro. You’ll notice that the syntax differs a little from Data Step IN Operator. In short, there are no brackets around the list of values, and the values are separated by spaces.

Also, the default delimiter is the space so you may need to change that using the MINDELIMITER= to specify an alternate delimiter.

You can use the hash character (#) instead of “in”, but I suggest that would just make your code hard to read.

I really hope that you must have found it useful.

Moreover, if you have any other suggestions regarding other tips or tricks to add, then please suggest them in the comment section. We’d love to include those lists in our next blog post.

Thanks for reading!


If you liked this article, you might also want to read SSMDEL in SAS and CALL SYMPUT as well.

Do you have any tips to add Let us know in the comments.

Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.

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

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #macro in operator#macro in operator sas
Subhro

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.

Facebook Twitter Linkedin

Post navigation

Previous Previous
SAS Generation Datasets
NextContinue
How to create folders using SAS?

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Proc SQL Case When Statement: A Guide to Efficient Data Analysis
  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You