Hi,
I need to execute a sql using like condition, That means i need to add % signs to the condition string. I having a hard time figure out how to do it.
I have a macrovariable that has the value "tablename"
what i want to do is to put % chars before and in the end of the string. I have tried using %str,%nrquote and %quote
%let tb="tablername";
%let likecondition=%str(%&tb%);
%put likecondition;
I have also tried compress but it seems that i cannot mask the % as string.
have
"tablername"
want
"%tablename%"
How do i do this in sas macro or data step if that is preferred.
You need a double percent sign to result in the single-percent sign in the output, see https://documentation.sas.com/doc/en/pgmmvacdc/9.4/mcrolref/n09tblrxldh8k0n1kt6dkj3xlxug.htm
%let tb=tablername;
%let likecondition="%nrstr(%%)&tb%nrstr(%%)";
%put &=tb;
%put &=likecondition;
Except for rare cases, I would not enclose the value of a macro variable in quotes or double-quotes, so I would use:
%let tb=tablername;
%let likecondition=%nrstr(%%)&tb%nrstr(%%);
%put &=tb;
%put &=likecondition;
and then use "&likecondition"
You need a double percent sign to result in the single-percent sign in the output, see https://documentation.sas.com/doc/en/pgmmvacdc/9.4/mcrolref/n09tblrxldh8k0n1kt6dkj3xlxug.htm
%let tb=tablername;
%let likecondition="%nrstr(%%)&tb%nrstr(%%)";
%put &=tb;
%put &=likecondition;
Except for rare cases, I would not enclose the value of a macro variable in quotes or double-quotes, so I would use:
%let tb=tablername;
%let likecondition=%nrstr(%%)&tb%nrstr(%%);
%put &=tb;
%put &=likecondition;
and then use "&likecondition"
Is there some reason the first macro variable includes the quotes? If it does include the quotes and you want the % inside the quotes you probably need to first remove the quotes.
But you probably would like to include single quotes around the value with the % in it to prevent the macro processor from typing to call a macro with the same name as the table.
If you can use a data step it is easier since you don't have to worry about the fighting the macro processor.
So if you already have the table name in a dataset and want to generate a macro variable use something like:
18 data _null_;
19 tb="tablename";
20 call symputx('likecondition',quote(cats('%',tb,'%'),"'"));
21 run;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
22
23 %put &=likecondition;
LIKECONDITION='%tablename%'
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!
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.