Announcement

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

  • Check if variable at datetime equal variable at datetime + 24 hours

    Good day,

    I have two variables (among others): One variable is a binary variable 0 or 1 (sleepwake). The other variable (datetime) is date followed by time (in %tc format). There are 7 days worth of rows, at 15-sec intervals. I want to create a new variable, called "sri", and I want to replace sri for datetime with 0 if sleepwake at datetime != sleepwake at datetime + 24 hours, and I want to replace sri for datetime with 1 if sleepwake at datetime == sleepwake at datetime + 24h. sri for the last day must be "." since there is no sleepwake for datetime + 24 hours to compare with.

    datetime sleepwake sri
    24feb2017 18:01:30 0 0
    24feb2017 18:01:45 0 1
    24feb2017 18:02:00 0 1
    24feb2017 18:02:15 1 1
    24feb2017 18:02:30 1 1
    ...
    25feb2017 18:01:30 1 .
    25feb2017 18:01:45 0 .
    25feb2017 18:02:00 0 .
    25feb2017 18:02:15 1 .
    25feb2017 18:02:30 1 .

    Please help me find a solution.

    Thanks so much.

  • #2
    This makes no sense. It is mathematically impossible to have x = x + 24 hours. I suspect you actually have something else in mind, but it isn't clear what that might be. I suggest you post back showing a hand-worked example of what you want along with a step-by-step explanation of how you got it.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      I think I understand that you want to compare a value of sleepwake at a given time with the value recorded precisely 24 hours later. I transformed your sample data into a useful presentation of sample data at some cost in time and tedium. So to offset that time, I offer an incomplete solution that should start you in a useful direction.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double datetime byte sleepwake
      1803578490000 0
      1803578505000 0
      1803578520000 0
      1803578535000 1
      1803578550000 1
      1803664890000 1
      1803664905000 0
      1803664920000 0
      1803664935000 1
      1803664950000 1
      end
      format %tc datetime
      tempfile master
      save `master'
      list, clean
      replace datetime = datetime - 1000*60*60*24
      rename sleepwake nextday
      merge 1:1 datetime using `master'
      list, clean

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        This makes no sense. It is mathematically impossible to have x = x + 24 hours. I suspect you actually have something else in mind, but it isn't clear what that might be. I suggest you post back showing a hand-worked example of what you want along with a step-by-step explanation of how you got it.

        In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
        Thanks Clyde, my apologies for not using the right format. I installed dataex (running Stata 12) and will use it next time. With regards to my problem, William Lisowski explains it correctly: I want to compare a value at a specific time, with the value recorded exactly 24h later. I would post the requested example and explanation if William Lisowski hadn't already solved my problem.

        Thanks so much again, I really appreciate it.

        Comment


        • #5
          Originally posted by William Lisowski View Post
          I think I understand that you want to compare a value of sleepwake at a given time with the value recorded precisely 24 hours later. I transformed your sample data into a useful presentation of sample data at some cost in time and tedium. So to offset that time, I offer an incomplete solution that should start you in a useful direction.
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double datetime byte sleepwake
          1803578490000 0
          1803578505000 0
          1803578520000 0
          1803578535000 1
          1803578550000 1
          1803664890000 1
          1803664905000 0
          1803664920000 0
          1803664935000 1
          1803664950000 1
          end
          format %tc datetime
          tempfile master
          save `master'
          list, clean
          replace datetime = datetime - 1000*60*60*24
          rename sleepwake nextday
          merge 1:1 datetime using `master'
          list, clean
          Thanks so much William. This is exactly right. I apologise for the confusion and for not using the right example data format. Thank you for taking the time and effort to bring the data in a useful format. Offering an incomplete solution for that only makes sense to me.

          Just for completeness, should someone else be looking for this solution, I have made the following changes to get what I needed:

          Code:
          tempfile master
          save `master'
          list, clean
          replace datetime = datetime - 1000*60*60*24
          rename currentday nextday // I liked your "nextday" varname, so I decided to use "currentday" instead of "sleepwake". 
          merge 1:1 datetime using `master' // I really like this approach. 
          list, clean
          
          drop status2
          drop if currentday==.
          drop if nextday==.
          drop _merge
          
          gen sri=. 
          replace sri=1 if nextday==currentday
          replace sri=0 if nextday!=currentday
          Thanks so much!!

          Comment


          • #6
            Glad you were able to take it the rest of the way. Here's a Stata tip inspired by your code.

            Logical operators like "==" are no different than arithmetical operators like "+". They take their two arguments and return a value. For logical operators, the value is 0 if the expression formed by the operator and its arguments is false, and 1 otherwise. Since Stata treats 0 as "false" and non-zero as "true" these work as true and false in subsequent expressions using logical operators.

            So the last three lines of your code could be replaced by
            Code:
            generate sri = nextday==currentday
            and later on, of course, you can use sri in code like
            Code:
            summarize sri
            as well as in further logical expressions.

            Comment

            Working...
            X