Announcement

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

  • Collapsing a dataset with categorical variables

    Hi all,

    I have a dataset with indiviudal-level data that I would like to collapse into household-level data. The dataset contains a dummy variable that is 1 when the individual has a disability, 0 otherwise.

    Data example (fake data, because it's confidential):

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(householdcode individualid disability_i)
    1  1234 0
    1  4566 0
    1 34653 1
    2 45321 0
    2  3564 1
    2 36433 0
    end

    I would like to have a dummy variable that is == 1 when any person in a household has a disability in the new household-level dataset.

    I used the following code. (I am using Stata 14.)
    Code:
    collapse (max) disability_i by(householdcode)
    In return, I got the error "factor variables not allowed". Is there a different command I could use in this case?

    This is my first time posting on the forum, so I hope I explained my problem well... any suggestions on how to improve are most welcome.

    Thank you!
    Last edited by rebecca dam; 06 Sep 2019, 04:01.

  • #2
    You may use - egen - with the max() function, BY householdcode to create the wished variable.
    Best regards,

    Marcos

    Comment


    • #3
      Thank you Marcos, that was very helpful!

      Comment


      • #4
        Adding to Marcos's advice, the reason your collapse command failed - with an impressively unhelpful error message! - was because you omitted the comma necessary before the option list, and Stata got very confused.
        Code:
        . collapse (max) disability_i, by(householdcode)
        
        . list, abbreviate(20)
        
             +------------------------------+
             | householdcode   disability_i |
             |------------------------------|
          1. |             1              1 |
          2. |             2              1 |
             +------------------------------+

        Comment

        Working...
        X