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

 

%macro EM(cntry=);

%let cntr = %sysfunc(compress(&cntry.));

%put cntry resolves to &cntry ;

%if "&cntry."="OMAN" or "&cntry." = "CHINA" %then %let like_cntry=%nrbquote('&cntry.%');  
 %else %let like_cntry=%nrquote('%')||%bquote('&cntry.')||%nrquote('%') ;

%put like_cntry resolves to &like_cntry.;
%mend
%em(cntry=japan)

Hi,

 

I want to resolve &like_cntry as  '%japan%'  since iam using in where clause

when i was trying to resolve iam getting this output  '%'||'japan'||'%'

 

Can anyone help me on this?

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

To concatenate macro variables you cant use '||' (nor '!!').

In order to dislay the &cntry value you must use double quotes. 

Check next code:

%macro EM(cntry=);
	%let cntr = %sysfunc(compress(&cntry.));
	%put cntry resolves to &cntry ;
	%if "&cntry."="OMAN" or "&cntry." = "CHINA" 
	   %then %let like_cntry=%nrbquote("&cntry.%");  
	   %else %let like_cntry=%nrbquote(%)&cntry.%nrbquote(%);
    %put like_cntry resolves to &like_cntry.;
%mend;
%em(cntry=OMAN);
%em(cntry=japan);

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

To concatenate macro variables you cant use '||' (nor '!!').

In order to dislay the &cntry value you must use double quotes. 

Check next code:

%macro EM(cntry=);
	%let cntr = %sysfunc(compress(&cntry.));
	%put cntry resolves to &cntry ;
	%if "&cntry."="OMAN" or "&cntry." = "CHINA" 
	   %then %let like_cntry=%nrbquote("&cntry.%");  
	   %else %let like_cntry=%nrbquote(%)&cntry.%nrbquote(%);
    %put like_cntry resolves to &like_cntry.;
%mend;
%em(cntry=OMAN);
%em(cntry=japan);
Shmuel
Garnet | Level 18

t can be even more simple - both methods were tested:

%macro EM(cntry=);
	%let cntr = %sysfunc(compress(&cntry.));
	%put cntry resolves to &cntry ;
	%if "&cntry."="OMAN" or "&cntry." = "CHINA" 
	   %then %let like_cntry=&cntry.%;  
	   %else %let like_cntry=%nrbquote(%)&cntry.%; /*nrbquote(%);*/
    %put like_cntry resolves to &like_cntry.;
%mend;
%em(cntry=OMAN);
%em(cntry=japan);
gamotte
Rhodochrosite | Level 12

Hello,

 

What is the following instruction used for ?

 

%let cntr = %sysfunc(compress(&cntry.));

 

If you want to remove leading and trailing blanks, the following will be enough :

%let cntr=&cntry.;

By using compress, you also remove blanks in the country name so "COSTA RICA", for instance, will become "COSTARICA".

 

Try the following code and look at the log :

data _NULL_;
call symput("cntry","  COSTA RICA   ");
run;

%put ***&=cntry.***;

%let cntr = %sysfunc(compress(&cntry.));

%put ***&=cntr.***;

%let cntr2=&cntry.;

%put ***&=cntr2.***;

Note that it would not have been necessary if symputx had been used instead of symput.

Ksharp
Super User
%macro EM(cntry=)/minoperator mindelimiter=',';
%put cntry resolves to &cntry ;

%if %upcase(&cntry.) in OMAN,CHINA %then %let like_cntry=%bquote('&cntry.%');  
 %else %let like_cntry=%sysfunc(compress(%bquote('% &cntry.%'),%str( ))) ;

%put like_cntry resolves to &like_cntry.;
%mend;
%em(cntry=japan)
%em(cntry=china)

How about this one ?

Siva_Harish
Obsidian | Level 7
This also worked for me but i want '%japan%' (including quotes) . I resolved the issue .Thank you..!!
Ksharp
Super User
Did you run my code ?

1 %macro EM(cntry=)/minoperator mindelimiter=',';
2 %put cntry resolves to &cntry ;
3
4 %if %upcase(&cntry.) in OMAN,CHINA %then %let like_cntry=%bquote('&cntry.%');
5 %else %let like_cntry=%sysfunc(compress(%bquote('% &cntry.%'),%str( ))) ;
6
7 %put like_cntry resolves to &like_cntry.;
8 %mend;
9 %em(cntry=japan)
cntry resolves to japan
like_cntry resolves to '%japan%'
10 %em(cntry=china)
cntry resolves to china
like_cntry resolves to 'china%'



Tom
Super User Tom
Super User

To put the values of macro variables next to each other just evaluate them next to each.  

&X.&Y.

If you just remove the spurious vertical bar characters from your source code then and they won't end up in the generated code.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 7 replies
  • 1558 views
  • 0 likes
  • 5 in conversation