sas macro how to check a value in a list

Accepted Solution Solved
Reply
Occasional Contributor luu
Occasional Contributor
Posts: 15
Accepted Solution

sas macro how to check a value in a list

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!


Accepted Solutions
Solution
‎04-09-2018 01:09 PM
Super User
Posts: 13,583

Re: sas macro how to check a value in a list


@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


All Replies
Solution
‎04-09-2018 01:09 PM
Super User
Posts: 13,583

Re: sas macro how to check a value in a list


@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?

Occasional Contributor luu
Occasional Contributor
Posts: 15

Re: sas macro how to check a value in a list

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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