I want to create a item list at the beginning of a SAS code and use it as condition to pull data. Cannot figure out how to do it...
example:
%let fruitlist = apple, orange, banana;
.
.
.
select *
from table A
where fruit in (&fruitlist.)
.
.
.
select *
from table B
where fruit in (&fruitlist.)
.
.
.
My code is similar as above. So next time when I want to change condition fruit items, I just need to modify the first line.
Please help me on this, thanks,
Best regards,
The key thing to remember is that this is direct text replacement. So following that logic, your code becomes:
%let fruitlist = apple, orange, banana;
select *
from table A
where fruit in (apple, orange, banana);
With this, its clear to see that this is not valid SAS syntax, and you probably received error to indicate this as well.
Adding quotes solves this issue but remember that it's case sensitive so Apple is not the same as apple.
%let fruitlist = 'apple', 'orange', 'banana';
If you're creating your macro variables from a data step or PROC SQL step, use the QUOTE function to quote them as you add them to the list.
proc sql noprint;
select quote(name) into :name_list separated by ", "
from sashelp.class;
quit;
%put &name_list.;
The key thing to remember is that this is direct text replacement. So following that logic, your code becomes:
%let fruitlist = apple, orange, banana;
select *
from table A
where fruit in (apple, orange, banana);
With this, its clear to see that this is not valid SAS syntax, and you probably received error to indicate this as well.
Adding quotes solves this issue but remember that it's case sensitive so Apple is not the same as apple.
%let fruitlist = 'apple', 'orange', 'banana';
If you're creating your macro variables from a data step or PROC SQL step, use the QUOTE function to quote them as you add them to the list.
proc sql noprint;
select quote(name) into :name_list separated by ", "
from sashelp.class;
quit;
%put &name_list.;
@leonzheng wrote:
oh, my mistake, in real case my items in lists are numbers, that is why I miss '' in my example
Well, I can only comment on what you show. If this doesn't resolve your problem you need to provide more details of what you want and what isn't working - and what not working means.
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.