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: Loops and counter


From   George Vega Yon <g.vegayon@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Loops and counter
Date   Fri, 22 Mar 2013 15:03:29 -0300

gen total = a1 * b1 * 365.24
forval i = 2/14 {
    replace total = total + a`i' * b`i' * 365.24
}
replace total = total/(z * 365.24)

Cheers!

George Vega Yon
7 647 2552
http://cl.linkedin.com/in/georgevegayon


2013/3/22 Thomas Speidel <thomas@tmbx.com>:
> I am trying to shorten the following expression in Stata:
>
> gen total = ((a1 * b1 * 365.24) + (a2 * b2 * 365.24) + ... +  (a14 * b14 *
> 365.24)) / (z * 365.24)
>
> Where a1-a14, b1-b14, z are all continuous variables.
> Typing the expression in full is time consuming. I seem to recall I should
> be using a loop with a counter to cycle through every value 1-14. However, I
> am not sure how to do that.
>
> Thanks.
>
> --
> Thomas Speidel
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index