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
Rhodochrosite | Level 12

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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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