## %qsubstr

Solved
Super Contributor
Posts: 285

# %qsubstr

I wonder why this does not work.

``````data _null_;
a = %qsubstr(12&34,1,4);
put a;
run;``````

expected ouput : 12&3

Accepted Solutions
Solution
‎06-05-2017 05:05 PM
Super User
Posts: 10,280

## Re: %qsubstr

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;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Super User
Posts: 6,785

## Re: %qsubstr

[ Edited ]

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.

Solution
‎06-05-2017 05:05 PM
Super User
Posts: 10,280

## Re: %qsubstr

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;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 285