BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tianerhu
Pyrite | Level 9

%SUBSTR does not mask special characters or mnemonic operators in its
result, even when the argument was previously masked by a macro quoting function.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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(&quoted,1)=%substr(&quoted,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(&quoted,1)=%substr(&quoted,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(&quoted,1),1);
c

 

View solution in original post

4 REPLIES 4
WarrenKuhfeld
Ammonite | Level 13

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
Pyrite | Level 9
Thank you for your help.
Tom
Super User Tom
Super User

@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(&quoted,1)=%substr(&quoted,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(&quoted,1)=%substr(&quoted,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(&quoted,1),1);
c

 

tianerhu
Pyrite | Level 9

Thanks for your help.

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
  • 4 replies
  • 703 views
  • 0 likes
  • 3 in conversation