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

st: RE: Finding max value and then replace w/ code


From   "Tonya Cropper" <[email protected]>
To   "Statalist" <[email protected]>
Subject   st: RE: Finding max value and then replace w/ code
Date   Mon, 19 Jul 2004 08:40:35 -0400

Thanks a lot Scott for the suggestion.

You inverted the expression and true/false result in your cond command.  But
once I reversed it, it worked fine.

gen w = cond(1, max(d, l, u) == d,  cond(0, max(d,l,u) == l, .5)) should be

gen w = cond(max(d, l, u) == d,  1, cond(max(d,l,u) == l, 0, .5))


      From   "Scott Merryman" <[email protected]>
      To   <[email protected]>
      Subject   st: RE: Finding max value and then replace w/ code
      Date   Sun, 18 Jul 2004 18:45:19 -0500

----------------------------------------------------------------------------
----

The problem is that after the value of wMjIc is replaced with 0 it then
becomes equal to the value of uc, and so it is replace with 0.5.

It might be easier to use a nested cond() function:


gen w = cond(1, max(d, l, u) == d, ///
	  cond(0, max(d,l,u) == l, .5))

When max(d,l,u) == d is true then w = 1; if the condition is false it is
evaluated again against l.  If this is true then w = 0; if it false then w =
0.5.

Example:


     +-------------------------------------+
     | id         dc         lc         uc |
     |-------------------------------------|
  1. |  2   .4292084   1.381115          0 |
  2. |  3   1.219148   1.172775   .8614816 |
  3. |  5          0   1.858415          0 |
  4. |  6          0   1.021858   .4441429 |
  5. |  7   .9105099   1.041191          0 |
     +-------------------------------------+

. gen w = cond(1, max(d, l, u) == d,  cond(0, max(d,l,u) == l, .5))

. l

     +-----------------------------------------+
     | id         dc         lc         uc   w |
     |-----------------------------------------|
  1. |  2   .4292084   1.381115          0   0 |
  2. |  3   1.219148   1.172775   .8614816   1 |
  3. |  5          0   1.858415          0   0 |
  4. |  6          0   1.021858   .4441429   0 |
  5. |  7   .9105099   1.041191          0   0 |
     +-----------------------------------------+


Hope this helps,
Scott

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Tonya Cropper
Sent: Sunday, July 18, 2004 4:08 PM
To: Statalist
Subject: st: Finding max value and then replace w/ code

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/

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