BookmarkSubscribeRSS Feed
avbraga
Calcite | Level 5

Hi all,

 

any way we can put the contents of an IN operator in a SAS macro. I'm doing something like:

 

PROC SQL;
CREATE TABLE data1 AS
SELECT *
FROM dataset t1
WHERE  t1.PAYTOCGVRNPI in ('1023027968','1750591632');
QUIT;

 

The thing is that I have to do many data pulls based on subsetting on the variable PAYTOCGVRNPI, which takes different values everytime. I'd like to put the content of teh IN operator in a macro, but I'm running into quoting issues. 

 

Any idea on how I could proceed here?

 

Thanks you!

 

6 REPLIES 6
ballardw
Super User

Describe how your are setting your macro variable(s) for use in the in statement.

 

You example code would work with: (though the comma isn't needed)

%let Mvar = '1023027968','1750591632';

PROC SQL;
   CREATE TABLE data1 AS
   SELECT *
   FROM dataset t1
   WHERE  t1.PAYTOCGVRNPI in (&mvar);
QUIT;

Are you passing values to a macro as parameters, generating them from a data set or something else?

 

avbraga
Calcite | Level 5

Thx Ballard. I've tried this option, but I would like to put it in this format

 

%macro a (var1, var2, var3);

   code....

%mend a; 

 

where:

var1  = would change the name of the datasets being created.

var2  = would be the variable I described previously to you

var3  = would be a facility number

 

but again, when I do it this way the commas and the quoting becomes an issue.

 

and I have a list of these paytocgrvnpi codes on a excel file for each facility number.

 

Thanks for the assistance.

 

ballardw
Super User

@avbraga wrote:

Thx Ballard. I've tried this option, but I would like to put it in this format

 

but again, when I do it this way the commas and the quoting becomes an issue.

 

The commas are not needed unless you are still running SAS 9.0 (I think, don't remember exactly which releast but is quite awhile ago).

So don't include them. Or if you need the commas at some other stage make a separate variable with commas inside the macro.

And are you always using character values that need to be quoted or will you sometimes need to pass actual numeric values?

Astounding
PROC Star

It sounds like you are turning an easy problem into a difficult one.  It's easy, assuming that you know how to turn your spreadsheet into a SAS data set containing a variable that holds facility numbers.  (If you don't know that part, you can get help with that too, but that would be a separate question.)

 

The third paramter in your macro would be the name of the SAS data set holding facility numbers.  Then your IN operator would look like this:

 

in (select distinct FACILITYNO from &var3)

avbraga
Calcite | Level 5

oh, this is interesting. a subquery within the query itself. Will try this approach. Thanks, Astounding.

Reeza
Super User

 

How are you creating your macro variables? If its from a different table use the quote function to help get quotes in the data.  

Can you do all at once, do you really need to create multiple subsets of data?

 

proc sql noprint;

select quote(paytocgvrnpi) into :select_list separated by " "
from my_table
where <your where condition>;
quit;

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
  • 6 replies
  • 1141 views
  • 0 likes
  • 4 in conversation