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: Selecting variables corresponds to observation numbers


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Selecting variables corresponds to observation numbers
Date   Tue, 8 Mar 2011 11:37:40 +0000

Something should be added to this. (Also I corrected a tiny detail in
what I quote.)

There is a difference, as downstream of (2) you have the local macros

newvar1 , newvar2, ...
newvars

and downstream of (1) you have

newvar
newvars

where this time a reference to `newvar' will evoke just the _last_
tempvar name to be assigned. (1) entails no loss as you want to loop
over `newvars' again, you can do that.

Even if you want to refer to the j th variable in `newvars' again,
there are ways of doing that.

But indeed (2) can well be better style, depending on what is done downstream.

On Tue, Mar 8, 2011 at 11:12 AM, Nick Cox <[email protected]> wrote:

>  Here's a more challenging example. Given a prior
>
> local J = ...
>
> what is the difference between
>
> (1)
>
> forval j = 1/`J' {
>        tempvar newvar
>        gen `newvar' = ...
>        local newvars `newvars' `newvar'
> }
>
> and
>
> (2)
>
> forval j = 1/`J' {
>        tempvar newvar`j'
>        gen `newvar`j'' = ...
>        local newvars `newvars' `newvar`j''
> }
>
> Answer: none in terms of the result. The first may look like a bug,
> but second time, third time ... around the loop -tempvar- is
> guaranteed to come up with a new name not already in use, so long as
> you keep track of what that new name is, you're OK.
>
> If (1) looks too tricky, don't use it. It's more for the amusement of
> the programmer.

*
*   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