Announcement

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

  • Splitting a numeric variable

    Hello,

    This should be a simple question, any help would be appreciated.

    I am currently working with a date and time variable ("submissiondate") of the following format:
    dd/mm/yyyy hh:mm:ss
    12/24/1901 02:34:05

    I want to split the variable parsing it at the space, so that I can isolate the date part of the variable. I haven't been able to find a command to split the numeric like this. I tried creating a string out of it to split it that way, but I am met with the "cannot be generated reversibly" message. For clarity, my desired outcome is the following:
    submissiondate1 submissiondate2
    12/24/1901 02:34:05

    Thank you very much in advance

  • #2
    Presumably submissiondate is a double variable; it can be mapped to a daily date by


    Code:
    gen dailydate = dofc(submissiondate)
    See
    help datetime. This is a numeric operation. not a string operation as a daily date too is, or should be, a numeric variable.

    Time of day as a whole is presumably

    Code:
    gen double timeofday = submissiondate - cofd(dofc(submissiondate))
    but someone may have a better method.
    Last edited by Nick Cox; 26 Oct 2021, 10:51.

    Comment


    • #3
      Erik, based on your algorithm, the following codes might work.

      Code:
      gen strvar = string(submissiondate, "%tcNN/DD/CCYY_HH:MM:SS")    // convert datetime to string
      gen submissiondate1 = substr(strvar, 1, 10)
      gen submissiondate2 = substr(strvar, 12, 8)
      drop strvar
      Last edited by Fei Wang; 26 Oct 2021, 11:24.

      Comment

      Working...
      X