Announcement

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

  • generating variables using min or max as conditionals

    Hello stata users

    Im trying to generate dummies for birth order in children within households. I want to generate three dummies: first_born middle_child and last_child. Im trying to run the following code but can´t seem to use the min or max functions as conditionals for stata:

    sort idhousehold age
    bysort idhousehold : gen first_born = 1 if age==max(age) & if relation==3
    bysort idhousehold : gen middle_child = 1 if age>max(age) & age<max(age) & if relation==3
    bysort idhousehold : gen last_child = 1 if age==min(age) & if relation==3

    The conditional relation==3 means that this will be only generated for the ones that are identified as child inside the household.

    Thanks in advance!
    Alfonso

  • #2
    I think an easier way to do what you need is as follows
    gsort idhousehold -age relation
    by idhousehold relation:gen orderborn=_n if relation==3
    gen first_born = (orderborn==1)
    gen second_born = (orderborn==2)
    gen third_born = (orderborn==3)
    HTH

    Comment


    • #3
      I think the code in post #2 assumes that there are only 3 births. Here is a version of your original code that may point you in a useful direction.
      Code:
      sort idhousehold relation age
      bysort idhousehold relation : gen first_born   = relation==3 & _n==_N
      bysort idhousehold relation : gen last_child   = relation==3 & _n==_1
      bysort idhousehold relation : gen middle_child = relation==3 & first_born==0 & last_child==0

      Comment


      • #4
        There are questions on several levels here, including whether households contain adults too and/or a number of children not 3.

        We don’t know the precise definition of the variable relation.

        But the Stata function max() does not work as you are imagining it. It needs two or more arguments and does not compare values in different observations without explicit subscripting. The same holds for min().

        Note also that your syntax for the middle born is that the age is greater than the maximum, and also less than it, which you will see is quite wrong on further reflection.

        Stata allows only one
        if qualifier in any statement.

        The syntax of Fernando Rios looks more likely to be what you want. But I can imagine a simpler approach if you sort on age within household. If there are 3 children in a household then the third born is the youngest person, ..., the first born is the third youngest and this is not perturbed by observations for adults.

        Note: William’s approach is loosely similar.
        Last edited by Nick Cox; 10 Sep 2019, 18:10.

        Comment

        Working...
        X