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!
@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 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?
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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.