Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Create a new variable based on some other variables


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   Re: st: Create a new variable based on some other variables
Date   Thu, 21 May 2009 13:10:35 +0200

<>

We have at least four alternatives here, and Charles can look at the result based on my random dataset and make his pick according to his wishes. If he needs a bigger sample, he can increase the # of observations in row 2. If he wants a different one, he can kill the -set seed- command...

***
clear*
set obs 50

local a 1
local b 8
set seed 12345

forv i=1/3{
gen A`i'=`a'+/*
*/int((`b'-`a'+1)*runiform())

}

compress
list, noobs

//Martin
gen B=(A1==1)*1
replace B=(A2==3)*2
replace B=(A3==8)*3


gen Balt=cond(A1 == 1, 1, /*
*/cond(A2 == 3, 2, /*
*/cond(A3 == 8, 3, 0)/*
*/))

//Martin
gen Balt2=(A1==1)*1 + (A2==3)*2/*
*/+(A3==8)*3

//Eric`s recommendation
gen Balt3=1 if A1==1
replace Balt3=2 if A2==3
replace Balt3=3 if A3==8

list, noo h(25)
***

HTH
Martin
_______________________
----- Original Message ----- From: "Nick Cox" <[email protected]>
To: <[email protected]>
Sent: Thursday, May 21, 2009 12:39 PM
Subject: RE: st: Create a new variable based on some other variables


Let's spell out that there is a difference between

gen B = cond(A1 == 1, 1, cond(A2 == 3, 2, cond(A3 == 8, 3, 0)))

and

gen B=(A1==1)*1 + (A2==3)*2+(A3==8)*3

The order in -cond()- is crucial. Once for example observations with A1
== 1 are dealt with, they are not revisited, regardless of whether A2 ==
3 or A3 == 8 in the same observation.

Nick
[email protected]


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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