Announcement

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

  • Calculating with dates from two datasets

    I have issues calculating the days between day A and day B for each observation , however, if I calculate with the current formats I get numbers like
    Code:
    19145
    Day B has already the
    Code:
    type: Numeric daily date (int)
    and the values look like the following:
    Code:
    05jul2010
    While day A has type: "Numeric (byte)" and haas different values in the
    Code:
    Range: [1 to 16]
    with value labels such as:
    Code:
    Friday 6th June 2010
    How can I format day A in order to calculate and display the amount of days between day A and B?
    Last edited by Penelope Smart; 12 Dec 2021, 12:23.

  • #2
    It is easier to help if you provide a data example. To do so use the command dataex and copy the output that appears in the results window.

    Comment


    • #3
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte A
       6
       6
       6
       6
       6
      12
      12
      12
      12
      12
      12
      12
      12
      13
      13
      13
      13
      13
      13
      end
      label values A
      label def A 1 "Monday 3nd June 2011", modify
      label def A 3 "Wednesday 5th June 2011", modify
      label def A 4 "Thursday 6th June 2011", modify
      label def A 5 "Friday 7th June 2011", modify

      Comment


      • #4
        Thank you for the example. That is a peculiar date variable. Are the values days since Sunday 2nd June 2011? If so,
        Code:
        gen A2 = A + 18780 
        gen daysbetween = B - A2

        Comment


        • #5
          Is there a way to convert day A into a format such as day B
          Code:
           
           type: Numeric daily date (int)   
           05jul2010
          ?

          Comment


          • #6
            Yes, if we know what dates the values of A represent.

            Comment


            • #7
              Yes, there is.

              It is difficult for me to imagine that the -dataex- you show in #3 is the actual output of -dataex- from a real Stata dataset. It includes a value label definition, but only for values of A that do not occur in the example data itself. -dataex- does not do that. I can't help thinking you edited some real -dataex- output in a way that turned out to make it unusable. Never modify the output of -dataex-. If -dataex- produces output that differs from what you want to show, go back and run -dataex- again with appropriate -if- or -in- qualifiers, or make modifications to the Stata data set itself and then run -dataex- again.

              In any case, what you show cannot be used because there is no way to know what actual dates the values 6, 12, and 13 of A correspond to. Perhaps Oyvind Snilsberg's conjecture is correct--but you didn't respond to his question. If the answer to his question is yes, tell us that and your problem is already solved in #4 by the variable A2. All you have to do is -format A2 %td-.

              If the answer to his question is no, then it's more complicated because the named days of the week and the ordinal suffixes on the date get in the way and have to be removed:

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte A
              2
              4
              3
              1
              end
              label values A A
              label def A 1 "Friday 7th June 2011", modify
              label def A 2 "Monday 3nd June 2011", modify
              label def A 3 "Thursday 6th June 2011", modify
              label def A 4 "Wednesday 5th June 2011", modify
              
              decode A, gen(A_str)
              foreach x in st nd rd th {
                  replace A_str = subinstr(A_str, "`x'", "", .)
              }
              replace A_str = subinstr(A_str, word(A_str, 1), "", 1)
              gen wanted = daily(A_str, "DMY" )
              format wanted %td
              Added: Crossed with #5.

              Comment

              Working...
              X