# RE : st: Loop with forvalues ..

 From To Subject RE : st: Loop with forvalues .. Date Tue, 7 Nov 2006 07:42:48 +0100

```Thank you for your help, but the number of measurement is different from
patient to patient. So I need to get the maximum value of measurement:

egen max_mes=max(nb_mes)

And then use

forvalues i=1/`max_mes' {

Is there a way to do that ?

I try

local n=max_mes
forvalues i=1/`n' {

But it doesn't work

Thank you

-----Message d'origine-----
De : owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] De la part de Svend Juul
Envoyé : 06 November 2006 17:43
À : statalist@hsphsun2.harvard.edu
Objet : Re: st: Loop with forvalues ..

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

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