11-16-2016 08:53 AM
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.
11-16-2016 09:08 AM - edited 11-16-2016 09:11 AM
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.
11-16-2016 09:09 AM
I am in agreement with @draycut 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.
11-16-2016 09:09 AM - edited 11-16-2016 09:14 AM
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:
a = "cmsdy;
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:
a = cmstdy;
as you desired.
So long story short, don't include the quotation marks in the source portion of your %substr function.
Hope this helps!
11-16-2016 09:16 AM
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.