Announcement

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

  • Dealing with Date variables in Stata

    Hello, I am using Stata 16.1 (MP). I have a date variable, which is stored as a numeric variable in the data. When I -tab- the variable, it shows the following (it seems like a string variable):
    Click image for larger version

Name:	QQ20230107-154954@2x.png
Views:	1
Size:	159.7 KB
ID:	1696242

    But When I codebook this variable, it has numeric values associated with each date, as follows:
    Click image for larger version

Name:	QQ20230107-155349@2x.png
Views:	1
Size:	120.1 KB
ID:	1696243

    I googled and found it is the Stata formate for date. But I am not sure how to deal with this vairbale. For example, I would like to create a categorical variable based on this variable: people who complete their degree before a certain date.
    I was wondering if anyone could help with this question.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int date
    22671
    22085
    21903
    21686
    22264
        .
        .
    21663
    21451
        .
    21626
    21686
    22095
        .
    21762
    22095
    22280
    21959
        .
    21314
        .
        .
        .
    22505
    22234
        .
    22125
    21805
        .
    22159
        .
        .
    22049
    21704
    22188
    21672
    21501
    20975
    21901
    21889
    21609
    21693
    22431
    22056
    21906
    21489
    21532
    21508
        .
    22224
    21686
        .
    22592
    22342
    22096
    21684
    20951
    22624
    21726
    21675
        .
    22415
        .
        .
    22599
    22026
        .
    21898
    21745
    21897
    22043
    21312
    22144
    22300
        .
        .
    21677
    21792
    20963
    21715
        .
    21647
        .
        .
    22234
        .
    22090
    21390
    22123
        .
        .
    22481
    21924
        .
        .
    22050
    22097
    22415
    21684
    21898
    end
    format %tdnn/dd/CCYY date

  • #2
    You should consult

    Code:
    help datetime
    for an extensive treatment of date and time variables in Stata. The first sentence in the documentation answers a good part of your questions:


    Stata dates are numeric values that record durations (positive or negative) from 01jan1960.
    In any case, the display command can be used to translate SIF values to human-readable dates and vice versa, e.g.,

    Code:
    display %td 21898
    display td(15dec2019)
    Res.:

    Code:
    . display %td 21898
    15dec2019
    
    . display td(15dec2019)
    21898
    The -td()- function from above therefore makes it easier to type dates without knowing their underlying SIF values. See

    Code:
    help td()
    Last edited by Andrew Musau; 07 Jan 2023, 02:02.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      You should consult

      Code:
      help datetime
      for an extensive treatment of date and time variables in Stata. The first sentence in the documentation answers a good part of your questions:




      In any case, the display command can be used to translate SIF values to human-readable dates and vice versa, e.g.,

      Code:
      display %td 21898
      display td(15dec2019)
      Res.:

      Code:
      . display %td 21898
      15dec2019
      
      . display td(15dec2019)
      21898
      The -td()- function from above therefore makes it easier to type dates without knowing their underlying SIF values. See

      Code:
      help td()
      Thank you so much, Andrew! It solves the problem. In case anyone has the same problem, what you need to do is to wrap the human readable dates using the brackets td() (as my date is stored in td format). In my example, if I would like to know someone who completed a degree before a certain date, I put this
      Code:
      date<td(02jul2019)
      instead of
      Code:
      date<02jul2019
      Using td() will help Stata to read the command.

      Comment


      • #4
        Code:
        ... if date < mdy(7, 2, 2019)
        is another way to do it.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Code:
          ... if date < mdy(7, 2, 2019)
          is another way to do it.
          Thank you, Nick

          Comment

          Working...
          X