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!
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?
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.
@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?
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)
oh, this is interesting. a subquery within the query itself. Will try this approach. Thanks, Astounding.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.