Announcement

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

  • Generation dummy variable

    Hi, I want to create a dummy variable for whether an individual is a 1st or 2nd generation migrant. I am using 3 variables to do this:
    Born in the country - which takes value 1 if yes and 2 if not
    Mother born in the country - which takes 1 if yes and 2 if not
    Father born in the country - which takes 1if yes and 2 if not

    A first-generation migrant is defined as them or their parents being born elsewhere- the code I did for this was:
    born in country * mother born in country* father born in country >=2 Which came out correct

    However, I am struggling as a second-generation migrant. Here second-generation migrant is a native-born population with at least one parent being born elsewhere.
    I tried the code:
    Born in country==1 | Mother born in country * father born in country >=2
    However this is not working

    Any help to do this more effectively or correctly. Thank you

  • #2
    Coding yes/no variables as 1 = yes and 2 = no is a bad idea in Stata. It causes you to write convoluted, opaque code. So I'd recode those three variables immediately and then work from there:

    Code:
    recode born_in_country mother_born_in_country father_born_in_country (2 = 0)
    
    gen byte first_generation_migrant = !(born_in_country & father_born_in_country & mother_born_in_country)
    gen byte second_generation_migrant = born_in_country & !(father_born_in_country & mother_born_in_country)
    If I have understood your definitions, these codes will work for you. And they have the advantage that when you come back and reread them weeks from now it will be immediately clear what they do because they mean exactly what they say.

    In Stata it is better to always work with yes = 1, no = 0.

    Comment


    • #3
      Understood. This worked for the second-generation migrant but not exactly for the first-generation migrant. For example, if the individual is born in the country but the mother is not, it still resulted in 1. So it should be either the individual or at least of the parents are born elsewhere.
      Would it not be:
      gen byte first_generation_migrant = !( born_in_country | father_born_in_country & mother_born_in_country )


      Last edited by Taiba Chau; 12 Apr 2022, 08:31.

      Comment


      • #4
        I just tried this line of code and it still did not work. Instead would it be:
        gen byte first_generation_migrant = born_in_country==0 & !( father_born_in_country & mother_born_in_country )

        Comment


        • #5
          So it should be either the individual or at least of the parents are born elsewhere.
          This is a syntactically incorrect sentence and it leaves out crucial information. If you mean either the individual or at least one of the parents is born elsewhere, then "if the individual is born in the country but the mother is not, it still resulted in 1" would be a correct result. I don't know what else you might have intended here. The point is that until you provide a clear and unambiguous description in English of your desired definition of first generation migrant, it isn't possible to say what would be a correct translation of that into Stata code.

          English, and, more generally, natural languages, do not do a good job of conveying complex logical conditions. The key operators "and" and "or" are often used inconsistently in ordinary speech. So we might spend a long time trying to hammer this out. So I suggest instead that you fill in a "truth table." There are 8 possible combinations of the focal person, that person's mother, and that person's father being born in country (BIC) or not born in country (!BIC). For each of those, fill in as Yes or No whether you want that person to be classed as a first generation migrant and I'll piece together corresponding code.

          Code:
          Focal Person    Mother      Father      First-Generation Immigrant(Yes/No)
          BIC             BIC         BIC
          BIC             BIC         !BIC
          BIC             !BIC        BIC
          BIC             !BIC        !BIC
          !BIC            BIC         BIC
          !BIC            BIC         !BIC
          !BIC            !BIC        BIC
          !BIC            !BIC        !BIC

          Comment

          Working...
          X