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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 18465 views
  • 2 likes
  • 2 in conversation