BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
havmaage
Obsidian | Level 7

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. 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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" 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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" 

--
Paige Miller
Tom
Super User Tom
Super User

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

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1853 views
  • 1 like
  • 3 in conversation