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

# Re: st: RE: Loops and counter

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Loops and counter Date Fri, 22 Mar 2013 19:08:37 +0000

```I agree with Sarah's general advice.

I also note that the factor 365.24/365.24 surely cancels to 1.

Nick

On Fri, Mar 22, 2013 at 6:09 PM, Sarah Edgington <sedging@ucla.edu> wrote:
> I think you're looking for something along the lines of :
>         gen total=0
>         forvalues i=1/14 {
>                 replace total=total+a`i'*b`i'*365.24
>         }
>
>         replace total=total/(z*365.24)
>
> If you find yourself doing repetitive calculations often, you would likely be well-served by carefully reading the -forvalues- and -foreach- manual entries carefully.
>
> -Sarah
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Thomas Speidel
> Sent: Friday, March 22, 2013 10:57 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: Loops and counter
>
> 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.
*
*   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/
```