Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: loop for graphs


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: loop for graphs
Date   Fri, 8 Jul 2011 09:39:23 +0200

On Fri, Jul 8, 2011 at 5:43 AM, Kamarul Imran wrote:
>I have 3 dependent variables (a1,b1,c1) that i want plot
> against 1 var (x). i will be using scatter plot and  hope to use these
> commands
>
> foreach var of varlist a1-c1 {
> graph twoway (scatter `var' x ) (lfit `var' x), saving(gr.gph)
> }

For each iteration of the loop you are using the same name to store
your graph, thus overwriting the previous graph. So at the end of the
loop you are left with only the last graph. If you change the name
during each iteration, you'll get what you want. A convenient way is
to use the local `var', as that will already change for each iteration
and later it will be easier for you to remember which file contains
which graph just by looking at the filenames (assuming you chose more
informative variable names than a1 b1 c1...). So the example below
should do what you want. (I use the -name()- option instead of the
-saving()- option as I do not wish to store these graphs on my
computer. You can of course change that.)

*------------- begin example --------------
sysuse nlsw88, clear
foreach var of varlist wage hours ttl_exp {
    twoway scatter `var' grade || ///
           lfit `var' grade,      ///
           name(gr`var', replace)
}
*--------------- end example --------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany


http://www.maartenbuis.nl
--------------------------

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index