%SUBSTR does not mask special characters or mnemonic operators in its
result, even when the argument was previously masked by a macro quoting function.
@tianerhu wrote:
%SUBSTR does not mask special characters or mnemonic operators in its
result, even when the argument was previously masked by a macro quoting function.
It is saying if you use %SUBSTR() to extract part of an existing string the resulting string will not be masked.
If you use %QSUBSTR() to extract part of an existing string the resulting string WILL have macro quoting.
Consider this example:
%let unquoted=abcd,3;
%let quoted=%str(abcd,3);
%put substr(&unquoted,1)=%substr(&unquoted,1);
%put substr("ed,1)=%substr("ed,1);
When the input is unquoted the ,3 is considered one of the arguments and the result is just the third character, c.
When it is quoted then the ,3 is part of the string to subset and since the starting point is the first character and no length is given the result is the full string back again.
24 %put substr(&unquoted,1)=%substr(&unquoted,1); substr(abcd,3,1)=c 25 %put substr("ed,1)=%substr("ed,1); substr(abcd,3,1)=abcd,3
But the result is no longer quoted.
You can see this by feeding the output back into the same test:
27 %put %substr(%substr("ed,1),1); c
While not directly answering your question, a good heuristic when you have to process values of macro variables that might contain any special characters is use a DATA step (e.g. DATA _NULL_), SYMGET, ordinary DATA step string processing, and then SYMPUTX. Your life will be much easier.
@tianerhu wrote:
%SUBSTR does not mask special characters or mnemonic operators in its
result, even when the argument was previously masked by a macro quoting function.
It is saying if you use %SUBSTR() to extract part of an existing string the resulting string will not be masked.
If you use %QSUBSTR() to extract part of an existing string the resulting string WILL have macro quoting.
Consider this example:
%let unquoted=abcd,3;
%let quoted=%str(abcd,3);
%put substr(&unquoted,1)=%substr(&unquoted,1);
%put substr("ed,1)=%substr("ed,1);
When the input is unquoted the ,3 is considered one of the arguments and the result is just the third character, c.
When it is quoted then the ,3 is part of the string to subset and since the starting point is the first character and no length is given the result is the full string back again.
24 %put substr(&unquoted,1)=%substr(&unquoted,1); substr(abcd,3,1)=c 25 %put substr("ed,1)=%substr("ed,1); substr(abcd,3,1)=abcd,3
But the result is no longer quoted.
You can see this by feeding the output back into the same test:
27 %put %substr(%substr("ed,1),1); c
Thanks for your help.
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!
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.