- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I have the following query that search for three words in two columns :
PROC SQL;
CREATE TABLE test AS
SELECT t1.*
FROM mylib.mytable
WHERE upcase(t1.c1) contains "BOB" or
upcase(t1.c1) contains "ROBERT" or
upcase(t1.c1) contains "LUCY" or
upcase(t1.c2) contains "BOB" or
upcase(t1.c2) contains "ROBERT" or
upcase(t1.c2) contains "LUCY"
;QUIT;
I'd like to be able to replace this by a macro-variable where I store a list of all the words I want to search and a macro that call that list and the column I want it to apply to
i.e :
%LET MY_WORDS = ("BOB","ROBERT","LUCY")
%mymacro(t1,c1,c2,&my_words)
is this something that can be done?
Thank you for your help and time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sure you can.
It depends, though, on what you want to achieve, what steps you do besides the proc sql and how clean you want your code to be.
Here is one quick'n'dirty example:
data have;
a = 'emil, Nordpole, richard, Theo'; b = 'something, somethingelse'; output;
a = 'sandman, emil, peter'; b = 'whatever'; output;
a = 'samuel'; b = 'anoterexample'; output;
a = 'stephany'; b = 'lookingforwordshere'; output;
run;
%macro quickndirty(varlist);
%let varlist = %upcase(&varlist.);
proc sql;
create table want as
select * from have
where 1=0
%do i=1 %to %sysfunc(countw(&varlist.));
%let next_name = %scan(&varlist., &i.);
or upcase(a) contains "&next_name."
or upcase(b) contains "&next_name."
%end;
;quit;
%mend;
%let list = nordpole pet lookingforwords;
%quickndirty(&list.)
I think you will be able to apply this to your example.
The "1=0" is necessary, because the first "or" would lead to an error otherwise. As I said, this is just a quick'n'dirty example to get you started.
Notice, however, that this does not search for complete words, but rather for the part that you enter. In my example I wrote "pet" instead of "peter", yet it still finds "peter". So unclear identifiers and typos might lead to unexpected results!
More sophisticated solutions might be found, but then again, it depends on your requirements.
You might also consider using ID's instead of names and follow the example procided here.
Cheers,
Michael