I wonder why this does not work.
data _null_;
a = %qsubstr(12&34,1,4);
put a;
run;
expected ouput : 12&3
After resolution of the macro function, you get this data step:
data _null_;
a = 12&3;
put a;
run;
& is a mnemonic for the boolean "and" operator, so the SAS interpreter sees the 12 and the 3 as boolean values; since both are non-zero, this resolves to the boolean expression "true and true", so the result is "true", which is represented in SAS by 1.
If you wanted a to be the string "12&3", you would have needed to write
data _null_;
a = "%qsubstr(12&34,1,4)";
put a;
run;
Macro functions are not part of a DATA step. You might as well have coded:
data _null_;
a = 12&34,1,4;
put a;
run;
Note that Kurt is right about what you get when you do use %QSUBSTR ... 12&3 (not what I posted above). Now that will get resolved by as DATA step into the number 1. 12 is true, 3 is true, so 12 & 3 is true and gets resolved to 1.
In a DATA step, you might simply use:
data _null_;
a = substr('12&34',1,4);
put a;
run;
Single quotes always turn off macro processing, whether in a DATA step or in macro language statements.
After resolution of the macro function, you get this data step:
data _null_;
a = 12&3;
put a;
run;
& is a mnemonic for the boolean "and" operator, so the SAS interpreter sees the 12 and the 3 as boolean values; since both are non-zero, this resolves to the boolean expression "true and true", so the result is "true", which is represented in SAS by 1.
If you wanted a to be the string "12&3", you would have needed to write
data _null_;
a = "%qsubstr(12&34,1,4)";
put a;
run;
Thank you both for great explanation.
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!
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.