BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
teg_76
Calcite | Level 5


Hi Everyone,

I have a SAS Macro that looks like this:

%LET SUBGRP=('202')

and I'm using it in a proc SQL statement:

cm.subgrp in @SUBGRP

This works, but what if I want to allow the user of the program to select all subgroups?  Sometimes they will need to do that.  How would I adjust my macro?

I tried:

%LET SUBGRP=('*')

and

%LET SUBGRP=('%')

..and both of those didn't work.  Any suggestions?

Thanks,

Tom

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Not really possible in PROC SQL to use both the IN operator and some type of wildcard.

If you are actually inside of a macro so that you can use macro logic then you could conditionally generate different SQL based on the value of your macro variable.  For example if you decide to use %let subgrp=*; to mean select all then you could code it like this:


...

where

%if "&subgrp" = "*" then 1 ;

%else cm.subgrp in &subgrp ;

...

Otherwise you could place the whole conditional into the macro variable.

%let subgrp=1;

or

%let subgrp=cm.subgrp in ('22','33');

...

where &subgrp

...

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Not really possible in PROC SQL to use both the IN operator and some type of wildcard.

If you are actually inside of a macro so that you can use macro logic then you could conditionally generate different SQL based on the value of your macro variable.  For example if you decide to use %let subgrp=*; to mean select all then you could code it like this:


...

where

%if "&subgrp" = "*" then 1 ;

%else cm.subgrp in &subgrp ;

...

Otherwise you could place the whole conditional into the macro variable.

%let subgrp=1;

or

%let subgrp=cm.subgrp in ('22','33');

...

where &subgrp

...

teg_76
Calcite | Level 5

Thanks for your help Tom!  I'll use your suggestions and see if I can work around the problem...

Florent
Quartz | Level 8

You could also update the proc SQL with something like this:

%LET SUBGRP = ;

Proc sql;

....

%IF &SUBGRP ne %str() %THEN %DO;

   where cm.subgrp in @SUBGRP

   ...

%END; %ELSE %DO;

   ....

%END;

...;

quit;

This would check whether a value was assigned to your macro and, in such a case, add the where condition to your sql query.

Regards,

Florent

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
  • 3 replies
  • 1193 views
  • 0 likes
  • 3 in conversation