CODE:
%macro ss(s=);
data d;
a=%substr("&s",2,%length(&s)-3)dy;
run;
%mend;
%ss(s=cmstdtc)
OUTPUT:
a=cmstdy
If im giving the substr position as 1 its not working but, if i give the position as 2 its working correctly. i want to know how this code works.Plz check the answer for a in log using "options mprint symbolgen".
Thanks in advance.
Please describe what you are trying to accomplish with your code? 🙂
The macro language does not use quotation marks to indicate a text value. In the macro language, all values are text, so they are not needed. If you use quote marks they become part of the value.
Below shows the quote marks are part of the value:
37 %let s=cmstdtc; 38 %put The length of macro var S is: %length(&s); The length of macro var S is: 7 39 %put The length of macro var S with quote marks is: %length("&s"); The length of macro var S with quote marks is: 9
It's a macro best practice to avoid adding quote marks to your values. If you choose to add quote marks, you need to remember that they are there, and account for them, which is why starting at position two works:
40 %put %substr("&s",2,%length(&s)-3)dy; cmstdy
If you started at position 1, the quote mark would be part of the value returned by %substr() [and you would end up with unmatched quoation marks].
But if you avoid adding the quote marks, you can start at position one, as you would expect:
41 %put %substr(&s,1,%length(&s)-3)dy; cmstdy
The quote marks are needed in the SAS language to indicate that a literal value is character rather than numeric. In the macro language, all values are character, so quotation marks are not used to to indicate character values.
Now i understood what mistake i have done thank you. 🙂
Hi,
I am in agreement with @PeterClemmensen here, there seems to be no point to this code, other than to obfuscate code? Substr is a datastep function, not a macro function - so nothing like %susbtr(). Create your question using the guidance provided. Provide test data - in the form of a datastep, and examplpe of what the output should look like.
Hi ramuking,
The reason you are experiencing the error when you change the "start" portionof the %substr to 1 if the quotation marks you have used on the "source" portion of the %substr.
Macro language, unlike dataset, is purely text. So there is no need for you to use quotations in your %substr function, as text is all it will read.
So when you are including the quotation marks the result you are getting from the %substr is "cmsdy, making your datastep read:
data d;
a = "cmsdy;
run;
and this is why it does not work.
In contrast, when you used 2 as your "start" your %substr function return cmstdy, making your datastep read:
data d;
a = cmstdy;
run;
as you desired.
So long story short, don't include the quotation marks in the source portion of your %substr function.
Hope this helps! 🙂
Thank you. 🙂
Hello,
You are messing things. You try applying macro functions in order to set data variables.
Macro functions shall be used to manipulate macro variables.
Just answering your question - the code works in terms of no error message but if you look at the database created, the values
are missing therefore in fact the code does not work.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.