BookmarkSubscribeRSS Feed
Nietzsche
Lapis Lazuli | Level 10

Hello, I am reading about the %upcase function of the Advanced Prep Guide 4e pg 308

Nietzsche_0-1679114065622.png

 

  

%let a=begin;
%let b=%nrstr(&a);
%put UPCASE produces: %upcase(&a); %put UPCASE produces: %upcase(&b);

 

running the code produces 

 

Nietzsche_0-1679113004337.png

I think I understand why %upcase(&a) would resolves to "BEGIN". The logic I think should be something like this

%upcase(&a)      %upcase(begin)       BEGIN

 

But I don't understand why %upcase(&b) is not getting "BEGIN" as well.

%upcase(&b) becomes %upcase(%nrstr(&a)) as it refers to &b. 

%nrstr(&a) masks the "&" trigger, so it becomes just &a.

so it becomes %upcase(&a), which comes %upcase(begin) which should become BEGIN

 

%upcase(&b)      %upcase(%nrstr(&a))   → %upcase(&a)   %upcase(begin)

 → BEGIN

I don't understand why %upcase(&b) resolves to lowercase "begin".

 

thanks in advance.

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
5 REPLIES 5
Tom
Super User Tom
Super User

It does not matter what case you use when referring to a macro variable.  So &A and &a both reference the same macro variable named A. 

 

Because the value of B is macro quoted the %UPCASE() function just converted the string &a to the string &A.  But since you used %UPCASE() instead of %QUPCASE() the macro quoting was removed so the reverence to A is resolved by the %PUT statement.

 

Nietzsche
Lapis Lazuli | Level 10

@Tom wrote:

It does not matter what case you use when referring to a macro variable.  So &A and &a both reference the same macro variable named A. 

 

Because the value of B is macro quoted the %UPCASE() function just converted the string &a to the string &A.


Okay, is what your saying this?

%upcase(&b) → %upcase(%nrstr(&a)) → %upcase(&a) → %upcase(&A)→ being

 

 

if that is true, then why the first macro reference %upcase(&a) resolved to "BEGIN"?

While the second macro variable reference %upcase(&b)  becomes %upcase(&a) resolves to "begin"?

should they have the the same result as %upcase(&b) becomes exact %upcase(&a) mid step?

 

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
Tom
Super User Tom
Super User

The %NRSTR() delays the resolution of the macro variable reference.

 

So %UPCASE(&a) first resolves the &a to being.  Then it does the %UPCASE() on the string being to get BEING.

But %UPCASE(&b) first results the &b to &a (but with macro quoting).  So the upcase applies to just the letter a.  However the macro quoting is removed by the %upcase() fubction. So it is converting essentially %nrstr(&a) into plain old &A, which will resolve to  being .

 

If you ever need to do anything near this complicated with the macro language you are using the wrong tool for the job.  If you really need to manipulate data, like strings, then use SAS code.  You can always store the result back into a macro variable if it will help with future code generation.  Although for complex code generation you are better off using SAS code for that also.

Kurt_Bremser
Super User

The first %UPCASE converts the contents of macro variable a to upper case; the second %UPCASE gets the macro-quoted (because of the previous use of %NRSTR) string &a, and converts it to &A, which is then resolved by the %PUT to the original content of macro variable a, which is lowercase.

Nietzsche
Lapis Lazuli | Level 10

still can't get my head around it. Will come back after the exam. Hopefully this will not be tested in Advanced programming exam.

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 416 views
  • 1 like
  • 3 in conversation