Announcement

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

  • Problem with logical operator when creating a dummy

    Dear Stata users,

    I'm struggling with creating a dummy indicating belong to one of the two groups. I have a panel data. I want to create a dummy indicating that a firm is either important or is polish. I wanted to have fixed timing. That's why I wanted to have a state from 2019 for all firms.

    I used the code:
    Code:
    gen imp_polish=0
    replace imp_polish=cond(year==2019,1,.) if Importance==1| PL==1
    replace imp_polish=imp_polish[_N]
    Then I browsed and noticed I didn't get what I wanted to achieve. E.g I see that the indicator Importance equals 1 and I see my dummy imp_polish=0. I wanted to have all important firms + polish firms. It is what I want. Please help me. I don't understand what I'm doing wrong

  • #2
    Guest:

    I tried to replicate your issue via a toy-example:
    Code:
    . use "https://www.stata-press.com/data/r17/nlswork.dta"
    (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
    
    . gen wanted=0
    
    . replace wanted =cond(year==70,1,.) if msp==1| union==1
    (19,061 real changes made, 18,070 to missing)
    
    . replace wanted=wanted[_N]
    (10,464 real changes made, 10,464 to missing)
    Sice the last observation of -wanted- has a missing values, all the values become missing after the last -replace-.
    Last edited by sladmin; 28 Aug 2023, 08:34. Reason: anonymize original poster
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Thank you Carlo for your answer. I tried this code in the example you provided:

      Code:
       
       use "https://www.stata-press.com/data/r17/nlswork.dta"   
       gen wanted=0    
       replace wanted =cond(year==70,1,.) if msp==1| union==1  bysort id (wanted): replace wanted=wanted[_N]
      However, I wasn't successful as well


      Comment


      • #4
        When you sort a numerical variable, missing values are sorted last, so you want to pick the first value.

        gen imp_polish=0
        replace imp_polish=cond(year==2019,1,.) if Importance==1| PL==1
        replace imp_polish=imp_polish[_N]
        The -cond()- function assigning 1 or missing is unnecessary here. You can write this as:


        Code:
        gen imp_polish= year==2019& (Importance==1| PL==1)
        bys id (imp_polish): replace imp_polish= imp_polish[_N]
        as the generated variable is an indicator 0/1, or in one line

        Code:
        bys id: egen imp_polish= max(year==2019 & (Importance==1| PL==1))
        where id is your panel identifier. Note that for your code, you needed the last line to be

        Code:
        bys id (imp_polish): replace imp_polish=imp_polish[1]
        as your variable is coded missing/1 after the replace statement in line 2 of your code.
        Last edited by Andrew Musau; 11 Jul 2022, 03:29.

        Comment


        • #5
          Thank you so much Andrew for your help! It is exactly what I wanted to have.

          Comment

          Working...
          X