Announcement

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

  • Changing SIF DMYhms Time Variables to DMY Time Variables

    I seek to geospatially merge some rainfall data with a master dataset. Before I can do that though, I must have the date of the weather be formatted properly. Here's some of my data so you can follow me.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long index double(date _CY _CX) float rain
      711 1.9131552e+12 49.400000000000006  -95.14166663333334 0
      712 1.9131552e+12 49.400000000000006  -95.09999996666667 0
      713 1.9131552e+12 49.400000000000006  -95.05833330000002 0
      714 1.9131552e+12 49.400000000000006  -95.01666663333334 0
      715 1.9131552e+12 49.400000000000006  -94.97499996666667 0
      716 1.9131552e+12 49.400000000000006  -94.93333330000002 0
      717 1.9131552e+12 49.400000000000006  -94.89166663333334 0
      718 1.9131552e+12 49.400000000000006  -94.84999996666667 0
     2096 1.9131552e+12 49.358333333333334  -95.18333330000002 0
     2097 1.9131552e+12 49.358333333333334  -95.14166663333334 0
     2098 1.9131552e+12 49.358333333333334  -95.09999996666667 0
     2099 1.9131552e+12 49.358333333333334  -95.05833330000002 0
     2100 1.9131552e+12 49.358333333333334  -95.01666663333334 0
     2101 1.9131552e+12 49.358333333333334  -94.97499996666667 0
     2102 1.9131552e+12 49.358333333333334  -94.93333330000002 0
     2103 1.9131552e+12 49.358333333333334  -94.89166663333334 0
     2104 1.9131552e+12 49.358333333333334  -94.84999996666667 0
     2105 1.9131552e+12 49.358333333333334  -94.80833330000002 0
     3482 1.9131552e+12  49.31666666666667  -95.18333330000002 0
     3483 1.9131552e+12  49.31666666666667  -95.14166663333334 0
     3484 1.9131552e+12  49.31666666666667  -95.09999996666667 0
     3485 1.9131552e+12  49.31666666666667  -95.05833330000002 0
     3486 1.9131552e+12  49.31666666666667  -95.01666663333334 0
     3487 1.9131552e+12  49.31666666666667  -94.97499996666667 0
     3488 1.9131552e+12  49.31666666666667  -94.93333330000002 0
     3489 1.9131552e+12  49.31666666666667  -94.89166663333334 0
     3490 1.9131552e+12  49.31666666666667  -94.84999996666667 0
     3491 1.9131552e+12  49.31666666666667  -94.80833330000002 0
     3492 1.9131552e+12  49.31666666666667  -94.76666663333334 0
     4868 1.9131552e+12 49.275000000000006  -95.18333330000002 0
     4869 1.9131552e+12 49.275000000000006  -95.14166663333334 0
     4870 1.9131552e+12 49.275000000000006  -95.09999996666667 0
     4871 1.9131552e+12 49.275000000000006  -95.05833330000002 0
     4872 1.9131552e+12 49.275000000000006  -95.01666663333334 0
     4873 1.9131552e+12 49.275000000000006  -94.97499996666667 0
     4874 1.9131552e+12 49.275000000000006  -94.93333330000002 0
     4875 1.9131552e+12 49.275000000000006  -94.89166663333334 0
     4876 1.9131552e+12 49.275000000000006  -94.84999996666667 0
     4877 1.9131552e+12 49.275000000000006  -94.80833330000002 0
     4878 1.9131552e+12 49.275000000000006  -94.76666663333334 0
     6254 1.9131552e+12 49.233333333333334  -95.18333330000002 0
     6255 1.9131552e+12 49.233333333333334  -95.14166663333334 0
     6256 1.9131552e+12 49.233333333333334  -95.09999996666667 0
     6257 1.9131552e+12 49.233333333333334  -95.05833330000002 0
     6258 1.9131552e+12 49.233333333333334  -95.01666663333334 0
     6259 1.9131552e+12 49.233333333333334  -94.97499996666667 0
     6260 1.9131552e+12 49.233333333333334  -94.93333330000002 0
     6261 1.9131552e+12 49.233333333333334  -94.89166663333334 0
     6262 1.9131552e+12 49.233333333333334  -94.84999996666667 0
     6263 1.9131552e+12 49.233333333333334  -94.80833330000002 0
     6264 1.9131552e+12 49.233333333333334  -94.76666663333334 0
     7640 1.9131552e+12  49.19166666666667  -95.18333330000002 0
     7641 1.9131552e+12  49.19166666666667  -95.14166663333334 0
     7642 1.9131552e+12  49.19166666666667  -95.09999996666667 0
     7643 1.9131552e+12  49.19166666666667  -95.05833330000002 0
     7644 1.9131552e+12  49.19166666666667  -95.01666663333334 0
     7645 1.9131552e+12  49.19166666666667  -94.97499996666667 0
     7646 1.9131552e+12  49.19166666666667  -94.93333330000002 0
     7647 1.9131552e+12  49.19166666666667  -94.89166663333334 0
     7648 1.9131552e+12  49.19166666666667  -94.84999996666667 0
     7649 1.9131552e+12  49.19166666666667  -94.80833330000002 0
     7650 1.9131552e+12  49.19166666666667  -94.76666663333334 0
     7651 1.9131552e+12  49.19166666666667  -94.72499996666667 0
     9026 1.9131552e+12 49.150000000000006  -95.18333330000002 0
     9027 1.9131552e+12 49.150000000000006  -95.14166663333334 0
     9028 1.9131552e+12 49.150000000000006  -95.09999996666667 0
     9029 1.9131552e+12 49.150000000000006  -95.05833330000002 0
     9030 1.9131552e+12 49.150000000000006  -95.01666663333334 0
     9031 1.9131552e+12 49.150000000000006  -94.97499996666667 0
     9032 1.9131552e+12 49.150000000000006  -94.93333330000002 0
     9033 1.9131552e+12 49.150000000000006  -94.89166663333334 0
     9034 1.9131552e+12 49.150000000000006  -94.84999996666667 0
     9035 1.9131552e+12 49.150000000000006  -94.80833330000002 0
     9036 1.9131552e+12 49.150000000000006  -94.76666663333334 0
     9037 1.9131552e+12 49.150000000000006  -94.72499996666667 0
    10412 1.9131552e+12 49.108333333333334  -95.18333330000002 0
    10413 1.9131552e+12 49.108333333333334  -95.14166663333334 0
    10414 1.9131552e+12 49.108333333333334  -95.09999996666667 0
    10415 1.9131552e+12 49.108333333333334  -95.05833330000002 0
    10416 1.9131552e+12 49.108333333333334  -95.01666663333334 0
    10417 1.9131552e+12 49.108333333333334  -94.97499996666667 0
    10418 1.9131552e+12 49.108333333333334  -94.93333330000002 0
    10419 1.9131552e+12 49.108333333333334  -94.89166663333334 0
    10420 1.9131552e+12 49.108333333333334  -94.84999996666667 0
    10421 1.9131552e+12 49.108333333333334  -94.80833330000002 0
    10422 1.9131552e+12 49.108333333333334  -94.76666663333334 0
    10423 1.9131552e+12 49.108333333333334  -94.72499996666667 0
    11137 1.9131552e+12  49.06666666666667 -122.72499996666667 0
    11138 1.9131552e+12  49.06666666666667 -122.68333330000002 0
    11139 1.9131552e+12  49.06666666666667 -122.64166663333334 0
    11140 1.9131552e+12  49.06666666666667 -122.59999996666667 0
    11141 1.9131552e+12  49.06666666666667 -122.55833330000002 0
    11142 1.9131552e+12  49.06666666666667 -122.51666663333334 0
    11143 1.9131552e+12  49.06666666666667 -122.47499996666667 0
    11144 1.9131552e+12  49.06666666666667 -122.43333330000002 0
    11145 1.9131552e+12  49.06666666666667 -122.39166663333334 0
    11146 1.9131552e+12  49.06666666666667 -122.34999996666667 0
    11147 1.9131552e+12  49.06666666666667 -122.30833330000002 0
    11148 1.9131552e+12  49.06666666666667 -122.26666663333334 0
    11149 1.9131552e+12  49.06666666666667 -122.22499996666667 0
    end
    format %tc date
    br
    Here, time series data come in the form of "16aug2020 00:00:00". I only need the "12aug2020" part. I've worked with converting string dates to something Stata recognizes... But I've never needed (or thought I'd need!) to convert a date with seconds, minutes and hours to just the day, month and year. My first instinct was that it was a formatting issue, so I did
    Code:
    format %td date
    but this returns an ungodly long number in scientific notation (presumably, the number of seconds since January 1 1960).What might I be missing here?

  • #2
    It isn't clear what you want to do because your post is ambiguous about whether you just want the variables to look like date variables, or whether you want them to actually become date variables. Those are two different things. If you want them to look like date variables:

    Code:
    format date %tcDMY
    will do that. Then whenever you list or display or browse those values you will see something that looks like it's just a date. But don't try to do any date calculations with that--you will get garbage. The actual internal representation of the variable is unchanged and those hours, minutes, and seconds are still there, and the unit is still the millisecond: you've just told Stata to hide that detail from view. But you haven't made it go away.

    If you want them to actually become date variables you have to do this:

    Code:
    gen date_from_time = dofc(date)
    format date_from_time %td
    The dofc() function actually converts a clock (i.e. date and time) variable to a date variable. With this new variable, you can do calculations for dates and they will produce correct results. This new variable takes on integer values, and its unit is one day. It is a real date variable--it contains no information about time units less than one day.

    Added: What you attempted, -format %td date- does neither of these things. It simply applies an inappropriate display format to the clock variable, which leads to the appearance of a gargantuan number (because typical clock variable values are gargantuan compared to typical date variable values), and also fails to change the internal representation. It's kind of like trying to squeeze your fist into a thimble--nothing good comes of it.
    Last edited by Clyde Schechter; 21 Sep 2021, 21:54.

    Comment


    • #3
      Clyde Schechter Apologies for the ambiguity. I was indeed looking for them to become date variables. Worked perfectly. Thanks a lot!

      Comment

      Working...
      X