Statalist


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

Re: st: Creating a variable with the highest value from two othervariables


From   Christer Thrane <[email protected]>
To   [email protected]
Subject   Re: st: Creating a variable with the highest value from two othervariables
Date   Sat, 13 Oct 2007 09:36:35 +0200

Thanks Joseph, but I can't get it to work. Can you se what is wrong in the assert-command

. sum X Y

Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
X | 364 6970.604 6315.697 100 45000
Y | 360 3978.485 4844.969 .5 50000

. list X Y if X< Y

+---------------+
| X Y |
|---------------|
30. | 2500 3000 |
34. | 7000 . |
37. | 1000 2000 |
54. | 700 1200 |
127. | 100 150 |
|---------------|
137. | 20000 50000 |
162. | 2000 . |
209. | 2000 4000 |
254. | 1000 . |
276. | 2000 3000 |
|---------------|
308. | 10000 . |
335. | 900 1090 |
441. | 30000 . |
+---------------+

. assert !missing(X) & !missing(Y)
108 contradictions in 467 observations
assertion is false
r(9);

. generate byte Zstar = cond((X >= Y), X, Y)
(108 missing values generated)

. assert Zstar == Z

. sum Zstar

Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
Zstar | 0


Thanks again,

Christer








----- Original Message ----- From: "Joseph Coveney" <[email protected]>
To: "Statalist" <[email protected]>
Sent: Sunday, October 14, 2007 12:59 AM
Subject: Re: st: Creating a variable with the highest value from two other variables



Christer Thrane wrote:

I have two income measures, X and Y. I want to create a variable, Z, that is
equal to X for those obs where X>=Y and equal to Y for those obs where Y>X.
Like this:

id X Y Z
1 12 11 12
2 10 10 10
3 10 12 12

Is there a simple way?

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

You can use -cond()-. You can nest -cond()-s to handle missing values.

See also -egen-.

Joseph Coveney

clear *
input byte id byte X byte Y byte Z
1 12 11 12
2 10 10 10
3 10 12 12
end
assert !missing(X) & !missing(Y)
generate byte Zstar = cond((X >= Y), X, Y) // Here
assert Zstar == Z
exit


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