Bookmark and Share

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: syntax help


From   "Hardy, Dale S" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: syntax help
Date   Wed, 16 Nov 2011 20:12:47 -0600

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.



Below is my syntax and the feedback I got from stata when I -set trace on-

SYNTAX
local numsubjects=_N              //record number

.forvalues i=1(1)`numsubjects'{
 forvalues i=1(1)1541 { 
 local sum=0
 forvalues sum=1(1)43 {
 local j="gi1-gi43 cho1-cho43 freq1-freq43"
 forvalues j=1(1)43 { 
 foreach var  of varlist gi1-gi43 {
 foreach x of varlist cho1-cho43 {
 foreach f of varlist freq1-freq43 {
   
. local sum= (`var'`j'[`i'] * `x'`j'[`i'] * `f'`j'[`i']) + `sum' 
  }
 }
 }
 }
 replace gload=`sum' in `i'
 }
 }
 }


SET TRACE ON
- forvalues i=1(1)`numsubjects'{
= forvalues i=1(1)1541{
- forvalues i=1(1)1541 {
- local sum=0
- forvalues sum=1(1)43 {
- local j="gi1-gi43 cho1-cho43 freq1-freq43"
- forvalues j=1(1)43 {
- foreach var of varlist gi1-gi43 {
- foreach x of varlist cho1-cho43 {
- foreach f of varlist freq1-freq43 {
- local sum= (`var'`j'[`i'] * `x'`j'[`i'] * `f'`j'[`i']) + `sum'
= local sum= (gi11[1] * cho11[1] * freq11[1]) + 1
- }
- local sum= (`var'`j'[`i'] * `x'`j'[`i'] * `f'`j'[`i']) + `sum'
= local sum= (gi11[1] * cho11[1] * freq21[1]) + 1
- }
- local sum= (`var'`j'[`i'] * `x'`j'[`i'] * `f'`j'[`i']) + `sum'
= local sum= (gi11[1] * cho11[1] * freq31[1]) + 1
- }
- local sum= (`var'`j'[`i'] * `x'`j'[`i'] * `f'`j'[`i']) + `sum'
= local sum= (gi11[1] * cho11[1] * freq41[1]) + 1
- }
- local sum= (`var'`j'[`i'] * `x'`j'[`i'] * `f'`j'[`i']) + `sum'
= local sum= (gi11[1] * cho11[1] * freq51[1]) + 1
freq51 not found
  }
  }
  }
  }
  replace gload=`sum' in `i'
  }
  }
  }
r(111);

end of do-file

r(111);


I appreciate all the help you can give me. 

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index