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

st: Finding max value and then replace w/ code


From   "Tonya Cropper" <[email protected]>
To   "Statalist" <[email protected]>
Subject   st: Finding max value and then replace w/ code
Date   Sun, 18 Jul 2004 17:08:11 -0400

Dear Statalisters,


I have this section of code that creates a new variable that is equal to the
maximum value amongst three choices.  I then replace the value of the new
variable to equal to either one of three integers.  However, I am getting
the incorrect replacement in certain cases  - when the max is between the Lc
and the Uc variable and only when Uc is 0.  In the output below, variable
wMjIc in lines 1, 3, and 5 is incorrect.  I made sure that all four
variables are formatted the same type (double %9.0g) , but I am still
getting this error.  When I performed an assert for line1,

. assert Lc >Uc if id==2 , it responds as true, but the new value for wMjIc
should be 0 instead of 0.5.   I use this code in other analysis and I have
no problems.



Any assistance is appreciated.



Thanks,



Tonya



Here's the code:



foreach v in c wc xc {

   gen double wMjI`v' = max(D`v',L`v',U`v')

   format wMjI`v' %9.0g

   replace wMjI`v' = 1 if wMjI`v'==D`v'

   replace wMjI`v' = 0 if wMjI`v'==L`v'

   replace wMjI`v' = 0.5 if wMjI`v'==U`v'

}



Here's an excerpt of the output before the replacement.

     |   id          Dc         Lc         Uc      wMjIc |

     |---------------------------------------------------|

  1. |     2   .4292084   1.381115          0   1.381115 |

  2. |     3   1.219148   1.172775   .8614816   1.219148 |

  3. |     5          0   1.858415          0   1.858415 |

  4. |     6          0   1.021858   .4441429   1.021858 |

  5. |     7   .9105099   1.041191          0   1.041191 |



Here's an excerpt of the output after the replacement.



     |    id         Dc         Lc         Uc   wMjIc |

     |------------------------------------------------|

  1. |     2   .4292084   1.381115          0      .5 |

  2. |     3   1.219148   1.172775   .8614816       1 |

  3. |     5          0   1.858415          0      .5 |

  4. |     6          0   1.021858   .4441429       0 |

  5. |     7   .9105099   1.041191          0      .5 |





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