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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.