Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: tricky calculation with dozens of variables


From   "Svend Juul" <[email protected]>
To   <[email protected]>
Subject   Re: st: tricky calculation with dozens of variables
Date   Sat, 8 Oct 2005 22:20:45 +0200

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:       [email protected]
_________________________________________________________

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index