Announcement

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

  • Generating new variable with same value to everyone withing group

    Hi, I have a family database similar to this one: https://www.stata.com/support/faqs/d...ng-properties/
    While the examples were useful to me and, with the help from another thread, I was able to do what I wanted, I was hoping there was a simpler way to do this.

    My data looks like this:
    Family Person Sex Age
    1 1 1 45
    1 2 0 43
    1 3 1 17
    2 1 1 57
    2 2 0 49
    3 1 1 50
    3 2 0 45
    3 3 0 15
    3 4 1 20
    and I want to create two variables: Father_Age & Father_Sex
    which will take the Sex and Age from the person 1 from each family for every member.

    I was able to do this with this code:

    Code:
    bysort Family: gen Father_Age = Age if Person == 1
    bysort Family: replace Father_Age = Father_Age[1]
    
    bysort Family: gen Father_Sex = Sex if Person == 1
    bysort Family: replace Father_Sex = Father_Sex[1]
    but I was hoping to find a way to achieve this with one line per variable. Thanks!

  • #2
    Code:
    bysort Family (Person): gen Father_Age = Age[1]
    bysort Family (Person): gen Father_Sex = Sex[1]
    Also, please refer to the FAQ (http://www.statalist.org/forums/help) and follow it to post your data using -dataex-. Table form is not very convenient because we cannot read that into Stata quickly and test our codes.

    Comment


    • #3
      Here's another approach noting that calling such variable "father" is problematic in various senses.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(family person sex age)
      1 1 1 45
      1 2 0 43
      1 3 1 17
      2 1 1 57
      2 2 0 49
      3 1 1 50
      3 2 0 45
      3 3 0 15
      3 4 1 20
      end
      
      egen father_age = total((person == 1) * age), by(family)
      
      egen father_sex = total((person == 1) * sex, by(family)
      
      egen father_count = total(person == 1), by(family)
      As a data check, I added a variable counting the number of such persons in each family.

      For suggestions in similar style, check out https://www.stata-journal.com/articl...article=dm0055

      Comment


      • #4
        Originally posted by Nick Cox View Post
        Here's another approach noting that calling such variable "father" is problematic in various senses.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte(family person sex age)
        1 1 1 45
        1 2 0 43
        1 3 1 17
        2 1 1 57
        2 2 0 49
        3 1 1 50
        3 2 0 45
        3 3 0 15
        3 4 1 20
        end
        
        egen father_age = total((person == 1) * age), by(family)
        
        egen father_sex = total((person == 1) * sex, by(family)
        
        egen father_count = total(person == 1), by(family)
        As a data check, I added a variable counting the number of such persons in each family.

        For suggestions in similar style, check out https://www.stata-journal.com/articl...article=dm0055
        What senses would those be? Just curious. My real variables have different names

        Comment

        Working...
        X