Announcement

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

  • Problem with date variable

    Dear all,

    I have the following dataset, where the variable inwde captures the date when a respondent finished a survey interview (formatted as %tc).

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str2 cntry double inwde
    "SI" 1916047311000
    "SI" 1916053356000
    "SI" 1916053685000
    "SI" 1916058067000
    "SI" 1916061390000
    "SI" 1916063002000
    "SI" 1916064384000
    "SI" 1916076302000
    "SI" 1916076578000
    "SI" 1916081613000
    "SI" 1916129098000
    "SI" 1916134104000
    "SI" 1916135427000
    "SI" 1916141659000
    "SI" 1916142740000
    "SI" 1916145806000
    "SI" 1916146589000
    "SI" 1916150409000
    "SI" 1916151887000
    "SI" 1916157233000
    "SI" 1916158693000
    "SI" 1916160157000
    "SI" 1916233103000
    "SI" 1916236730000
    "SI" 1916309981000
    "SI" 1916313555000
    "SI" 1916316030000
    "SI" 1916327459000
    "SI" 1916330013000
    "SI" 1916330154000
    "SI" 1916332397000
    "SI" 1916333051000
    "SI" 1916334443000
    "SI" 1916335672000
    "SI" 1916340562000
    "SI" 1916390441000
    "SI" 1916392520000
    "SI" 1916393054000
    "SI" 1916393709000
    "SI" 1916396380000
    "SI" 1916397095000
    "SI" 1916401776000
    "SI" 1916401966000
    "SI" 1916408215000
    "SI" 1916409243000
    "SI" 1916411182000
    "SI" 1916411315000
    "SI" 1916413477000
    "SI" 1916413634000
    "SI" 1916414232000
    "SI" 1916415162000
    "SI" 1916417635000
    "SI" 1916417657000
    "SI" 1916418083000
    "SI" 1916420092000
    "SI" 1916420408000
    "SI" 1916420558000
    "SI" 1916420652000
    "SI" 1916420770000
    "SI" 1916425447000
    "SI" 1916426064000
    "SI" 1916469585000
    "SI" 1916473605000
    "SI" 1916475741000
    "SI" 1916475762000
    "SI" 1916476486000
    "SI" 1916477378000
    "SI" 1916478087000
    "SI" 1916479657000
    "SI" 1916480386000
    "SI" 1916481411000
    "SI" 1916484954000
    "SI" 1916485652000
    "SI" 1916486956000
    "SI" 1916487818000
    "SI" 1916490977000
    "SI" 1916493996000
    "SI" 1916496134000
    "SI" 1916499812000
    "SI" 1916500754000
    "SI" 1916501387000
    "SI" 1916501538000
    "SI" 1916501780000
    "SI" 1916502096000
    "SI" 1916502220000
    "SI" 1916503021000
    "SI" 1916503446000
    "SI" 1916505628000
    "SI" 1916506539000
    "SI" 1916507055000
    "SI" 1916509113000
    "SI" 1916509661000
    "SI" 1916509781000
    "SI" 1916511557000
    "SI" 1916556728000
    "SI" 1916559332000
    "SI" 1916560586000
    "SI" 1916561351000
    "SI" 1916563318000
    "SI" 1916565049000
    end
    format %tc inwde
    For example for the first respondent the date 18sep2020 11:21:51 corresponds to 1916047311000

    However, for some reason I am not able to work with these dates. For example, running the following commands return no observations:

    Code:
    .di date("18sep2020","DMY")
    22176
    
    . tab cntry if inwde == date("18sep2020","DMY")
    no observations
    
    . tab cntry if inwde == 22176
    no observations
    I need to identify respondents interviewed in a given date (I do not care about the time/hour), but I do not know how to do it. I am sure I am doing something wrong, so if you could point me in the right direction I would really appreciate it.

    Thanks!

  • #2
    Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    As you do the reading, you will recognize that your dates are stored as Stata datetime values, and from them you need to create dates stored as Stata daily date values, discarding the time portion. You can do this with something like
    Code:
    generate inwde_day = dofc(inwde)
    format %td inwde_day
    Here's how to modify the specific code you wrote, if you don't really care about the date beyond identifying certain observations.
    Code:
    . // based on your code
    . di date("18sep2020","DMY")
    22176
    
    . tab cntry if dofc(inwde) == date("18sep2020","DMY")
    
          cntry |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             SI |         10      100.00      100.00
    ------------+-----------------------------------
          Total |         10      100.00
    
    . tab cntry if dofc(inwde) == 22176
    
          cntry |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             SI |         10      100.00      100.00
    ------------+-----------------------------------
          Total |         10      100.00
    
    . // more direct
    . tab cntry if dofc(inwde) == td(18sep2020)
    
          cntry |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             SI |         10      100.00      100.00
    ------------+-----------------------------------
          Total |         10      100.00
    
    .

    Comment


    • #3
      Thanks very much William!

      Comment

      Working...
      X