Announcement

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

  • Create dummy variable per group conditional on two other variables

    Good afternoon,

    I am currently working on my data for my master's thesis but I am having some obstacles.
    Below I provide a small sample of my dataset.
    I would like to know how can I create a dummy variable that equals 1 if both contact and peer have the same sex (no matter if sex =1 or 0). In my dataset, I have 262 villages, and each of them has 4 peers and 1 contact. However, the sequence peer-contact does not follow any logic.
    Village Sex What I need:
    Peer 1 1 1 (since this peer and contact of village 1 has the same sex)
    Peer 1 1 1
    Peer 1 0 0 (since contact in village 1 has sex=1, but this peer has sex=0)
    Contact 1 1 1
    Peer 1 0 0
    Peer 2 0 1 (since this peer and contact of village 2 has the same sex)
    Contact 2 0 1
    Peer 2 1 0 (since contact in village 1 has sex=0, but this peer has sex=1)
    Peer 2 1 0
    Peer 2 0 1
    I would be so grateful if someone can help me with this. I have been struggling for a while but I haven't found the way. ( have Stata16)

    Thank you in advance,
    Maria
    Last edited by Maria Domingo; 20 Apr 2020, 07:43. Reason: dummy, conditional, groups

  • #2
    Welcome to Statalist.

    Starting with your example data, the following seems to do what you want.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 type byte(village sex need)
    "Peer"    1 1 1
    "Peer"    1 1 1
    "Peer"    1 0 0
    "Contact" 1 1 1
    "Peer"    1 0 0
    "Peer"    2 0 1
    "Contact" 2 0 1
    "Peer"    2 1 0
    "Peer"    2 1 0
    "Peer"    2 0 1
    end
    generate order = _n
    sort village type
    by village: assert (type=="Contact" & _n==1) | (type=="Peer" & _n>1)
    by village: generate wanted = sex==sex[1]
    // put observations back in the original order they were in
    sort order
    drop order
    list, sepby(village)
    Code:
    . list, sepby(village) 
    
         +-----------------------------------------+
         |    type   village   sex   need   wanted |
         |-----------------------------------------|
      1. |    Peer         1     1      1        1 |
      2. |    Peer         1     1      1        1 |
      3. |    Peer         1     0      0        0 |
      4. | Contact         1     1      1        1 |
      5. |    Peer         1     0      0        0 |
         |-----------------------------------------|
      6. |    Peer         2     0      1        1 |
      7. | Contact         2     0      1        1 |
      8. |    Peer         2     1      0        0 |
      9. |    Peer         2     1      0        0 |
     10. |    Peer         2     0      1        1 |
         +-----------------------------------------+

    Comment


    • #3
      William Lisowski Thanks a lot for your reply. I will give it a try.

      However, I should have mentioned that Contact and Peer have already values =1 for Contacts, and =0 for Peers. Does it change the code?

      Comment


      • #4
        Village Sex
        Peer 1 1 1 (since this peer and contact of village 1 has the same sex)
        Peer 1 1 1
        Peer 1 0 0 (since contact in village 1 has sex=1, but this peer has sex=0)
        Contact 1 1 ·
        Peer 1 0 0
        Peer 2 0 1 (since this peer and contact of village 2 has the same sex)
        Contact 2 0 ·
        Peer 2 1 0 (since contact in village 1 has sex=0, but this peer has sex=1)
        Peer 2 1 0
        Peer 2 0 1

        Sorry, I have another question. How can I replace the value for "contact" in the new dummy (right column) for · ?

        Thanks a lot.
        Maria

        Comment


        • #5
          This is a reply to post #3, it crossed with post #4.

          Yes, because Contact will sort to the end of the village rather than to the beginning ("Contact" < "Peer" but 1 > 0). We'll use the gsort command so that type can be sorted in descending order - first the one, then the zeroes.
          Code:
          gsort village -type
          For post #4, use the replace command?
          Code:
          replace wanted = . if type==1
          A piece of advice for you as apparently a new user of Stata.

          I'm sympathetic to you as a new user of Stata - it's a lot to absorb. But the replace command is among the most basic of Stata commands, so it appears you have not yet mastered the basics.

          When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

          All of these manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals, rather than have to wait for someone on Statalist to come to my assistance.

          Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

          Last edited by William Lisowski; 20 Apr 2020, 13:25.

          Comment


          • #6
            Thanks a lot for your reply Willia, you helped me a lot!

            You are right, I am a new user of Stata, but I am trying my best to learn as fast as possible! I will try the manuals you suggest.

            Comment

            Working...
            X