I'm trying to create a filter to use in a proc sql. The comma in the string seems to be the issue. How do I get around the problem?
%let name_list = 'DAN','CAROL','JUNE';
data _null_;
/* create name filters */
length name_filter $40;
name_filter = " name in ("||&name_list.|| ")";
call symputx('name_where',name_filter);
run;
%put name_where =====> &name_where.;
This is the error I get.
SYMBOLGEN: Macro variable NAME_LIST resolves to 'DAN','CAROL','JUNE'
NOTE: Line generated by the macro variable "NAME_LIST".
29 'DAN','CAROL','JUNE'
_
388
200
ERROR 388-185: Expecting an arithmetic operator.
ERROR 200-322: The symbol is not recognized and will be ignored.
Thanks
Try running the SAS code you generated without the macro variables. That will make the error message more understandable. So replace the reference to NAME_LIST with its value.
1236 data _null_;
1237 length name_filter $40;
1238 name_filter = " name in ("||'DAN','CAROL','JUNE'|| ")";
-
388
200
ERROR 388-185: Expecting an arithmetic operator.
ERROR 200-322: The symbol is not recognized and will be ignored.
1239 call symputx('name_where',name_filter);
1240 run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
1238:31 1238:45
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
1:1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds
So you stuck a comma into the middle of your assignment statement and SAS is not happy with syntax like that.
Why use the data step at all?
%let name_where=name in (&name_list) ;
Of if you do want to use the data step code then make sure you are generating valid SAS code. While you are at it use one of the new CATxxx functions instead of the concatenation operator.
name_filter = cats("name in (","&name_list",")");
If you are worried that NAME_LIST will contain double quotes already causing issue like:
1246 name_filter = cats("name in (",""DAN","CAROL","JUNE"",")"); -- --- 1 49 --- 49 ----- 388 ----- 76 WARNING 1-322: Assuming the symbol AND was misspelled as AN. NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space between a quoted string and the succeeding identifier is recommended. ERROR 388-185: Expecting an arithmetic operator. ERROR 76-322: Syntax error, statement will be ignored.
then you could use the QUOTE() function, but then you need to macro quote the commas.
data _null_;
length name_filter $40;
name_filter = cats("name in (",%sysfunc(quote(%superq(name_list))),")");
call symputx('name_where',name_filter);
run;
Or you might want to just use SYMGET() instead of referencing the macro variable directly.
data _null_;
length name_filter $40;
name_filter = cats("name in (",symget('name_list'),")");
call symputx('name_where',name_filter);
run;
Try running the SAS code you generated without the macro variables. That will make the error message more understandable. So replace the reference to NAME_LIST with its value.
1236 data _null_;
1237 length name_filter $40;
1238 name_filter = " name in ("||'DAN','CAROL','JUNE'|| ")";
-
388
200
ERROR 388-185: Expecting an arithmetic operator.
ERROR 200-322: The symbol is not recognized and will be ignored.
1239 call symputx('name_where',name_filter);
1240 run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
1238:31 1238:45
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
1:1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds
So you stuck a comma into the middle of your assignment statement and SAS is not happy with syntax like that.
Why use the data step at all?
%let name_where=name in (&name_list) ;
Of if you do want to use the data step code then make sure you are generating valid SAS code. While you are at it use one of the new CATxxx functions instead of the concatenation operator.
name_filter = cats("name in (","&name_list",")");
If you are worried that NAME_LIST will contain double quotes already causing issue like:
1246 name_filter = cats("name in (",""DAN","CAROL","JUNE"",")"); -- --- 1 49 --- 49 ----- 388 ----- 76 WARNING 1-322: Assuming the symbol AND was misspelled as AN. NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space between a quoted string and the succeeding identifier is recommended. ERROR 388-185: Expecting an arithmetic operator. ERROR 76-322: Syntax error, statement will be ignored.
then you could use the QUOTE() function, but then you need to macro quote the commas.
data _null_;
length name_filter $40;
name_filter = cats("name in (",%sysfunc(quote(%superq(name_list))),")");
call symputx('name_where',name_filter);
run;
Or you might want to just use SYMGET() instead of referencing the macro variable directly.
data _null_;
length name_filter $40;
name_filter = cats("name in (",symget('name_list'),")");
call symputx('name_where',name_filter);
run;
Thanks for so many choices.
Why a data step?
%let name_where = name in ( &name_list. );
I had code already where I built several filters using a data step. I didn't think of the simplest way to do it. I need the data step because there are other decisions that have to be made depending on other data passed in to the report. Thanks for the reply.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.