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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 780 views
  • 2 likes
  • 3 in conversation