/
 Doles Contents  Doles Contents

Doles Contents - PDF document

pasty-toler
pasty-toler . @pasty-toler
Follow
448 views
Uploaded On 2014-11-10

Doles Contents - PPT Presentation

1 Description 1611 Version 1612 Comments and blank lines in do64257les 1613 Long lines in do64257les 1614 Error handling in do64257les 1615 Logging the output of do64257les 1616 Preventing more condit ID: 9728

Description 1611 Version 1612

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document " Doles Contents " 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

[U]16Do­les5 Example2Inthefollowingfragmentofado-le,wetemporarilychangetheend-of-linedelimiter: fragmentofexample.do usemydata#delimit;summarizeweightpricedisplheadroomrep78lengthturngear_ratioifsubstr(company,1,4)=="Ford"|substr(company,1,2)=="GM",detail;genbyteford=substr(company,1,4)=="Ford";#delimitcrgenbytegm=substr(company,1,2)=="GM" fragmentofexample.do Oncewechangethelinedelimitertosemicolon,alllines,evenshortones,mustendinsemicolons.Statatreatscarriagereturnsasnodifferentfromblanks.Wecanchangethedelimiterbacktocarriagereturnbytyping#delimitcr.The#delimitcommandisallowedonlyindo-les—itisnotallowedinteractively.Youneednotremembertosetthedelimiterbacktocarriagereturnattheendofado-lebecauseStatawillresetitautomatically. Example3Theotherwayaroundlonglinesistocommentoutthecarriagereturnbyusing/**/commentbracketsortousethe///line-joinindicator.Thusourcodefragmentcouldalsoread fragmentofexample.do usemydatasummarizeweightpricedisplheadroomrep78lengthturngear_ratio/**/ifsubstr(company,1,4)=="Ford"|/**/substr(company,1,2)=="GM",detailgenbyteford=substr(company,1,4)=="Ford"genbytegm=substr(company,1,2)=="GM" fragmentofexample.do or fragmentofexample.do usemydatasummarizeweightpricedisplheadroomrep78lengthturngear_ratio///ifsubstr(company,1,4)=="Ford"|///substr(company,1,2)=="GM",detailgenbyteford=substr(company,1,4)=="Ford"genbytegm=substr(company,1,2)=="GM" fragmentofexample.do [U]16Do­les7 .domyjob2.version13.usecensasfilecensas.dtanotfoundr(601);endofdo-filer(601);. WhenStatawastoldtousecensas,itrespondedwith“lecensas.dtanotfound”andareturncodeof601.Statathentyped“endofdo-le”andrepeatedthereturncodeof601.TherepeatedmessageoccurredforthesamereasonitdidwhenwepressedBreakinthepreviousexample.Theuseresultedinareturncodeof601,sothedo-leitselfresultedinthesamereturncode.TheimportantthingtounderstandisthatStatastoppedexecutingthelebecausetherewasanerror. TechnicalnoteWecantellStatatocontinueexecutingtheleevenifthereareerrorsbytypingdolename,nostop.Hereistheresult:.domyjob2,nostop.version13.usecensasfilecensas.dtanotfoundr(601);.tabulateregionnovariablesdefinedr(111);summarizemarriage_ratedivorce_ratemedian_ageifstate!="Nevada"novariablesdefinedr(111);endofdo-file. Noneofthecommandsworkedbecausethedo-le'srstcommandfailed.ThatiswhyStataordinarilystops.However,ifourlehadcontainedanythingthatcouldwork,itwouldhaveworked.Ingeneral,wedonotrecommendcodinginthismanner,asunintendedconsequencescanresultwhenerrorsdonotstopexecution. 16.1.5Loggingtheoutputofdo­lesYoulogtheoutputofdo-lesjustasyouwouldaninteractivesession;see[U]15Savingandprintingoutput—logles. [U]16Do­les9 16.3Creatingandrunningdo­les16.3.1Creatingandrunningdo­lesforWindows1.Youcanexecutedo-lesbytypingdofollowedbythelename,aswedidabove.2.Youcanexecutedo-lesbyselectingFile�Do....3.YoucanusetheDo-leEditortocompose,save,andexecutedo-les;see[GSW]13UsingtheDo­leEditor—automatingStata.TousetheDo-leEditor,clickontheDo­leEditorbutton,ortypedoeditintheCommandwindow.StataalsohasaProjectManagerformanagingcollectionsofdo-lesandotherles.See[P]ProjectManager.4.Youcandouble-clickontheiconforthedo-letolaunchStataandopenthedo-leintheDo-leEditor.5.Youcanrunthedo-leinbatchmode.See[GSW]B.5Statabatchmodefordetails,buttheshortexplanationisthatyouopenaWindowcommandwindowandtype�C:\data"C:\ProgramFiles\Stata13\Stata"/sdomyjobor�C:\data"C:\ProgramFiles\Stata13\Stata"/bdomyjobtoruninbatchmode,assumingthatyouhaveinstalledStatainthefolderC:\ProgramFiles\Stata13./band/sdeterminethekindoflogproduced,butputthatasideforasecond.WhenyoustartStataintheseways,Statawillruninthebackground.Whenthedo-lecompletes,theStataicononthetaskbarwillash.YoucanthenclickonittocloseStata.Ifyouwanttostopthedo-lebeforeitcompletes,clickontheStataicononthetaskbar,andStatawillaskyouifyouwanttocancelthejob.IfyouwantStatatoexitwhenthedo-leiscompleteratherthanashingonthetaskbar,alsospecify/eonthecommandline.Tologtheoutput,youcanstartthelogbeforeexecutingthedo-leoryoucanincludethelogusingandlogcloseinyourdo-le.WhenyourunStataintheseways,Statatakesthefollowingactions:a.Stataautomaticallyopensalog.Ifyouspecied/s,StatawillopenaSMCLlog;ifyouspecied/b,Statawillopenaplaintextlog.Ifyourdo-leisnamedxyz.do,thelogwillbecalledxyz.smcl(/s)orxyz.log(/b)inthesamedirectory.b.Ifyourdo-leexplicitlyopensanotherlog,Statawillsavetwocopiesoftheoutput.c.Stataignores more conditionsandanythingelsethatwouldcausethedo-letostopwereitrunninginteractively.16.3.2Creatingandrunningdo­lesforMac1.Youcanexecutedo-lesbytypingdofollowedbythelename,aswedidabove.2.Youcanexecutedo-lesbyselectingFile�Do....3.YoucanusetheDo-leEditortocompose,save,andexecutedo-les;see[GSM]13UsingtheDo­leEditor—automatingStata.ClickontheDo­leEditorbutton,ortypedoeditintheCommandwindow.StataalsohasaProjectManagerformanagingcollectionsofdo-lesandotherles.See[P]ProjectManager.4.Youcandouble-clickontheiconforthedo-letoopenthedo-leintheDo-leEditor. 10[U]16Do­les 5.Double-clickingontheiconforado-lenamedStata.dowilllaunchStataifitisnotalreadyrunningandsetthecurrentworkingdirectorytothelocationofthedo-le.6.Youcanrunthedo-leinbatchmode.See[GSM]B.3Statabatchmodefordetails,buttheshortexplanationisthatyouopenaTerminalwindowandtype%/Applications/Stata/Stata.app/Contents/MacOS/Stata-sdomyjobor%/Applications/Stata/Stata.app/Contents/MacOS/Stata-bdomyjobtoruninbatchmode,assumingthatyouhaveinstalledStata/ICinthefolder/Applications/Stata.-band-sdeterminethekindoflogproduced,butputthatasideforasecond.WhenyoustartStataintheseways,Statawillruninthebackground.Whenthedo-lecompletes,theStataiconontheDockwillbounceuntilyouputStataintotheforeground.YoucanthenexitStata.Ifyouwanttostopthedo-lebeforeitcompletes,right-clickontheStataiconontheDock,andselectQuit.Tologtheoutput,youcanstartthelogbeforeexecutingthedo-leoryoucanincludethelogusingandlogcloseinyourdo-le.WhenyourunStataintheseways,Statatakesthefollowingactions:a.Stataautomaticallyopensalog.Ifyouspecied-s,StatawillopenaSMCLlog;ifyouspecied-b,Statawillopenaplaintextlog.Ifyourdo-leisnamedxyz.do,thelogwillbecalledxyz.smcl(-s)orxyz.log(-b)inthesamedirectory.b.Ifyourdo-leexplicitlyopensanotherlog,Statawillsavetwocopiesoftheoutput.c.Stataignores more conditionsandanythingelsethatwouldcausethedo-letostopwereitrunninginteractively.16.3.3Creatingandrunningdo­lesforUnix1.Youcanexecutedo-lesbytypingdofollowedbythelename,aswedidabove.2.Youcanexecutedo-lesbyselectingFile�Do....3.YoucanusetheDo-leEditortocompose,save,andexecutedo-les;see[GSU]13UsingtheDo­leEditor—automatingStata.ClickontheDo­leEditorbutton,ortypedoeditintheCommandwindow.StataalsohasaProjectManagerformanagingcollectionsofdo-lesandotherles.See[P]ProjectManager.4.AttheUnixprompt,youcantype$xstatadolenameor$statadolenametolaunchStataandrunthedo-le.Whenthedo-lecompletes,StatawillpromptyouforthenextcommandjustasifyouhadstartedStatathenormalway.IfyouwantStatatoexitinstead,includeexit,STATAclearasthelastlineofyourdo-le.Tologtheoutput,youcanstartthelogbeforeexecutingthedo-leoryoucanincludethelogusingandlogcloseinyourdo-le. 12[U]16Do­les youcouldwritethedo-le beginmyxmpl.do use`1'tabulateregionsummarizemarriage_ratedivorce_rate endmyxmpl.do andyoucouldrunthisdo-lebytyping,forinstance,.domyxmplcensus(outputomitted)Therstcommand—use`1'—wouldbeinterpretedasusecensus5becausecensus5wastherstargumentyoutypedafterdomyxmpl.Anevenbetterversionofthedo-lewouldread beginmyxmpl.do argsdsnameuse`dsname'tabulateregionsummarizemarriage_ratedivorce_rate endmyxmpl.do Theargscommandmerelyassignsabetternametotheargumentpassed.argsdsnamedoesnotverifythatwhatwetypefollowingdomyxmplisalename—wewouldhavetousethesyntaxcommandifwewantedtodothat—butsubstituting`dsname'for`1'doesmakethecodemorereadable.Ifourprogramweretoreceivetwoarguments,wecouldrefertothemas`1'and`2',orwecouldputan`argsdsnameother'atthetopofourdo-leandthenreferto`dsname'and`other'.Tolearnmoreaboutargumentpassing,see[U]18.4Programarguments.Baum(2009)providesmanyexamplesandtipsrelatedtodo-les.16.4.2SuppressingoutputThereisanalternativetotypingdolename;itisrunlename.runworksinthesamewayasdo,exceptthatneithertheinstructionsinthelenoranyoftheoutputcausedbythoseinstructionsisshownonthescreenorinthelogle.Forinstance,withtheabovemyxmpl.do,typingrunmyxmplcensus5resultsin.runmyxmplcensus. Alltheinstructionswereexecuted,butnoneoftheoutputwasshown.Thisisnotusefulhere,butifthedo-lecontainedonlythedenitionsofStataprograms—see[U]18ProgrammingStata—andyoumerelywantedtoloadtheprogramswithoutseeingthecode,runwouldbeuseful.