I have +200 terms I want to throw at a table to see if they appear in in a column. The column is a string with many values so I’m using the contains function. The code below works as intended if I only have one word values in the variable. If I need two or more words with space(s) I can’t get it to work. I think I need to use quotes to maintain the string but I can’t pinpoint if it's how I’m building my terms list or the contains function doesn’t allow it or the formatting of my variable in my loop is incorrect. Any guidance would be appreciated.
%let terms =
JACK
JOHN SMITH
JOHN JACOB SMITH;
%macro doit(list);
proc sql noprint;
%let n=%sysfunc(countw(&list));
%do i=1 %to &n;
%let val = %scan(&list,&i);
INSERT INTO Name_Identifier
Select Distinct
"&val." as Search_Term
,a.*
From SCENARIO_TBL a
Where ID in
(
Select ID
From NAME_TABLE
Where NAMES_COL contains "&val."
)
%end;
quit;
%mend;
%doit(
&terms.
);
You did not tell %SCAN() what delimiter(s) to use, so it used its default list of delimiters.
Pick a character to use as the delimiter that will not appear in your actual search terms and use that.
%let terms =JACK|JOHN SMITH|JOHN JACOB SMITH;
...
... %sysfunc(countw(&list,|));
...
... %scan(&list,&i,|);