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%'
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.
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.