Is it possible to use a macro variable for proc sql like condition?
Such as :
proc sql;
create table key as
select * from c.numkeys
where upcase(esc) like '%&&name&i.%' ;
run;
The name and i are macro variables.
I think you need triple & if I understand your macro variable structure.
You can use %QUOTE() to generate a protected percent sign so that SAS doesn't think you want to call macro.
Or you can just use the CONTAINS operator instead of the LIKE operator.
%let i=1;
%let name=value;
%let value1=A ;
proc sql ;
select * from sashelp.class
where upcase(name) like "%quote(%%)&&&name&i%"
;
%put &sqlobs;
select * from sashelp.class
where upcase(name) contains "&&&name&i"
;
%put &sqlobs;
Well, I personally would avoid using several macro variables like that, i.e. &name, and &I. It becomes very messy with all those ampersands and de-referencing. Never actually found a time when I needed to do that.
As for your question, the like would need to have double quotes:
"%&&name&i.%"
Otherwise the macro variable won't de-reference. As for &&this and the other...
The double quotes were used before. The warning messages are:
WARNING: Apparent invocation of macro value1 not resolved.
WARNING: Apparent symbolic reference NAME not resolved.
WARNING: Apparent invocation of macro value1 not resolved.
WARNING: Apparent symbolic reference value1value1 not resolved.
The value1 is name's value. Is it a way to get out the warnings?
Exactly, parts of the macro variables are being de-referenced at differing times. TBH I am not going to try to work out which order they do it as I really don't like lines of ampersands. Post some example code and test data/output and will see what I can come up with tomorrow.
I think you need triple & if I understand your macro variable structure.
You can use %QUOTE() to generate a protected percent sign so that SAS doesn't think you want to call macro.
Or you can just use the CONTAINS operator instead of the LIKE operator.
%let i=1;
%let name=value;
%let value1=A ;
proc sql ;
select * from sashelp.class
where upcase(name) like "%quote(%%)&&&name&i%"
;
%put &sqlobs;
select * from sashelp.class
where upcase(name) contains "&&&name&i"
;
%put &sqlobs;
Why not use SYMGET() ?
%let value1=A ;
proc sql ;
select * from sashelp.class
where upcase(name) contains symget('value1');
;
quit;
Xia Keshan
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.