Announcement

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

  • How do i tell stata to pick two categories of a variable given another category of another to create a new var.

    Good day,

    I am using a survey sample file and i am trying to create a new variable X=1 for each household (HID) that has two observations of PATNR=1, as one observation of PATNR indicates a single parent household.

    Code:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 ABSHID float PATNR
    "CSF11B00000001" .
    "CSF11B00000001" .
    "CSF11B00000002" 1
    "CSF11B00000002" 1
    "CSF11B00000003" .
    "CSF11B00000003" .
    "CSF11B00000003" .
    "CSF11B00000004" .
    "CSF11B00000005" .
    "CSF11B00000006" 1
    "CSF11B00000006" .
    end
    I have tried several egen commands but i keep getting the error code.

    Code:
    unknown egen function 1()
    r(133);

    Can anyone please help with suggestions?

    Many thanks.
    Sunganani Kalemba
    PhD Student.
    Queensland

  • #2
    Code:
    bys ABSHID: egen temp= total(PATNR)
    gen X=1 if temp==2

    Comment


    • #3
      Jorrit,

      Thanks for your response, i have tried your suggestion that but as you can see from the output, i am not getting a unique variable of X per ABSHID.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str14 ABSHID float(PATNR temp X)
      "CSF11B00000008" 1 2 1
      "CSF11B00000008" 1 2 1
      "CSF11B00000008" . 2 1
      "CSF11B00000009" 1 2 1
      "CSF11B00000009" 1 2 1
      "CSF11B00000010" . 0 .
      "CSF11B00000011" 1 2 1
      "CSF11B00000011" 1 2 1
      "CSF11B00000011" . 2 1
      "CSF11B00000011" . 2 1
      "CSF11B00000011" . 2 1
      end
      Sunganani Kalemba
      PhD Student.
      Queensland

      Comment


      • #4
        Please provide an example of what the variable X should contain for the sample data you provide.
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Carole,

          The ABSHID are households the X in this case should be a var that represents a household with 2 counts of PATNR.
          Sunganani Kalemba
          PhD Student.
          Queensland

          Comment


          • #6
            The output looks exactly like what I understood you wanted to have, also with your new data example and further explanation.
            Please do as Carole advises and provide an example output of the way you want to have your data.

            Comment


            • #7
              Jorrit,

              I was confused in thinking that i should create a separate variable for the observation per household. But this is usable too.

              I want to thank you for your help and quick turn around. Apologies for the confusion.

              Best regards.
              Sunganani Kalemba
              PhD Student.
              Queensland

              Comment

              Working...
              X