Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: FW: nested local


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: FW: nested local
Date   Fri, 1 Apr 2011 14:52:17 +0100

Oliver Jones has given positive examples of what you should do. What
remains to be explained is what you were doing wrong.

Before you entered the loop, you defined

local variables  TR_democr_y_y`j'

Now what this defines depends on what is in the local macro `j'. If
there is no such macro, your statement reduces to

local variables  TR_democr_y_y

which is not a wildcard; it is a stub. That appears to be the case
with your code.

Suppose that there had been such a macro. Say it held "1". Then your
statement would reduce to

local variables  TR_democr_y_y1

which again is not a wildcard.

In other words, if you refer to macros that don't exist, that is quite
legal, but they are interpreted as empty strings.

Nick.

On Fri, Apr 1, 2011 at 1:36 PM, Francesco Fasani
<[email protected]> wrote:

> My question is the following:
>
> I´m trying to run a loop with a local that contains a local and I don´t
> manage to make it work. I wrote a simplified version below. Basically, it
> stops at summarize where instaed of summarizeng the variable TR_democr_y_y1
> (as I would like it to do) it stops and says that "TR_democr_y_y is an
> ambiguous abbreviation (because there are five variable called in that way
> in my dataset: TR_democr_y_y1 ... TR_democr_y_y5.
>

>                                local variables  TR_democr_y_y`j'
>
>                                foreach var of local variables {
>
>                                                forvalues j=1(1)5  {
>

> display `j'
> di "TR_democr_y_y`j'"
> su TR_democr_y_y`j'
> su `var'
> }
> }
>
> I know I coudl just rewrite the loop as I did below and it woudl work. But
> the loop I am trying to run is fairly more complex and cannot be written in
> the version below.

>                                local variables  TR_democr_y_y

>                                foreach var of local variables {
>
                                                forvalues j=1(1)5  {

>                 display `j'
> di "TR_democr_y_y`j'"
> su TR_democr_y_y`j'
> su `var'`j'

> }
> }

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index