Announcement

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

  • Generating variable with conditions

    Dear all,

    I have a eu dummy variable that takes value 1 since the year of incorporation in the EU. I would like to generate a new dummy variable that takes value 1 if eu equals 1 for any year of the period, that is, a variable that indicates if the country is or not in the EU. I do not know how to indicate stata the "for any year of the period" issue.
    I tried with the following code but got exactly the same eu variable.
    Code:
    gen EU_new_d=0
        replace EU_new_d=1 if eu_d==1&year==1995|eu_d==1&year==1996|eu_d==1&year==1997|eu_d==1&year==1998|eu_d==1&year==1999|eu_d==1&year==2000|eu_d==1&year==2001|eu_d==1&year==2002|eu_d==1&year==2003|eu_d==1&year==2004|eu_d==1&year==2005|eu_d==1&year==2006|eu_d==1&year==2007|eu_d==1&year==2008|eu_d==1&year==2009|eu_d==1&year==2010|eu_d==1&year==2011|eu_d==1&year==2012|eu_d==1&year==2013|eu_d==1&year

    Thanks for any suggestions.

    Best regards,
    Laura




  • #2
    Laura:
    I'm not sure I've got you right, as it seems that you already have what you're after.
    Anyway, a tempative answer would imply something along the following lines:
    Code:
    . set obs 10
    
    . g  country="whatyouwant"
    
    . g year=_n
    
    . g eu=1 in 5/10
    
    . replace eu=0 if eu==.
    
    . bysort country year: gen new_eu=1 if eu==1
    
    . replace new_eu=0 if new_eu==.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Your code as copied here ends

      Code:
      & year 
      and year as a condition by itself always counts as true (given that I guess you have no year 0).

      Your code boils down to something more like

      Code:
      gen EU_new_d = eu_d==1 & inrange(year, 1995, 2014)
      See for more guidance

      https://www.stata-journal.com/sjpdf....iclenum=dm0058

      https://www.stata.com/support/faqs/d...rue-and-false/

      Comment


      • #4
        I´m sorry but I do not understand what you suggest. With the previous commnad my new EU dummy looks like the old one, as it is shown in the attached image. I would like it to take value 1 for the whole period (1995-2014) whenever my old eu variable takes the value 1 for any year of that period. Hope to have explained myself better now.
        Best regards,
        Laura
        Attached Files

        Comment


        • #5
          I think #3 does you what you ask. Perhaps you're replying to Carlo in #2.

          Comment


          • #6
            Hi Nick,

            Thanks for your suggestion. However, when I apply what you suggest the new variable takes the same values as in the image I´ve attached. It should take the value 1 since 1995 to 2014 for the example shown in the image and not just for 2013-2014 as in the old eu variable.

            Comment


            • #7
              I don't see your point. If you want as one necessary condition

              Code:
              eu_d == 1 
              then the values shown in the image (please don't show images; FAQ Advice #12) are exactly what are expected.

              Comment


              • #8
                Laura:
                I propose another temptative answer:
                Code:
                set obs 10
                g country="whatyouwant"
                g year=_n
                g eu=1 in 5/10
                replace eu=0 if eu==.
                bysort country: egen new_eu=max(eu)
                list
                
                     +----------------------------------+
                     |     country   year   eu   new_eu |
                     |----------------------------------|
                  1. | whatyouwant      1    0        1 |
                  2. | whatyouwant      2    0        1 |
                  3. | whatyouwant      3    0        1 |
                  4. | whatyouwant      4    0        1 |
                  5. | whatyouwant      5    1        1 |
                     |----------------------------------|
                  6. | whatyouwant      6    1        1 |
                  7. | whatyouwant      7    1        1 |
                  8. | whatyouwant      8    1        1 |
                  9. | whatyouwant      9    1        1 |
                 10. | whatyouwant     10    1        1 |
                     +----------------------------------+
                Kind regards,
                Carlo
                (Stata 19.0)

                Comment


                • #9
                  Thank you so much Carlo. It provides exactly the variable I need.

                  Kind regards,
                  Laura

                  Comment

                  Working...
                  X