DATA Step, Macro, Functions and more

create a item list and use for entire code

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

create a item list and use for entire code

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,

 


Accepted Solutions
Solution
a month ago
Super User
Posts: 23,663

Re: create a item list and use for entire code

Posted in reply to leonzheng

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


All Replies
Solution
a month ago
Super User
Posts: 23,663

Re: create a item list and use for entire code

Posted in reply to leonzheng

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

 

 

 

 

 

Contributor
Posts: 36

Re: create a item list and use for entire code

oh, my mistake, in real case my items in lists are numbers, that is why I miss '' in my example
Super User
Posts: 23,663

Re: create a item list and use for entire code

Posted in reply to leonzheng

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

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 97 views
  • 0 likes
  • 2 in conversation