%UPCASE versus %QUPCASE

Reply
Contributor
Posts: 61

%UPCASE versus %QUPCASE

hello,

Chang here. My understanding is that both %UPCASE and %QUPCASE function convert lowercase values of a macro variable to uppercase values. However, they differ in that a macro trigger & remains functional in the %UPCASE but is maksed in the %QUPCASE. The following example is from SAS(R) 9.2 Macro Language: Reference. It is not clear to me why the %upcase(&b) resolves to "begin" rather than "BEGIN." When I ran %upcase(&b) alone, I got an error message. Please see my step-by-step analysis after the example. Thanks

Here is the example:

%let a=begin;

%let b=%nrstr(&a);

%put UPCASE produces: %upcase(&b);

%put QUPCASE produces: %qupcase(&b);

When these statements execute, the following is written to the SAS log:

UPCASE produces: begin
QUPCASE produces: &A

SAS(R) 9.2 Macro Language: Reference

Here is my step-by-step analysis: 

%let a=begin;

     /*Macro variable A resolves to begin*/

%let b=%nrstr(&a);

     /*the macro trigger & is treated as plain text in the %NRSTR() function*/

     /*Macro variable B resolves to &a*/

     /*the value &a is then assigned to the macro variable b by the %LET statement*/

  

%put UPCASE produces: %upcase(&b);

     /*As in the second %LET statement, &b resolves to &a*/

     /*the macro trigger & remains functional in the %UPCASE function. Therefore, &a resolves to begin*/

     /*the %UPCASE function converts begin to BEGIN*/

     /*but in the SAS log, it shows UPCASE produces: begin*/

%put QUPCASE produces: %qupcase(&b);

     /*As in the second %LET statement, &b resolves to &a*/

     /*the macro trigger & is masked by the %UPCASE function. Therefore, & becomes part of the plain text*/

     /*the %UPCASE function converts &a to &A*/

     /*In the SAS log, it shows QUPCASE produces: &A*/

%upcase(&b);

     /*when I submitted this code alone, I got this error message:

NOTE: Line generated by the macro function "UPCASE".

1     BEGIN

      -----

      180

ERROR 180-322: Statement is not valid or it is used out of proper order.

Many thanks

Respected Advisor
Posts: 3,124

Re: %UPCASE versus %QUPCASE

Macro quoting is difficult, it is so not just to you and me, it is so even to Ian Whitlock, so consider the confusing as a norm when dealing with macro quoting.

Here is my version of explanation, accuracy is NOT guaranteed.

I think you pretty much got it regarding %qupcase, so let's focus on %upcase. Note that many macro functions (obviously with the exceptions of macro quoting functions) that are not starting with a "Q" will do the UNQUOTE. %upcase is one of them. It does TWO things at the same time:

1. Unquote 2. Upcase. Let's see what happened here:

%put UPCASE produces: %unquote(&b); &b first unquote and resolve to &a, then upcase to &A, then resolve to 'begin'. Timing is the key: before the time that &a (or &A) is further resolved, the upcase function has already been applied and since expired.

Understand this part, you will know there are two places you can try to make BEGIN happen. 1. add another %upcase to upcase the 'begin', or 2. Unquote it first before the upcase, so the upcase will only work on the resolved results.

1.%put UPCASE produces: %upcase(%upcase(&b));

2. %put UPCASE produces: %upcase(%unquote(&b));

Both will give you BEGIN.

I assume you are on 9.2, maybe SAS has done something since then, as I can't repeat the error on 9.4 if I direct submit %upcase(&b); basically I got nothing, almost like SAS completely ignored this command, as if nothing has happened.

Good luck with macro quoting,

Haikuo

Super User
Super User
Posts: 6,315

Re: %UPCASE versus %QUPCASE

What did you expect UPCASE() to with those types of delayed references?

Ask a Question
Discussion stats
  • 2 replies
  • 759 views
  • 0 likes
  • 3 in conversation