Statalist


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

Re: st: how to create dummy for unique values across variables


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: how to create dummy for unique values across variables
Date   Mon, 22 Oct 2007 13:03:58 +0100 (BST)

--- Ana Gabriela Guerrero Serdan <[email protected]> wrote:
> I have data on victimes. The weapons used are 
> reported. Var cau*1 refers to the type of 
> aggression and cau1* to the weapon used. "-" means
> no information. 
> 
> For some victimes there are two or more types of
> aggression reported as well as type of weapon, up to
> nine types. Im only including a few below. 
> 
>  Im trying to isolate cases in which only one weapon
> is being used by creating a variable (dummy) that will
> indicate that that victim was only agressed by only
> that type of weapon. 

You can use logical operators here if you know that a 
true statement is given the value 1 and a false 
statement the value zero. So if you create a new 
variable using:
gen weapon1 = cau12 != "-"

and realize that cau12 != "-" is a logical statement
saying: "cau12 is not equal to -", than you can see 
that the new variable weapon1 will contain a 1 if 
cau12 contains a weapon and a 0 if no information.

This way you can make weapon2, weapon3, etc. The 
sum of weapon1 till weapon9 is the total number of 
weapons used. If this variable is called nweapons, 
you can make the dummy using:
gen singleweapon = nweapons == 1

see the example below:

*----------------------- begin example -----------------
drop _all
input long victim   ///
      str5 incident /// 
      str6 cau11    ///
      str7 cau12    ///
      str6 cau21    ///
      str7 cau22    ///
      str6 cau31    ///
      str7 cau32 
49711   d0096   Ground   Gunfire   Ground  Gunfire  Ground  Knife
49713   d0097   Other       -      Other      -      -        -
49712   d0097   -           -        -        -      -        -
49714   d0097   Ground   Gunfire     -        -      -        -
49595   d0098   Ground   Bombs       -        -      -        -
49596   d0098   Ground   Bombs       -        -      -        -
49599   d0099   Ground   Gunfire     -        -      -        -
49598   d0099   Ground   Gunfire     -        -      -        -
49597   d0099   Ground   Gunfire     -        -      -        -
49602   d0100   Ground   Gunfire   Ground   Bombs    -        -
49603   d0100   Ground   Gunfire   Ground   Bombs    -        -
49601   d0100   Ground   Gunfire   Ground   Bombs   Ground   Nuke
end

gen nweapons = 0
forvalues i = 1/3 {
	gen weapon`i' = cau`i'2 != "-"
	replace nweapons = nweapons + weapon`i'
}
gen byte singleweapon = nweapons == 1
tab singleweapon nweapons
*------------------------ end example ---------------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten


-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      ___________________________________________________________ 
Want ideas for reducing your carbon footprint? Visit Yahoo! For Good  http://uk.promotions.yahoo.com/forgood/environment.html
*
*   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