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

Hello,

I want to create a macro that needs to identify whether a macro variable previously defined contains certain variable. For example, I first created the macro variable 'covariates' as shown below:

 

%let covariates = seq weekend sex;

 

Then, the following macro runs a block of code depending whether the variable 'sex' is included in &covariates  or not (See red text below).

%macro test;

     *The two lines of code below are to show what I, not how;
     "if &covariates contains SEX" then &sex=1;
                                                     else &sex=0;

            %if %sysevalf(&sex=1) %then %do;
            %end;

           %else %do;
           %end;

%mend;

Can you please advice?

Thanks a lot.

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

Use the base function INDEXW to detect the position of a word in a delimited list of words.  If not found the result will be zero, so you can use the result as a logical test for presence.

 

INDEXW is case sensitive so you need to be careful.

 

Example

%let itemlist = seq weekend sex;
%put NOTE: token SEX at position %sysfunc(indexw(%upcase(&itemlist),SEX));
%put NOTE: token WEEKDAY at position %sysfunc(indexw(%upcase(&itemlist),WEEKDAY));

Log

NOTE: token SEX at position 13
NOTE: token WEEKDAY at position 0

 

View solution in original post

4 REPLIES 4
Reeza
Super User

Where will this occur? Within a data step, SQL or open code? What should happen in the end, a new variable is being created or a new macro variable? What is the macro variable sex coming from and what value does that contain? Why the duplicate logic as well, is there a specific reason to create that variable rather than go straight into the IF/THEN logic?

 

As to isolating terms, you can use regular functions like FIND but you need to use SYSFUNC() within the macro language. 

 

 

 

 

 

alexgonzalez
Quartz | Level 8
Hi Reeza,
The variable 'SEX' is specified in a previous part of my code as a covariate in a regression model. In the new macro I'm referring to, I wanted to decide which block of code to run. That's to say, if 'SEX' is part of the covariates in the model, then run a block, else run the other one. I tried the FIND function, but I could not get the answer I needed. I wasn't sure who to make work in my example. See reply by RichardAdeVenezia, that worked for me.
Thank you!
RichardDeVen
Barite | Level 11

Use the base function INDEXW to detect the position of a word in a delimited list of words.  If not found the result will be zero, so you can use the result as a logical test for presence.

 

INDEXW is case sensitive so you need to be careful.

 

Example

%let itemlist = seq weekend sex;
%put NOTE: token SEX at position %sysfunc(indexw(%upcase(&itemlist),SEX));
%put NOTE: token WEEKDAY at position %sysfunc(indexw(%upcase(&itemlist),WEEKDAY));

Log

NOTE: token SEX at position 13
NOTE: token WEEKDAY at position 0

 

alexgonzalez
Quartz | Level 8
Thanks Richard, that's exaclty what I needed.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1257 views
  • 3 likes
  • 3 in conversation