BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

 

Is there a way to pass macro parameters as shwon below where it can handle a straight assignment or a if statement:

 

 

%macro test(var1=);

 

 

 

%mend test;

%test(var1=abc);

%test(var1= (if x=1 then var1='blah';

                     else var1='blah blah');

                   );

6 REPLIES 6
tomrvincent
Rhodochrosite | Level 12
I've often found it works to put double quotes around it and then remove them in the macro.

Example:
%test(var1="(if x=1 then var1='blah'; else var1='blah blah');" );

Then use
%let newvar1=%qsysfunc(compress(&var1,%str(%")));
to remove the double-quotes.
SASPhile
Quartz | Level 8

var1="(if x=1 then var1='blah'; else var1='blah blah');"

 

Isnt this double assignment for the same variable Var1 ?

Var1="(if x=1 then var1='blah'; else var1='blah blah');"

Tom
Super User Tom
Super User

@SASPhile wrote:

var1="(if x=1 then var1='blah'; else var1='blah blah');"

 

Isnt this double assignment for the same variable Var1 ?

Var1="(if x=1 then var1='blah'; else var1='blah blah');"


NO.  The first use of VAR1 is the name of the macro parameter.  The second use depends on how &VAR1 is used in the macro, but most likely it is referencing a dataset variable named var1.

tomrvincent
Rhodochrosite | Level 12
Different 'variables'.

Best to have different names for your macro variables and dataset columns.

Maybe MacroVar1="(if x=1 then colvar1='blah'; else colvar1='blah blah');" would be better.
Tom
Super User Tom
Super User

@tomrvincent wrote:
I've often found it works to put double quotes around it and then remove them in the macro.

Example:
%test(var1="(if x=1 then var1='blah'; else var1='blah blah');" );

Then use
%let newvar1=%qsysfunc(compress(&var1,%str(%")));
to remove the double-quotes.

Don't use COMPRESS() as it will also remove any quotes that you want to keep.  Instead use DEQUOTE().  If the value is not enclosed in quotes then DEQUOTE() will not change it.

%sysfunc(dequote(&var1))
Tom
Super User Tom
Super User

It is the sem-colons that are going to cause trouble.

You need to quote the value in the call and then remove the quoting when using the value.

 

Either use macro quoting.

%macro test(var1);
...
%unquote(&var1)
...
%mend test;
%test(var1=%str(if x=1 then var1='blah';else var1='blah blah';))
%test(var1=if x=1 then var1='blah'%str(;)else var1='blah blah'%str(;))

Or use you can use actual quotes.  Note that with this method you can call it with either macro quoting or actual quoting.

%macro test(var1);
...
%sysfunc(dequote((&var1))
...
%mend test;
%test(var1="if x=1 then var1='blah';else var1='blah blah';")
%test(var1='if x=1 then var1="blah";else var1="blah blah";')
%test(var1='if x=1 then var1=''blah'';else var1=''blah blah'';')

%test(var1=%str(if x=1 then var1='blah';else var1='blah blah';))
%test(var1=if x=1 then var1='blah'%str(;)else var1='blah blah'%str(;))

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 907 views
  • 0 likes
  • 3 in conversation