BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

I wonder why this does not work.

 

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

expected ouput : 12&3

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

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.

Kurt_Bremser
Super User

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;
SAS_inquisitive
Lapis Lazuli | Level 10

Thank you both for great explanation.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 3 replies
  • 1006 views
  • 2 likes
  • 3 in conversation