# Re: st: tricky calculation with dozens of variables

 From Jia Xiangping To statalist@hsphsun2.harvard.edu Subject Re: st: tricky calculation with dozens of variables Date Sun, 9 Oct 2005 17:04:37 +0800

```Hi Svend,

It still doesn't work. I programed in this way(variables name are
different in my dataset, so I still use you example for convenience)

//calculated total
forvalues i=4(11)386{
local j='i'+4
replace total=total+var'i'*var'j'
}

Then Stata 8.2 informs me
'i' invalid name
r(198)

If I need to define i before run the ado above? Thanks

Joe

On 10/9/05, Svend Juul <SJ@soci.au.dk> wrote:
> Xiangping wrote:
>
> There are hundreds of variables from var4 to v389. Now I have to -gen-
> total=var4*var8+var15*var19+var26*var30...............
> The step of one multiplied pair is 4. The step between the first
> variable of one pair and the variable in front of it is 7. Can someone
> give me a hint? Or which command or commands can be used here.
> -----------------------------------
>
> Use -foreach-; see [P] foreach. I had to start by creating an
> artificial dataset; you have your own data, of course.
>
> // create artificial data with 389 variables
> clear
> set obs 10
> forvalues I = 1/389 {
>   gen var`I' = round(10*uniform())
> }
>
> // calculate -total-
> generate total=0
> forvalues I = 4(11)386 {
>   local J = `I'+4
>   replace total = total + var`I'*var`J'
> }
>
> Is that what you need?
>
> Svend
>
>
>
> ________________________________________________________
>
> Svend Juul
> Institut for Folkesundhed, Afdeling for Epidemiologi
> (Institute of Public Health, Department of Epidemiology)
> Vennelyst Boulevard 6
> DK-8000 Aarhus C,  Denmark
> Phone, work:  +45 8942 6090
> Phone, home:  +45 8693 7796
> Fax:          +45 8613 1580
> E-mail:       sj@soci.au.dk
> _________________________________________________________
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

--
Xiangping JIA

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```