DATA Step, Macro, Functions and more

Macro Variables

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

Macro Variables

Hi,

here the task:

 

%LET first=yourname

%LET last=first

 

%PUT &&&last;

 

What is the result in log?


Accepted Solutions
Solution
‎06-24-2016 02:44 AM
Super User
Super User
Posts: 7,977

Re: Macro Variables

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


All Replies
Super User
Posts: 5,515

Re: Macro Variables

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

 

Are you unable to test the code yourself?

Contributor
Posts: 29

Re: Macro Variables

Posted in reply to Astounding

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

Contributor
Posts: 29

Re: Macro Variables

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!

Super User
Super User
Posts: 7,977

Re: Macro Variables

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.

Super User
Super User
Posts: 7,065

Re: Macro Variables

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.

 

Super User
Super User
Posts: 7,977

Re: Macro Variables

Posted in reply to Astounding

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

Super Contributor
Posts: 441

Re: Macro Variables

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
Contributor
Posts: 29

Re: Macro Variables

Posted in reply to jklaverstijn

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.

Solution
‎06-24-2016 02:44 AM
Super User
Super User
Posts: 7,977

Re: Macro Variables

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

Super Contributor
Posts: 441

Re: Macro Variables

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 521 views
  • 2 likes
  • 5 in conversation