Stata: Visual date displays

  • %tc | mdyhms(M, D, Y, h, m, s)
  • %tc | dhms(td, h, m, s)
  • %tc | hms(h, m, s)
  • %td | mdy(M, D, Y)
  • %tw | yw(Y, W)
  • %tm | ym(Y, M)
  • %tq | yq(Y, Q)
  • %th | yh(Y, H)
  • %ty | Y

clock values (%tc) for data in format "12/03/2009 23:34"

format x = hh(x) shows hours. Mm(x) or ss(x)

gen x = mdy(m,d,y) or mdyhs

gen bdayday = day(bdaynew)

gen bdaydow = dow(bdaynew)

gen bdaymo = month(bdaynew)

gen bdayyr = year(bdaynew)

Convert date times

  • %tc to %td use dofc(x) ie from "12/03/2009 23:34" to "12/03/2009"
  • %td to %tm use mofd( ) or %td to %tq use qofd( )
  • then can apply year( ) month( ) day( )
  • doy( ) dow( ) this is day of yr from 1-365 or day of week
  • halfyear( ) quarter( ) week( ) dow ( )= sunday

%tc is milliseconds

%td is seconds

%tm is months

ALWAYS generate as double with %tc calcs:

gen double x = …..milliseconds to hours: hours (ms), minutes (ms) seconds (ms)

eg. gen double lov = hours(end – start)

Conditional date arguments

  • gen before=cond(adm<td(15jun2006),1,0
  • list if !inrange(x,2,10) lists if not in range 2 to 10
  • list age if inrange(population,200,5000)
  • gen byte x = inlist(x,"one","two")
  • egen x =rcount(v1-v4),cond(@>5 & @>15)
  • by year,sort:egen y=sum(died)

generate bdaynew=date(bday,"mdy", 2010) if data as 02/03/07

Leave a comment