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

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,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.;

 

 

 

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

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
Obsidian | Level 7
oh, my mistake, in real case my items in lists are numbers, that is why I miss '' in my example
Reeza
Super User

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

sas-innovate-2024.png

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.

 

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
  • 3 replies
  • 639 views
  • 0 likes
  • 2 in conversation