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

Hi,

here the task:

 

%LET first=yourname

%LET last=first

 

%PUT &&&last;

 

What is the result in log?

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

I believe I did above?  Maybe read the SAS documentation thoroughly on macro programming, this document may help also:

http://www2.sas.com/proceedings/sugi22/CODERS/PAPER77.PDF

View solution in original post

10 REPLIES 10
Astounding
PROC Star

The result is an error message.  SAS statements have to end with a semicolon.

 

Are you unable to test the code yourself?

cons
Calcite | Level 5

Hi. No you dont need ; if u define an macro variable.

cons
Calcite | Level 5

Hi its me again.

 

I know the result its vodername. But I dont understand why I need no & in Line 2 (%Last=first) If i want reference to macro variable first then I need zo type $first, right?

 

Thanks!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Its quite simple.

%PUT &&&last;

What happens when this goes into the macro pre-processor.  Well, it see's &last. which is a macro variable, so is de-references it to the text first.  Then it looks at the statement again:

%put &first;

Which again is a macro variable, so it de-references it to be:

%put yourname;

Which puts yourname in the log.  Not sure where you get "vodername" from as thats not in the test data.

 

 

And just to confirm, you should put semi-colons after macro invocation otherwise it does not work, @Astounding, is quite correct.  Also note, you should be putting "." after macro variables - whilst that is not required, it is good practice.

Tom
Super User Tom
Super User

You need to use an & when you want to trigger the macro processor to convert your macro variable reference to the value that it represents.

 

You do not add an & when defining the value of a macro variable in a %LET statement because that is how the %LET statement works.  The value before the equal sign is the name of the macro variable to create and the value after the equal is the value to assign to the macro variable.

 

 Also if you did add an & it would trigger the resolution of the macro variable reference and instead of defining a macro variable named LAST you would be asking the %LET statement to define a macro variable whose name is contained in the  existing macro variable named LAST.  That is actually a valid statement and useful in many situations.

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You are quite correct @Astounding, these %let statements SHOULD have semicolons after them to avoid errors. 

jklaverstijn
Rhodochrosite | Level 12

Yes a lot's been said about the semicolons and such so no need for me to do the same. But did you try yourself? I must assume you have SAS at your disposal so just suck it and see. It;s sometimes so easy to answer your own question. What did you find?

 

Regards,

- Jan.

 

PS: The correct answer is:

ERROR: Open code statement recursion detected
cons
Calcite | Level 5

Hi.

 

Thank you so much for all answers.

 

But you didnt talk about my question. Why do I not need & in %last=first? first is a value and not reference. I dont understand it.

 

Bye.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

I believe I did above?  Maybe read the SAS documentation thoroughly on macro programming, this document may help also:

http://www2.sas.com/proceedings/sugi22/CODERS/PAPER77.PDF

jklaverstijn
Rhodochrosite | Level 12

Hi,

 

There are a few issues that make it difficult to answer your question. First, do you agree that the semicolons are missing? Second, your question


 

Why do I not need & in %last=first? first is a value and not reference. I dont understand it.

 


 

refers to code that isn't in your initial example. So we must assume, again, what you actually mean. In the line

 

%let last=first

the ampersand is technically not needed. You will howewever assign the text 'first' to variable last and that may not be what you want. So it may be functionally needed. In that case you will assign the value of first to last.

 

 

My advice: be clear in what you want to achieve and what your expected results are versus your actual results. You just leave too much guesswork for those that so clearly want to help you out in what is certainly a problem at beginners level.

 

And use %PUT statements to check your results while experimenting. Simple but effective.

 

Regards,

- Jan.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 10 replies
  • 2702 views
  • 2 likes
  • 5 in conversation