Announcement

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

  • Can we create a variable using two conditions

    Can we create a new variable named "MID"
    Such that MID=1 if "experience">=3 & "experience"<=8 and "imprints" during 1st and 2nd year is ==1
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long stockcode int year str40 name float(experience imprints MID)
    1 2006 "APPIGNANI Antonio"  1 1 0
    1 2007 "APPIGNANI Antonio"  2 1 0
    1 2008 "APPIGNANI Antonio"  3 0 1
    1 2009 "APPIGNANI Antonio"  4 0 1
    1 2010 "APPIGNANI Antonio"  5 0 1
    1 2011 "APPIGNANI Antonio"  6 0 1
    1 2012 "APPIGNANI Antonio"  7 0 1
    1 2013 "APPIGNANI Antonio"  8 0 1
    1 2014 "APPIGNANI Antonio"  9 0 0
    1 2015 "APPIGNANI Antonio" 10 0 0
    1 2016 "APPIGNANI Antonio" 11 0 0
    1 2017 "APPIGNANI Antonio" 12 0 0
    1 2018 "APPIGNANI Antonio" 13 0 0
    1 2019 "APPIGNANI Antonio" 14 0 0
    2 2006 "TIM"                1 0 0
    2 2007 "TIM"                2 0 0
    2 2008 "TIM"                3 0 0
    2 2009 "TIM"                4 0 0
    2 2010 "TIM"                5 0 0
    2 2011 "TIM"                6 0 0
    2 2012 "TIM"                7 0 0
    2 2013 "TIM"                8 0 0
    2 2014 "TIM"                9 0 0
    2 2015 "TIM"               10 0 0
    2 2016 "TIM"               11 0 0
    2 2017 "TIM"               12 0 0
    end

  • #2
    Tauseef:
    do you mean something along the following lines?
    Code:
    . bysort name (year): egen wanted=total(imprints) if imprints<=2
    
    . bysort name (year): replace wanted=1 if experience>=3 & experience<=8 & wanted==2
    
    . replace wanted=0 if wanted==2
    
    . list
        +--------------------------------------------------------------------------+
         | stockc~e   year                name   experi~e   imprints   MID   wanted |
         |--------------------------------------------------------------------------|
      1. |        1   2006   APPIGNANI Antonio          1          1     0        0 |
      2. |        1   2007   APPIGNANI Antonio          2          1     0        0 |
      3. |        1   2008   APPIGNANI Antonio          3          0     1        1 |
      4. |        1   2009   APPIGNANI Antonio          4          0     1        1 |
      5. |        1   2010   APPIGNANI Antonio          5          0     1        1 |
         |--------------------------------------------------------------------------|
      6. |        1   2011   APPIGNANI Antonio          6          0     1        1 |
      7. |        1   2012   APPIGNANI Antonio          7          0     1        1 |
      8. |        1   2013   APPIGNANI Antonio          8          0     1        1 |
      9. |        1   2014   APPIGNANI Antonio          9          0     0        0 |
     10. |        1   2015   APPIGNANI Antonio         10          0     0        0 |
         |--------------------------------------------------------------------------|
     11. |        1   2016   APPIGNANI Antonio         11          0     0        0 |
     12. |        1   2017   APPIGNANI Antonio         12          0     0        0 |
     13. |        1   2018   APPIGNANI Antonio         13          0     0        0 |
     14. |        1   2019   APPIGNANI Antonio         14          0     0        0 |
     15. |        2   2006                 TIM          1          0     0        0 |
         |--------------------------------------------------------------------------|
     16. |        2   2007                 TIM          2          0     0        0 |
     17. |        2   2008                 TIM          3          0     0        0 |
     18. |        2   2009                 TIM          4          0     0        0 |
     19. |        2   2010                 TIM          5          0     0        0 |
     20. |        2   2011                 TIM          6          0     0        0 |
         |--------------------------------------------------------------------------|
     21. |        2   2012                 TIM          7          0     0        0 |
     22. |        2   2013                 TIM          8          0     0        0 |
     23. |        2   2014                 TIM          9          0     0        0 |
     24. |        2   2015                 TIM         10          0     0        0 |
     25. |        2   2016                 TIM         11          0     0        0 |
         |--------------------------------------------------------------------------|
     26. |        2   2017                 TIM         12          0     0        0 |
         +--------------------------------------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Possibly this is what you need?

      Code:
      bysort stockcode (year): gen imprints_check = imprints[1] == 1 & imprints[2] == 1
      gen byte MID2 = inrange(experience,3,8) & imprints_check
      drop imprints_check

      Comment


      • #4
        Code:
        egen imprints12 = total(imprints == 1 & inlist(year, 2006, 2007)), by(stockcode)
        
        gen wanted = (imprints12 == 2) & inrange(experience, 3, 8)
        
        assert wanted == MID

        EDIT #3 and #4 are essentially the same idea. I would go with Hemanshu Kumar and indeed you can telescope his code to


        Code:
         bysort stockcode (year): gen byte MID2 = imprints[1] == 1 & imprints[2] == 1 & inrange(experience,3,8)
        Last edited by Nick Cox; 18 Jan 2023, 03:53.

        Comment


        • #5
          Carlo, I worry about the code in #2.

          It works for the data example, but it would give wrong results if imprints were to be 1 in multiple years but not the first two (I don't know if this occurs in the actual data).

          Comment


          • #6
            Hemanshu:
            you're correct.
            My previous attempt lacks of generalizability.
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Thank you so much for your sincere efforts. It works

              Comment

              Working...
              X