SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 19379 views
  • 2 likes
  • 2 in conversation