I have data on ownership of household assets. I want to generate variables where logical operators '&' and '|' are used many times. Below is the sample data set and the codes that I used to create them.
I want to make sure if the codes are correct and is there any mistake? I want to create four variables, namely
1. whether a household owns "at least one asset1, asset2 or asset3"
2. whether a household owns"one asset5 OR one asset5 with one asset4"
3. whether a household owns"one asset4 BUT no asset5"
4. whether a household owns"no asset6 OR asset7 BUT at lease one asset8, asset9 or asset10"
Variables asset* indicate whether a household own specific asset or not, where 1 "yes" and 2 "no".
Variables q_* indicate how many of that specific asset household owns.
Code:
clear input float hhid int asset1 q_asset1 asset2 q_asset2 asset3 q_asset3 asset4 q_asset4 /// asset5 q_asset5 asset6 q_asset6 asset7 q_asset7 asset8 q_asset8 asset9 q_asset9 /// asset10 q_asset10 101000 2 . 2 . 1 2 2 . 2 . 2 . 1 1 2 . 2 . 2 . 102000 2 . 2 . 2 . 2 . 2 . 2 . 2 . 2 . 2 . 2 . 103000 1 1 2 . 1 1 2 . 2 . 1 5 2 . 2 . 2 . 1 6 104000 2 . 1 2 2 . 2 . 2 . 2 . 2 . 2 . 1 3 2 . 105000 2 . 1 1 2 . 2 . 2 . 1 1 2 . 2 . 1 1 2 . 106000 1 2 2 . 1 1 2 . 1 1 2 . 2 . 1 2 2 . 2 . 107000 1 5 2 . 1 5 1 1 2 . 2 . 2 . 1 8 1 4 2 . 108000 2 . 1 3 1 1 1 1 2 . 2 . 1 2 2 . 2 . 1 1 109000 2 . 2 . 2 . 1 1 1 1 2 . 1 2 2 . 2 . 1 1 110000 2 . 2 . 1 2 2 . 2 . 1 5 2 . 2 . 2 . 2 . 111000 2 . 2 . 1 2 2 . 2 . 1 1 2 . 2 . 2 . 2 . 112000 2 . 2 . 1 2 2 . 2 . 2 . 2 . 2 . 2 . 1 4 end gen assets1to3=0 bys hhid: replace assets1to3=1 if (q_asset1>=1 & q_asset1<.) | (q_asset2>=1 & q_asset2<.) /// | (q_asset3>=1 & q_asset3<.) label var assets1to3 "at least one asset1, asset2 or asset3" gen asset4to5=0 bys hhid: replace asset4to5=1 if q_asset5==1 | (q_asset5==1 & q_asset4==1) label var asset4to5 "one asset5 or one asset5 with one asset4" gen noasset5=0 by hhid: replace noasset5=1 if (q_asset4==1 & asset5==2) label var noasset5 "one asset4 but no asset5" gen asset6to10=0 by hhid: replace asset6to10=1 if (asset6==2 | asset7==2) & (q_asset8>=1 & q_asset8<.) | /// (q_asset9>=1 & q_asset9<.) | (q_asset10>=1 & q_asset10<.) label var asset6to10 "no asset6 or asset7 but at lease one asset8, asset9 or asset10"
0 Response to generating variable with logical operators
Post a Comment