Announcement

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

  • create a variable for coinciding dates

    Hello,

    I am trying to get my head around stata but i'm having issues creating a 0/1 variable where dates coincide:

    I have the following variables:
    Xstart
    Xstop
    Ystart
    Ystop

    If at any point x and y coincide, i would like to create a column where there's a 1. If they do not coincide, i would like a 0.

    For example:
    Xstart = 1jan2013
    Xstop = 1dec2013
    Ystart = 1apr2013
    Yend = 1apr2014

    They coincided between April and December, and therefore I would like a 1 in my new variable.

    However, if Xstop and Ystart occur on the same day, i would like a 0.


    I hope this makes sense. I am having trouble, and it is for my degree.

    Thanks in advance for your help.

  • #2
    how about:
    Code:
    gen byte coincide=inrange(ystart,xstart,xstop) | inrange(yend,xstart,xstop)
    replace coincide=0 if xstop==ystart
    note that the above assumes no missing values; if there are missing values you will have to fix them if you want your new variable to be equal to missing in that situation; also, of course, you can name your new variable whatever you want; I just called it "coincide" for convenience

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      how about:
      Code:
      gen byte coincide=inrange(ystart,xstart,xstop) | inrange(yend,xstart,xstop)
      replace coincide=0 if xstop==ystart
      note that the above assumes no missing values; if there are missing values you will have to fix them if you want your new variable to be equal to missing in that situation; also, of course, you can name your new variable whatever you want; I just called it "coincide" for convenience
      there's no missing variables. I'm about to try this out and let you know, thanks in advance.

      Brilliant. It has almost worked. Just needs some tweaking as something stopped on the same day something has started and it has coded it as 1. But thank you so much.
      Last edited by Wasim Miy; 26 Jul 2016, 08:32.

      Comment


      • #4
        1. I was not offended

        2. the "replace" command in my code should have dealt with this (or are you talking about something other than when xstop is the same as ystart?)

        Comment

        Working...
        X