/
2U24Workingwithdatesandtimes 2U24Workingwithdatesandtimes

2U24Workingwithdatesandtimes - PDF document

maisie
maisie . @maisie
Follow
343 views
Uploaded On 2021-09-22

2U24Workingwithdatesandtimes - PPT Presentation

StatarecordspuretimevaluesclocktimesindependentofdatethesamewayRatherthanthinkingofthenumericvalueasthenumberofmillisecondssince01jan1960howeverthinkofitasthenumberofmillisecondssincethebeginningofthe ID: 883115

150 000 date 479 000 150 479 date 24workingwithdatesandtimes list trading 400 week day orhalf tformats tdvariable quarter month

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "2U24Workingwithdatesandtimes" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1 2[U]24Workingwithdatesandtimes Statareco
2[U]24Workingwithdatesandtimes Statarecordspuretimevalues(clocktimesindependentofdate)thesameway.Ratherthanthinkingofthenumericvalueasthenumberofmillisecondssince01jan1960,however,thinkofitasthenumberofmillisecondssincethebeginningoftheday.Forinstance,at2p.m.everyday,theairplanetakesofffromHoustonforLondon.Thenumericvalueassociatedwith2p.m.is50,400,000becausetherearethatmanymillisecondsbetweenthebeginningoftheday(00:00:00.000)and2p.m.Theadvantageofthinkingthiswayisthatyoucanadddatesandtimes.Whatisthedatetimevalueforwhentheplanetakesoffon21nov2006?Well,21nov200600:00:00.000is1,479,686,400,000(ignoringleapseconds),and1,479,686,400,000+50,400,000is1,479,736,800,000.Subtractingdatetimevaluesisuseful,too.Howmanyhoursaretherebetween21jan19527:23a.m.and21nov20063:14p.m.?Answer:(1,479,741,240,000�(�250,706,220,000))=3,600,000=480,679.85hours.Variablesthatrecordthenumberofmillisecondssince01jan1960andignoreleaps

2 econdsarecalled%tcvariables.Variablestha
econdsarecalled%tcvariables.Variablesthatrecordthenumberofmillisecondssince01jan1960andaccountforleapsecondsarecalled%tCvariables.Statahassevenotherkindsof%tvariables.Inmanyapplications,calendardatesbythemselvesaresufcient.Theapplicantwashiredon15jan2006,forinstance.Youcouldusea%tcvariabletorecordthatvalue,assigningsomearbitrarytimethatyouwouldignore,butitisbetterandeasiertousea%tdvariable.In%tdvariables,0stillcorrespondsto01jan1960,butaunitchangenowrepresentsanentiredayratherthanamillisecond.Thevalue1represents02jan1960.Thevalue�1represents31dec1959.Whenyousubtract%tdvariables,youobtainthenumberofdaysbetweendates.Inanancialapplication,youmightuse%tqvariables.In%tq,0representstherstquarterof1960,1representsthesecondquarter,and�1representsthelastquarterof1959.Whenyousubtract%tqvariables,youobtainthenumberofquartersbetweendates.Stataunderstandsnine%tformats:FormatBaseUnitsComment %tc01jan1960m

3 illisecondsignoresleapseconds%tC01jan196
illisecondsignoresleapseconds%tC01jan1960millisecondsaccountsforleapseconds%td01jan1960dayscalendardateformat%tw1960-w1weeks52ndweekmayhavemorethan7days%tmjan1960monthscalendarmonthformat%tq1960-q1quartersnancialquarter%th1960-h1half-years1half-year=2quarters%ty0A.Dyear1960meansyear1960%tb–daysuserdened Allformatsexcept%tyand%tbarebasedonthebeginningofJanuary1960.Thevalue0meanstherstmillisecond,day,week,month,quarter,orhalf-yearof1960,dependingonformat.Thevalue1isthemillisecond,day,week,month,quarter,orhalf-yearafterthat.Thevalue�1isthemillisecond,day,week,month,quarter,orhalf-yearbeforethat.Stata's%tyformatrecordsyearsasnumericvaluesanditcodesthemthenaturalway:ratherthan0meaning1960,1960means1960,andso2006alsomeans2006. 4[U]24Workingwithdatesandtimes .list namebdayx 1. Bill21Jan195222 2. May11Jul194818 3. Sam12Nov196025 4. Kay9Aug197516 Thedatalookne,butifwesetaboutusingthem,wewouldqu

4 icklydiscoverthereisnotmuchwecoulddowith
icklydiscoverthereisnotmuchwecoulddowithvariablebday.Variablebdaylookslikeadate,butitisjustastring.Weneedtoturnbdayintoa%tvariablethatStataunderstands:.genbirthday=date(bday,"DMY").list namebdayxbirthday 1. Bill21Jan195222-2902 2. May11Jul194818-4191 3. Sam12Nov196025316 4. Kay9Aug1975165699 Newvariablebirthdayisa%tdvariable.Theproblemnowisthat,whereasthenewvariableisperfectlyunderstandabletoStata,itisnotunderstandabletous.Naturallyenough,a%tdvariableneedsa%tdformat:.formatbirthday%td.list namebdayxbirthday 1. Bill21Jan19522221jan1952 2. May11Jul19481811jul1948 3. Sam12Nov19602512nov1960 4. Kay9Aug19751609aug1975 Usingournew%tdvariable,wecancreateavariablerecordinghowoldeachofthesesubjectswason01jan2000:.genage2000=(td(1jan2000)-birthday)/365.25.list namebdayxbirthdayage2000 1. Bill21Jan19522221jan195247.94524 2. May11Jul19481811jul194851.47433 3. Sam12Nov19602512nov196039.13484 4. Kay9Aug19751609aug197524.39

5 699 td()isafunctionthatmakesiteasytotype
699 td()isafunctionthatmakesiteasytotype%tddates.Therearealsofunctionstc(),tC(),tw(),tm(),tq(),andth()fortheother%tformats;see[D]datetime. 6[U]24Workingwithdatesandtimes lookslikewithdefaultformatting.Ifwewantedtoseethenumericvaluesbetter,wecouldchangedttohavea%20.0gcformat.Wewouldthenseethattherstvalueis1,479,113,983,000,thesecond1,479,195,409,000,andsoon.Wewillnotdothat.Instead,wewillputa%tcformatonour%tcvariable:.formatdt%tc.listiddtaction iddtaction 1. 100114nov200608:59:4315 2. 100215nov200607:36:4915 3. 100315nov200609:21:0711 4. 100215nov200614:57:3616 5. 100516nov200608:22:5312 6. 100116nov200608:36:4416 Variabledtisavariablewecanuse.Saywewantedtoknowhowmanyhoursithadbeensincethepreviousaction:.sortdt.genhours=hours(dt-dt[_n-1])(1missingvaluegenerated).formathours%9.2f.listiddtactionhours iddtactionhours 1. 100114nov200608:59:4315. 2. 100215nov200607:36:491522.62 3. 100315nov200609:21:07111.74 4.

6 100215nov200614:57:36165.61 5. 100516nov
100215nov200614:57:36165.61 5. 100516nov200608:22:531217.42 6. 100116nov200608:36:44160.23 Wesubtractedthepreviousvalueofdtfromdt,whichresultsinthenumberofmilliseconds.Convertingmillisecondstohoursiseasyenough:wejusthavetodivideby60601,000=3,600,000.Itiseasytoforgetormistypethatconstant,soweusedStata'shours()function,whichconvertsmillisecondstohours.hours(),andotherusefulfunctions,aredocumentedin[D]datetime.24.3DisplayingdatesandtimesA%tdvariableshouldhavea%tdformat,a%tcvariableshouldhavea%tcformat,andsoonforalltheother%tvariable–formatpairs.Youdothatbytypingformatvarname%td,formatvarname%tc,etc.Formats%tc,%tC,%td,%tw,%tm,%tq,%th,and%tyarecalledthedefault%tformats.Byspecifyingcodesfollowingthem,youcancontrolhowthevariableistobedisplayed.Inthepreviousexample,westartedwithastringvariablethatcontainedatimestampandlookedlike“TueNov1408:59:43CST2006”.Aftercreatinga%tcvariablefromit,andputtin

7 gthedefault%tcformatonit,ourdatetimesloo
gthedefault%tcformatonit,ourdatetimeslookedlike“14nov200608:59:43”.Belowwespecifya%tcformatthatmakesournewvariablelookjustliketheoriginal: 8[U]24Workingwithdatesandtimes 24.7BusinessdatesandcalendarsInadditiontothebuilt-indatetypesabove,suchas%tcand%td,Stataprovidesatypeyoucandene,denotedas%tbandcalledbusinessdatesandcalendars.Abusinesscalendarislikeanordinarycalendarwithsomedatescrossedout.Thecrossed-outdatescorrespondtothedatesonwhichthebusinessisclosed:November2011SuMoTuWeThFrSa 1234XX7891011XX141516171819X212223X25XX282930 Withrespecttoabusinessdate,yesterdayisthelastdaythebusinesswasopen,andtomorrowisthenextdaythebusinesswillbeopen.Considerdate=25nov2011.Ifdateisaregular(%td)datevariable,yesterday=date�1=24nov2011tomorrow=date+1=26nov2011Ifdateisabusiness(%tb)datevariable,yesterday=date�1=23nov2011tomorrow=date+1=28nov2011ThatisimportantbecausevariablescontainingdatesareoftenusedwithS

8 tata'slagandleadoperators;see[U]13.9Time
tata'slagandleadoperators;see[U]13.9Time­seriesoperators.Ifvariabletrading dateisanordinary%tdvari-able,thenL.trading datereallyisyesterdayandF.trading datereallyistomorrow.Butiftrading datehasanappropriatelydened%tbformat,L.trading dateistheprevioustradingdateandF.trading dateisthenexttradingdate.Youcanusebcalcreatetocreateabusinesscalendarbasedonthecurrentdataset.Alternatively,youcancreatealenamedcalname.stbcal,suchasnyse.stbcal.Afterthat,Statadeeplyunderstandsthenewformat%tbnyse.Formoreinformation,see[D]datetimebusinesscalendars.Businessdatesworkjustlikeregulardates,itisjustthatsomedatesarecrossedout.24.8ReferencesCox,N.J.2006.SpeakingStata:Timeofday.StataJournal6:124–137. .2010.Statatip68:Weekassumptions.StataJournal10:682–685. .2012.Statatip111:Moreonworkingwithweeks.StataJournal12:565–569.Samuels,S.J.,andN.J.Cox.2012.Statatip105:Dailydateswithmissingdays.StataJournal12:159–1

Related Contents


Next Show more