Announcement

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

  • Turning numeric variable to string variable

    Hello!

    I am trying to convert the values from a numeric variable (double) to a string variable. I would like to do this so I can use the "drop" command to get rid of the dates prior to 2016. I tried using the command:
    tostring BookingDate, gen(BookingDate_str) but I got the message "BookingDate cannot be converted reversibly; no generate." I pasted a snippet of my data in the table below.

    Any advice would be very helpful. Thank you in advance!
    ​​​​​​​
    BookingDate
    9/19/1991 4:34
    9/4/2018 4:50
    7/2/2018 4:30
    11/6/2017 10:07
    4/7/2023 1:28
    4/1/2021 9:43
    11/14/1986 12:00
    10/3/2018 3:55
    5/28/1992 12:00

  • #2
    I am trying to convert the values from a numeric variable (double) to a string variable. I would like to do this so I can use the "drop" command to get rid of the dates prior to 2016
    No! No! No!

    If you convert the values from numeric to string, it will be very complicated and difficult to select the dates prior to 2016 for dropping! While it is not 100% clear what the variable bookingdate you have is like, your description makes it highly likely that you have a Stata internal format datetime variable with a %tcnn/dd/CCYY_hH:MM display format applied. To drop the observations from years before 2016 from this is simplicity itself:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double bookingdate
    1000874040000
    1.8516558e+12
     1.846125e+12
    1825582020000
    1996450080000
    1932889380000
     8.479728e+11
    1.8541581e+12
    1.0226736e+12
    end
    format %tcnn/dd/CCYY_hH:MM bookingdate
    
    drop if year(dofc(bookingdate)) < 2016
    Converting Stata datetime variables to strings makes them really quite unusable for almost any purpose, and certainly for the purpose you have in mind. And if you actually had a string variable to start with, the only sensible way to remove the observations from years before 2016 would begin with converting it to the numeric variable you actually have!

    Unless this is a one-off task, you really should study the information in -help datetime- to learn about how Stata represents dates and times internally, how they can be displayed in human readable form, as well as how to piece parts together into full dates and times, and how to extract parts from dates and times. It's something of a long read, and you won't remember all the details. But you will get an understanding of the entire Stata datetime system and how to use it. Then you can always use the help files to refresh your memory on details as needed. I promise you the time you invest in this will be amply repaid in short order.

    As an aside, bear in mind that Stata date and datetime variables are different. While it is simple to convert between them, they are not the same thing. To avoid mistakenly applying functions intended for date variables to datetime variables (or the other way around), which leads to gibberish results, it is a good idea for variable names to reflect what they really are. Since what you have includes time, it is not a date variable. So I would suggest renaming it to something like bookingdttm. That way you will not be tempted to mistakenly work with it as if it were a date variable.

    Finally, in the future, when showing data examples, please use the -dataex- command to do so. 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.
    Last edited by Clyde Schechter; 08 Nov 2023, 17:38.

    Comment


    • #3
      I agree strongly with Clyde Schechter's general advice, which sounds stern but is grounded in understanding and experience, some bitter!

      In addition I note that the first instant of 2016 as a Stata date-time in ms is a numeric constant that you can calculate directly so you just want to drop anything smaller as a date-time.

      Code:
      . di clock("1 Jan 2016", "DMY")
      1.767e+12
      
      . di %18.0f clock("1 Jan 2016", "DMY")
           1767225600000
      By the way you should never and need never type 1767225600000, as the code is the important detail for your log files.

      Comment

      Working...
      X