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: the use of foreach command in Stata


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: the use of foreach command in Stata
Date   Thu, 15 Mar 2012 01:36:44 +0000

I already answered this; see

http://www.stata.com/statalist/archive/2012-03/msg00544.html

Once again: My answer implied combining code, like this

foreach var of varlist lp* {
     histogram `var',  by(AID4, col(1))
    graph save histogram`var'
}

To see a slide show, not a movie, do this [new this posting]

foreach var of varlist lp* {
     histogram `var',  by(AID4, col(1))
     more
    graph save histogram`var'
}

Incidentally, the Statalist archive shows that you started this thread
by replying to a previous thread. See here to see that you should not
do that:

http://www.stata.com/support/faqs/res/statalist.html#toask

Specifically, please note the last sentence.

Nick

On Thu, Mar 15, 2012 at 1:23 AM, Rosie Chen <[email protected]> wrote:
>
> Nick, sorry that I didn't see your response to Till earlier. If using `var' instead of lname is ok, then I guess my question is:  how can I save all the histogram graphs and track back all the graphs? Stata only showed the last graph to me. Thank you,
>
> ________________________________
> From: Nick Cox <[email protected]>
> To: [email protected]
> Sent: Tuesday, March 13, 2012 3:13 PM
> Subject: Re: st: the use of loop function in Stata
>
> As before I recommend against using the term "loop function". You are
> using the -foreach- command. It's not a function (Stata sense).
> "Looping with -foreach- " also makes sense.
>
> The reason this doesn't work is that "lnamelist" is not a keyword
> recognised by -foreach-: look again at the help.
>
> Till actually recommended something different from what you typed:
>
> foreach lname in lnamelist
>
> but that is also illegal, except in very special circumstances, a loop
> with just one literal name "lnamelist". I didn't spot in my earlier
> reply that Till's suggestion was illegal (except in very special
> circumstances).
>
> Nick
>
> On Tue, Mar 13, 2012 at 6:50 PM, Rosie Chen <[email protected]> wrote:
>> Thank you, Steve and Till. Below is the revised syntax, but I got an error message
> of "variable lnamelist not
> found".  Isn't lp* the list? This is the first time I use the loop function, so any suggestion would be appreciated.
>>
>> foreach lname in lnamelist lp*{
>> histogram `lname',  by(AI4, col(1))
>> graph save histogram`lname', replace
>> }
>>
>>
>> ----- Original Message -----
>> From: Steve Nakoneshny <[email protected]>
>> To: "[email protected]" <[email protected]>
>> Cc:
>> Sent: Tuesday, March 13, 2012 11:28 AM
>> Subject: Re: st: the use of loop function in
> Stata
>>
>> You will also need to add the -replace- option to the histogram
> command.
>>
>> On 2012-03-13, at 8:44 AM, Dr. Till Ittermann wrote:
>>
>>> Hi,
>>>
>>> I would loop over lname not var:
>>>
>>> foreach lname in lnamelist {
>>> histogram `lname',  by(AID4, col(1))
>>> graph save histogram`lname'
>>> }
>>>
>>>
>>>
>>> m 13.03.2012 15:36, schrieb Rosie Chen:
>>>>
> Nick, thank you very much for the further advice, which makes the
> syntax more efficient. Given the loop syntax I have, how to save all the
> histogram graphs? If I use the code below, the error message says:
> "file histogram.gph already exists".
>>>>
>>>>
>>>> foreach var of varlist lp*{
>>>>
>>>>     histogram `var',  by(AID4, col(1))
>>>>
>>>>     graph save histogram
>>>> }

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