12-05-2017 11:56 AM
help request in indirect macro variable references?
Please correct my understnding:
&&city&n would resolves to &city1--> this resolves to london. Ok.
Looping the n one by one will get the above city names. Fine
so I suppose && resolves to &
What baffles me is use of more than && ampersands and resolving many other vars with multiple ampersands(&&&&) I vaguely remember reading an example on SAS documentation something dog resolves to cat and cat resolves to monkey or something like that. Should any experts lend their time in helping me understand this concept, I will most appreciate it.
12-05-2017 12:03 PM
The simple answer to that is this: Anymore than one ampersand in a line of code = badly planned/implemented coding = delete button.
What you have to do with such code, if you can't just delete it, is to go through a loop yourself, take your example:
First loop, replace &n with the variable text and demote double & to single:
The same can be said about more:
Still, never a need to go there.
12-05-2017 01:36 PM - edited 12-05-2017 02:01 PM
You have the first piece, right ... && resolves into &
Next, you have to realize that macro language resolves strings from left to right, resolving each piece that it finds. So:
&&city&n gets resolved as three pieces: && resolves to &, then city requires no resolution, then &n resolves to 1 (or 2 or 3).
So as you noted, &&city&n resolves to &city1 which then gets re-resolved into london.
For &&&, the best use I can think of occurs when calling a macro:
%let my_list = some list of a thousand variable names;
If such a macro variable exists with a thousand names, the macro could be called in a few ways:
%macro_call (list=some list of a thousand variable names)
The first two macro calls need to compile a long list of values ... slightly longer than the third macro call which only compiles a single word. Now within %MACRO_CALL, the first two macro calls can refer to &LIST to get the full list of variable names. But the third macro call, while it compiles faster, has to refer to a more complex expression to get the full list of values: &&&list
This resolves in two pieces: && becomes &, and &list becomes my_list. So the expression resolves into &my_list, which then gets re-resolved into the longer list of values.
We have even seen a recent question posted on the board here that required &&&&&&, but more than && is infrequent.
12-06-2017 08:08 AM
It is a little complicated problem. Just as @Reeza said put on as many & as you could , you will get right answer.