Announcement

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

  • Partial dates and full dates under one variable

    I have a variable which containts full dates (31/01/2018), partial dates (2018-01) and missing dates. I'm not interested in missing dates and missing data points in this case is not a problem. What is seeming to be a problem is having a combination of full dates and partial dates. If it was one or the other I would be fine.

    My aim is to be able to compare these full and partial dates to another date (30 April 2018, or just April 2018 if that is easier) and to see whether or not they exceed this date.

    I have tried generating a new variable:

    Code:
    gen ILISTDAT22 = date(ILISTDAT_E7_C17_2, "MDY")
    format ILISTDAT22 %td
    The problem is this then misses out the partial dates, and if I replace "MDY" with "YM" this then misses out all of the full dates. Any idea how I can get round this problem? I have spent most today trying to figure this out but to no avail.

    Many thanks,
    Jenna

  • #2
    not elegant, but ... clone your variable and run the MDY command one copy and the YM command on the other; see
    Code:
    help clonevar

    Comment


    • #3
      Note that date() is not a function for producing monthly dates. This is explained better by the name of the synonym daily().

      Your problem is that if any of the (day, month, year) components is missing, the result must be missing too. Imputing 15 when the day is missing has satisfied some although there is a case for something more elaborate:

      SJ-12-1 dm0062 . . . . . . . . . Stata tip 105: Daily dates with missing days
      . . . . . . . . . . . . . . . . . . . . . S. J. Samuels and N. J. Cox
      Q1/12 SJ 12(1):159--161 (no commands)
      presents strategies for dealing with daily dates where
      some observations only provide the month and year


      Comment


      • #4
        Originally posted by Rich Goldstein View Post
        not elegant, but ... clone your variable and run the MDY command one copy and the YM command on the other; see
        Code:
        help clonevar
        Thank you Rich I will give this a try and let you know how I get on.

        Nick - Thank you for your reply. I have downloaded the article and will read it over the weekend. In the previous place I worked if the date was a partial date then the person entering the data would add in 15 for the day. Unfortunately in my new place of work that is not the case and so we have to work with just the month and year and in some cases just the year.

        Comment


        • #5
          Nick Cox I have read the article you kindly referenced and I am happy to impute 15 for the day where I only have a partial date. However I am not sure how to implement this within my code as I have some partial dates and some full dates. Furthermore (as I am relatively new to using Stata) how to get the code to know that I want it to 'gen imputedday = 15' on variable ILISTDAT.

          Many thanks in advance,
          Jenna

          Comment


          • #6
            Nick Cox I read the article you kindly referenced and was content to impute 15 for the day where I only have a partial date. However I was not sure how to implement this within my code as I have some partial dates and some full dates. Furthermore (as I am relatively new to using Stata) I could not figure out how to get the code to know that I wanted it to gen imputedday = 15 on variable ILISTDAT.

            Rich Goldstein thank you for your advice on using clonevar. I gave this a try but stata didn't take kindly to my if conditon.

            Therefore my solution to the problem was this:

            Code:
            *Formatting dates
            gen ILISTDATFULL = date(ILISTDAT_E7_C17_2, "DMY")
            format ILISTDATFULL %td
            gen ILISTDATPRT = date(ILISTDAT_E7_C17_2, "YM")
            format ILISTDATPRT %tm
            
            *Check the ILI 2 date of onset is between the baseline date and the 30 April 2018
            putdocx paragraph
            putdocx text ("ILI 2 date of onset is not between the baseline date and the 30 April 2018"), bold
            putdocx table INVICTUS = data(StudySubjectID VISDAT_E1_C1 ILISTDAT_E7_C17_2) if ///
            ILISTDATFULL > td(30apr2018) & ILISTDATFULL !=.
            
            *Check the ILI 2 partial date of onset is between the baseline date and the April 2018
            putdocx paragraph
            putdocx text ("ILI 2 partial date of onset is not between the baseline date and the April 2018"), bold
            putdocx table INVICTUS = data(StudySubjectID VISDAT_E1_C1 ILISTDAT_E7_C17_2) if ///
            ILISTDATPRT > tm(2018-04) & ILISTDATPRT !=.
            Thank you both for your advice.

            Comment


            • #7
              I think you need to give relevant code and a data example. Using date() with argument YM can only yield missings, as already explained.

              Comment


              • #8
                I seem to have got round the missing data by using:

                Code:
                 
                 ILISTDATFULL !=.
                The code I have to compare the partial dates (I have the variable ILISTDAT_E7_C17_2 which contains full dates and partial dates and I need to make sure none of these are after the 30 April 2018) is as follows, but it would be useful and interesting to know how to implement 'gen imputeddate = 15' as an alternative method.

                Code:
                gen ILISTDATFULL = date(ILISTDAT_E7_C17_2, "DMY")
                format ILISTDATFULL %td
                gen ILISTDATPRT = date(ILISTDAT_E7_C17_2, "YM")
                format ILISTDATPRT %tm
                
                *Check the ILI 2 partial date of onset is between the baseline date and April 2018
                capture noisily {
                putdocx paragraph
                putdocx text ("ILI 2 partial date of onset is not between the baseline date and April 2018"), bold
                putdocx table INVICTUS = data(StudySubjectID VISDAT_E1_C1 ILISTDAT_E7_C17_2) if ///
                ILISTDATPRT < tm(2018-04) & ILISTDATPRT !=.
                }
                Example dummy data:


                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input int(StudySubjectID VISDAT_E1_C1) str9 ILISTDAT_E7_C17_2
                100 21124 "16feb2018"
                101 21125 ""         
                102 21126 "2018-02"  
                103 21127 "13jan2018"
                104 21128 ""         
                105 21129 ""         
                106 21130 "01jun2018"
                107 21131 "2018-05"  
                108 21132 ""         
                109 21133 ""         
                110 21134 "12dec2017"
                111 21135 ""         
                112 21136 ""         
                113 21137 "02dec2017"
                114 21138 "15jan2018"
                115 21139 ""         
                116 21140 ""         
                117 21141 "05jan2018"
                118 21142 ""         
                119 21143 "23may2018"
                120 21144 "05mar2018"
                  .     . ""         
                end
                format %tdnn/dd/CCYY VISDAT_E1_C1


                Comment


                • #9
                  I am not sure we are yet on the same page, but thanks for the data example. I haven't tried to understand the putdocx stuff, which I have never used. But your example shows two kinds of dates, so we try each in turn:

                  Code:
                  clear
                  input int(StudySubjectID VISDAT_E1_C1) str9 ILISTDAT_E7_C17_2
                  100 21124 "16feb2018"
                  101 21125 ""         
                  102 21126 "2018-02"  
                  103 21127 "13jan2018"
                  104 21128 ""         
                  105 21129 ""         
                  106 21130 "01jun2018"
                  107 21131 "2018-05"  
                  108 21132 ""         
                  109 21133 ""         
                  110 21134 "12dec2017"
                  111 21135 ""         
                  112 21136 ""         
                  113 21137 "02dec2017"
                  114 21138 "15jan2018"
                  115 21139 ""         
                  116 21140 ""         
                  117 21141 "05jan2018"
                  118 21142 ""         
                  119 21143 "23may2018"
                  120 21144 "05mar2018"
                    .     . ""         
                  end
                  format %tdnn/dd/CCYY VISDAT_E1_C1
                  
                  gen wanted = daily( ILISTDAT_E7_C17_2, "DMY") 
                  replace wanted = daily( ILISTDAT_E7_C17_2 + "_15", "YMD") if missing(wanted) 
                  format wanted  %tdnn/dd/CCYY
                  
                  list 
                  
                       +------------------------------------------------+
                       | StudyS~D   VISDAT_E~1   ILISTDA~2       wanted |
                       |------------------------------------------------|
                    1. |      100    11/1/2017   16feb2018    2/16/2018 |
                    2. |      101    11/2/2017                        . |
                    3. |      102    11/3/2017     2018-02    2/15/2018 |
                    4. |      103    11/4/2017   13jan2018    1/13/2018 |
                    5. |      104    11/5/2017                        . |
                       |------------------------------------------------|
                    6. |      105    11/6/2017                        . |
                    7. |      106    11/7/2017   01jun2018     6/1/2018 |
                    8. |      107    11/8/2017     2018-05    5/15/2018 |
                    9. |      108    11/9/2017                        . |
                   10. |      109   11/10/2017                        . |
                       |------------------------------------------------|
                   11. |      110   11/11/2017   12dec2017   12/12/2017 |
                   12. |      111   11/12/2017                        . |
                   13. |      112   11/13/2017                        . |
                   14. |      113   11/14/2017   02dec2017    12/2/2017 |
                   15. |      114   11/15/2017   15jan2018    1/15/2018 |
                       |------------------------------------------------|
                   16. |      115   11/16/2017                        . |
                   17. |      116   11/17/2017                        . |
                   18. |      117   11/18/2017   05jan2018     1/5/2018 |
                   19. |      118   11/19/2017                        . |
                   20. |      119   11/20/2017   23may2018    5/23/2018 |
                       |------------------------------------------------|
                   21. |      120   11/21/2017   05mar2018     3/5/2018 |
                   22. |        .            .                        . |
                       +------------------------------------------------+
                  In a larger dataset you would see if some dates remained missing by

                  Code:
                  list if missing(wanted) & !missing(ILISTDAT_E7_C17_2)

                  Comment


                  • #10
                    Thank you Nick. I have been able to use that code. I will keep a note of this for future reference. Many thanks.

                    Comment

                    Working...
                    X