Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • generate variable

    hello everyone,
    please I need help.
    I have a data set with a variable finasset_ type (coded 1-5) under which five different financial asset types are repeatedly listed for every household member. respondents answered if they have each particular financial asset type under a variable 's5bq02' (coded 1 if yes and 2 if No) . how do I generate a binary variable that will indicate 1 if the respondent owns a financial asset and 0 if not, irrespective of the type of financial asset? I just want the variable to take 1 if respondents own any of the types of assets.
    Here is the dataex

    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str18 household_id double(individual_id asset_type) str15 ea_id double(saq14 pw_w4 saq01) str2(saq02 saq03) double(saq04 saq05) str3 saq06 str2 saq07 double(saq08 s5bq01) str19 s5bq01_os double s5bq02
    "010101088800910007" 1 1 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 1 2 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 1 3 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 1 4 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 1 5 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 2 1 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 2 2 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 2 3 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 2 4 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 2 5 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 3 1 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 3 2 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 3 3 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 3 4 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910007" 3 5 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 7 2 "" .
    "010101088800910017" 1 1 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 17 2 "" .
    "010101088800910017" 1 2 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 17 1 "" 1
    "010101088800910017" 1 3 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 17 2 "" .
    "010101088800910017" 1 4 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 17 2 "" .
    "010101088800910017" 1 5 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 17 2 "" .
    "010101088800910026" 1 1 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 26 2 "" .
    "010101088800910026" 1 2 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 26 2 "" .
    "010101088800910026" 1 3 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 26 2 "" .
    "010101088800910026" 1 4 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 26 2 "" .
    "010101088800910026" 1 5 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 26 2 "" .
    "010101088800910026" 8 1 "010101088800910" 1 1658.475108571429 1 "01" "01" 8 88 "009" "10" 26 2 "" .
    --more--




  • #2
    This sounds like

    Code:
    bysort indvidual_id (s5bq02) : gen wanted = s5bq02[1] == 1
    The values possible for s5bq02 are 1 and 2 (as you say) and missing (as the data example shows).

    If you sort on that variable within individuals, any values of 1 will be sorted before any values of 2 and those in turn before missings.

    Therefore if any value of 1 is present for each individual, then one such value will be sorted to first position and an indicator variable can be created directly.

    See also https://www.stata.com/support/faqs/d...rue-and-false/ if you are new to the idea that evaluating a true-or-false expression yields 1 if true and 0 if false.

    Comment


    • #3
      Thank you very much. I will try this

      Comment

      Working...
      X