BookmarkSubscribeRSS Feed
nicnad
Fluorite | Level 6

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.

1 REPLY 1
mfab
Quartz | Level 8

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

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
  • 1 reply
  • 18505 views
  • 2 likes
  • 2 in conversation