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');
);
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');"
@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 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))
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(;))
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!
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.