Announcement

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

  • datediff not working

    Hi all

    looking for some help to work out difference in dates between two variables and output this in number of days format
    var1 format is %tCMonth_dd,_CCYY
    var2 format is %dM_d,_CY

    i have tried following command but its not working for some reason

    generate diffday = datediff( var1, var2, "day")

    any help would be appreciated

  • #2
    To be absolutely sure what is going on, I would need to see example data. But I will make an educated guess what is going wrong.

    var1 has a display format of %tC... If what you see as the values of var1 look sensible, then var1 is not a date variable. It is a datetime variable. (If it were a date variable and has a %tC display format, the values you would see in the browser or in -list- output would all be January 1, 1960.) The -datediff() function works with date variables, not with datetime variables. The solution is to convert var1 to a date variable:

    Code:
    gen diffday = datediff(dofC(var1), var2, "day")
    Added: In the future, when asking for help with code, please use the -dataex- command and show example data. Although sometimes, as here, it is possible to give an answer that has a reasonable probability of being correct, this is usually not the case. Moreover, such answers are necessarily based on experience-based guesses or intuitions about the nature of your data. When those guesses are wrong, both you and the person trying to help you have wasted their time as you end up with useless code. To avoid this, a -dataex- based example provides all of the information needed to develop and test a solution.

    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; 03 Aug 2023, 15:49.

    Comment


    • #3
      thats worked

      Many thanks Clyde

      will use dataex command in future

      Comment

      Working...
      X