Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Loop with forvalues ..


From   "Svend Juul" <[email protected]>
To   <[email protected]>
Subject   Re: st: Loop with forvalues ..
Date   Mon, 6 Nov 2006 17:43:17 +0100

Alvine wrote:
 
I have a date set with 1 line per patient, and many measures by patient
(mes1, mes2, mes3, ...). The number of measurement is given by the nb_mes
variable.
 
id mes1 mes2 mes3  mes4 nb_mes
1  30  21  25  46  4
2  26  24    2
3  14  15  13   3
 
For each patient, I would like to calculate the difference :
 mes2-mes1, mes3-mes, ..., until : mes`nb_mes'-mes1
 
I've tried this:
 
forvalues i=2/`nb_dos' {
local im1=`i'-1
gen diff`im1'=mes`i'-mes1
}

------------------------------------------------------
 
What is nb_dos? Do you mean nb_mes?
 
Even `nb_mes' does not work; nb_mes is a variable, and you can 
not treat it as a local macro which is a kind of constant.
You must know something about the maximum number of measurements
(below I assume 4), so just:
 
   clear
   input id mes1 mes2 mes3 mes4
   1 30 21 25 46
   2 26 24  .  .
   3 14 15 13  .
   end
 
   forvalues i=2/4 {
      gen diff`i'=mes`i'-mes1
   }
 
   list, clean
       id   mes1   mes2   mes3   mes4   diff2   diff3   diff4  
  1.    1     30     21     25     46      -9      -5      16  
  2.    2     26     24      .      .      -2       .       .  
  3.    3     14     15     13      .       1      -1       .  
 
(I suggest that you name the difference mes7-mes1 diff7, not diff6)
 
Hope this helps
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