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
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.
[U]16Doles5 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-lesitisnotallowedinteractively.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]16Doles7 .domyjob2.version13.usecensasfilecensas.dtanotfoundr(601);endofdo-filer(601);. WhenStatawastoldtousecensas,itrespondedwithlecensas.dtanotfoundandareturncodeof601.Statathentypedendofdo-leandrepeatedthereturncodeof601.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.5LoggingtheoutputofdolesYoulogtheoutputofdo-lesjustasyouwouldaninteractivesession;see[U]15Savingandprintingoutputlogles. [U]16Doles9 16.3Creatingandrunningdoles16.3.1CreatingandrunningdolesforWindows1.Youcanexecutedo-lesbytypingdofollowedbythelename,aswedidabove.2.Youcanexecutedo-lesbyselectingFileDo....3.YoucanusetheDo-leEditortocompose,save,andexecutedo-les;see[GSW]13UsingtheDoleEditorautomatingStata.TousetheDo-leEditor,clickontheDoleEditorbutton,ortypedoeditintheCommandwindow.StataalsohasaProjectManagerformanagingcollectionsofdo-lesandotherles.See[P]ProjectManager.4.Youcandouble-clickontheiconforthedo-letolaunchStataandopenthedo-leintheDo-leEditor.5.Youcanrunthedo-leinbatchmode.See[GSW]B.5Statabatchmodefordetails,buttheshortexplanationisthatyouopenaWindowcommandwindowandtypeC:\data"C:\ProgramFiles\Stata13\Stata"/sdomyjoborC:\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.2CreatingandrunningdolesforMac1.Youcanexecutedo-lesbytypingdofollowedbythelename,aswedidabove.2.Youcanexecutedo-lesbyselectingFileDo....3.YoucanusetheDo-leEditortocompose,save,andexecutedo-les;see[GSM]13UsingtheDoleEditorautomatingStata.ClickontheDoleEditorbutton,ortypedoeditintheCommandwindow.StataalsohasaProjectManagerformanagingcollectionsofdo-lesandotherles.See[P]ProjectManager.4.Youcandouble-clickontheiconforthedo-letoopenthedo-leintheDo-leEditor. 10[U]16Doles 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.3CreatingandrunningdolesforUnix1.Youcanexecutedo-lesbytypingdofollowedbythelename,aswedidabove.2.Youcanexecutedo-lesbyselectingFileDo....3.YoucanusetheDo-leEditortocompose,save,andexecutedo-les;see[GSU]13UsingtheDoleEditorautomatingStata.ClickontheDoleEditorbutton,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]16Doles youcouldwritethedo-le beginmyxmpl.do use`1'tabulateregionsummarizemarriage_ratedivorce_rate endmyxmpl.do andyoucouldrunthisdo-lebytyping,forinstance,.domyxmplcensus(outputomitted)Therstcommanduse`1'wouldbeinterpretedasusecensus5becausecensus5wastherstargumentyoutypedafterdomyxmpl.Anevenbetterversionofthedo-lewouldread beginmyxmpl.do argsdsnameuse`dsname'tabulateregionsummarizemarriage_ratedivorce_rate endmyxmpl.do Theargscommandmerelyassignsabetternametotheargumentpassed.argsdsnamedoesnotverifythatwhatwetypefollowingdomyxmplisalenamewewouldhavetousethesyntaxcommandifwewantedtodothatbutsubstituting`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-lecontainedonlythedenitionsofStataprogramssee[U]18ProgrammingStataandyoumerelywantedtoloadtheprogramswithoutseeingthecode,runwouldbeuseful.