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

How can I using macro to check if a value is in a list. 

For example, I have a macro variable 'med_rx',

%let med_rx=7804;

And i have a variable list 

%put &var_list;
1007383 1012123 103755 1151123 1151126 1151127 1151130 ...823181 866331 8785 995869

 

How can i check if 'med_rx' is in 'var_list'? Is there any way to do it like " if &med_var in &var_list then ..." in the macro?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@luu wrote:

How can I using macro to check if a value is in a list. 

For example, I have a macro variable 'med_rx',

%let med_rx=7804;

And i have a variable list 

%put &var_list;
1007383 1012123 103755 1151123 1151126 1151127 1151130 ...823181 866331 8785 995869

 

How can i check if 'med_rx' is in 'var_list'? Is there any way to do it like " if &med_var in &var_list then ..." in the macro?

Thanks!


That is not what I would think of as a variable list. A variable list would typically contain the names of Variables. That looks more like a value list. At which point it becomes likely to be a case of how are you using this? Sometimes this sort of question indicates some attempting to do the data manipulation in macro code that better fits in a data step.

 

%eval( %sysfunc(findw(&varlist,&med_rx)) > 0)

Will return a value of 1 (true) when the med_rx is an entire word in the varlist and 0 if not found.

 

HOWEVER the value is a macro variable and as such data step IF as you showed would be inappropriate.

 

if &med_rx in (&varlist) then do <something>

would be valid in a data step. However the values of the macro variables would be set before the data step compiles and would not be

 

any values of variables in the data step.

 

So how /where do you intend to use this comparison?

View solution in original post

2 REPLIES 2
ballardw
Super User

@luu wrote:

How can I using macro to check if a value is in a list. 

For example, I have a macro variable 'med_rx',

%let med_rx=7804;

And i have a variable list 

%put &var_list;
1007383 1012123 103755 1151123 1151126 1151127 1151130 ...823181 866331 8785 995869

 

How can i check if 'med_rx' is in 'var_list'? Is there any way to do it like " if &med_var in &var_list then ..." in the macro?

Thanks!


That is not what I would think of as a variable list. A variable list would typically contain the names of Variables. That looks more like a value list. At which point it becomes likely to be a case of how are you using this? Sometimes this sort of question indicates some attempting to do the data manipulation in macro code that better fits in a data step.

 

%eval( %sysfunc(findw(&varlist,&med_rx)) > 0)

Will return a value of 1 (true) when the med_rx is an entire word in the varlist and 0 if not found.

 

HOWEVER the value is a macro variable and as such data step IF as you showed would be inappropriate.

 

if &med_rx in (&varlist) then do <something>

would be valid in a data step. However the values of the macro variables would be set before the data step compiles and would not be

 

any values of variables in the data step.

 

So how /where do you intend to use this comparison?

luu
Fluorite | Level 6 luu
Fluorite | Level 6

Thank you for your reply! 

You are right, this is a value list instead of a variable list. 

I think your answer can solve my problem! I will check it. Thanks again!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 13239 views
  • 1 like
  • 2 in conversation