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: loop with global marco


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: loop with global marco
Date   Wed, 19 Feb 2014 12:52:37 +0000

Not so! The idea is perfectly sound.

Presumably you did something else, which you do not explain, and that
something else was wrong. Expecting comments on code you do not show
us is a bad idea.

. sysuse auto, clear
(1978 Automobile Data)

. global var1 weight length

. global var2 trunk weight

. foreach v in $var1 $var2 {
  2. su `v'
  3. }

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
      weight |        74    3019.459    777.1936       1760       4840

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
      length |        74    187.9324    22.26634        142        233

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
       trunk |        74    13.75676    4.277404          5         23

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
      weight |        74    3019.459    777.1936       1760       4840

.
Nick
[email protected]


On 19 February 2014 12:42, Nick Bornschein <[email protected]> wrote:
> Unfortunately that does not work. There is the error message: too few
> variables specified.
> When I use
> su $var1
> or any other global, it works perfectly, but not within the loop.
>
>
>
>
> Am 19.02.14 13:33, schrieb Nick Cox:
>
>> foreach x in $var1 $var2 $var3
>>
>> That way the macroname gets expanded to the macro contents and
>> -foreach- just sees the results of that, not the globals.
>> Nick
>> [email protected]
>>
>>
>> On 19 February 2014 12:23, Nick Bornschein <[email protected]>
>> wrote:
>>>
>>> Hello,
>>>
>>> I defined 3 global macros, each of them containing the names of
>>> variables:
>>> (... means there are a lot more variables in it)
>>>
>>> global var1 "hope truth..."
>>> global var2 "life death..."
>>> global var3 "never loose..."
>>>
>>> Now I want to summarize the global macros in blocks with:
>>>
>>> foreach x of global var1 var2 var3 {
>>> su `x'
>>> }
>>>
>>> But it does not work, it always runs through all variables without
>>> creating
>>> blocks. What is the problem?
>>>
>>> -Nick
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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