Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Re: syntax help
From
"Joseph Coveney" <[email protected]>
To
<[email protected]>
Subject
st: Re: syntax help
Date
Thu, 17 Nov 2011 11:35:40 +0900
Dale S Hardy wrote:
Dear Stata Users,
I am kindly asking for your help. I am defining a macro.
In this macro I would like to calculate a variable called glycemic load for each
subject-1540 subjects.
The formula is:
total gload=Sum[GI of food * Carbohydrate (gms) of food * frequency of food
eaten ]
I have 43 variables for each specific food (GI, Carbohydrate, frequency). Each
food has its corresponding GI, Carbohydrate, and frequency.
I want to calculate the gload of each food and sum the gloads of all foods to
make a summary gload value for each subject.
[loops and trace omitted]
--------------------------------------------------------------------------------
You don't mention your dataset layout, but from the code it looks like your
dataset has one study participant per observation (row), that there are
variables for for gi, cho and freq for each of the 43 foods, and that these
variables are indexed by a suffix, 1 to 43. So, how about something like the
following?
generate double gload = 0
quietly forvalues food = 1/43 {
replace gload = gload + gi`food' * cho`food' * freq`food'
}
Joseph Coveney
*
* 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/