Announcement

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

  • Time format reshuffle

    I've downloaded a file which has the American M/D/Y format:
    Date
    1/27/2018
    1/28/2018

    ….
    And I’d like to convert that into
    Date
    27-jan-2018
    28-jan-2018


    Can anyone help?

  • #2
    I've downloaded a file which has the American M/D/Y format:
    Unfortunately, all you are describing is how this looks to your eyes when you view the data somewhere, perhaps the Browser, or a -list- output. But to properly answer your question we need to also know how this data is represented internally: is it a string variable that looks the way you show it, or is it a Stata internal format daily date variable with a display format that produces what you see attached to it? Or is it possibly a Stata internal format clock variable which has been formatted to suppress the time components?

    On the hunch that it's a Stata internal format date variable that is formatted %tdnn/dd/CCYY or perhaps %tdnn/DD/CCYY (either of these would be consistent with what you show, although they would handle a date like 1 jan 2018 differently), all you need to do is:
    Code:
    format Date %tdNN-mon-CCYY
    However, if your variable Date is actually a string, then you would first be best advised to convert it to a Stata internal format date variable:
    Code:
    gen _Date = daily(Date, "MDY")
    assert missing(_Date) == missing(Date)
    format _Date %tdNN-mon-CCYY
    drop Date
    rename _Date Date
    And, finally, if it is actually a Stata internal format clock variable:
    Code:
    gen _Date = dofc(Date)
    format _Date %tdNN-mon-CCYY
    rename Date DateTime
    rename _Date Date
    In the future to avoid guesswork, always show example data when asking for help with coding. It is usually the case that correct code depends on the details of the data that cannot be adequately conveyed with verbal descriptions or visual displays. So always use the -dataex- command to share the example data on the Forum. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- 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
      Thanks Clyde. sorry indeed for my terse posting. Indeed, it's string data and in the MMDDYY format rather than my desired DD-MM-YY format.
      What you suggested makes sense but doesn't quite seem to do the trick (assuming I've implemented it correctly). see below

      de date symbol

      storage display value
      variable name type format label variable label
      ----------------------------------------------------------------------------------------------------------------
      date str10 %10s
      symbol str5 %9s



      li date symbol in 1/35

      +--------------------+
      | date symbol |
      |--------------------|
      1. | 1/1/2018 BAC |
      2. | 1/2/2018 BAC |
      <snip>
      29. | 1/29/2018 BAC |
      30. | 1/30/2018 BAC |
      |--------------------|
      31. | 1/31/2018 BAC |
      32. | 2/1/2018 BAC |
      33. | 2/2/2018 BAC |
      34. | 2/3/2018 BAC |
      35. | 2/4/2018 BAC |
      +--------------------+

      . gen _Date = daily(date, "MDY")
      . assert missing(_Date) == missing(date)
      . format _Date %tdNN-mon-CCYY
      . drop date
      . rename _Date Date

      . li Date symbol in 1/35

      +----------------------+
      | Date symbol |
      |----------------------|
      1. | 01-jan-2018 BAC |
      2. | 01-jan-2018 BAC |
      <snip>
      29. | 01-jan-2018 BAC |
      30. | 01-jan-2018 BAC |
      |----------------------|
      31. | 01-jan-2018 BAC |
      32. | 02-feb-2018 BAC |
      33. | 02-feb-2018 BAC |
      34. | 02-feb-2018 BAC |
      35. | 02-feb-2018 BAC |
      +----------------------+


      Comment


      • #4
        What you suggested makes sense but doesn't quite seem to do the trick (assuming I've implemented it correctly). see below
        Sorry, my error. The correct display format for what you want is %tdDD-mon-CCYY.

        Comment


        • #5
          Many thanks Clyde.

          Comment

          Working...
          X