This module will show how how to create and run .do files in Stata.
A do file contains one or more Stata commands and provides a convenient way to perform a series of Stata commands. Let’s have a look at dofile1.do using the type command.
type dofile1.do
use auto describe summarize tabulate mpg list make price mpg
These are called do files because you run them with the do command.
do dofile1.do use auto describe Contains data from auto.dta obs: 74 vars: 12 17 Feb 1999 10:49 size: 3,108 (98.3% of memory free) ------------------------------------------------------------------------------- 1. make str17 %17s 2. price int %9.0g 3. mpg byte %9.0g 4. rep78 byte %9.0g 5. hdroom float %9.0g 6. trunk byte %9.0g 7. weight int %9.0g 8. length int %9.0g 9. turn byte %9.0g 10. displ int %9.0g 11. gratio float %9.0g 12. foreign byte %9.0g ------------------------------------------------------------------------------- Sorted by: summarize Variable | Obs Mean Std. Dev. Min Max ---------+----------------------------------------------------- make | 0 price | 74 6165.257 2949.496 3291 15906 mpg | 74 21.2973 5.785503 12 41 rep78 | 69 3.405797 .9899323 1 5 hdroom | 74 2.993243 .8459948 1.5 5 trunk | 74 13.75676 4.277404 5 23 weight | 74 3019.459 777.1936 1760 4840 length | 74 187.9324 22.26634 142 233 turn | 74 39.64865 4.399354 31 51 displ | 74 197.2973 91.83722 79 425 gratio | 74 3.014865 .4562871 2.19 3.89 foreign | 74 .2972973 .4601885 0 1 tabulate mpg mpg | Freq. Percent Cum. ------------+----------------------------------- 12 | 2 2.70 2.70 14 | 6 8.11 10.81 15 | 2 2.70 13.51 16 | 4 5.41 18.92 17 | 4 5.41 24.32 18 | 9 12.16 36.49 19 | 8 10.81 47.30 20 | 3 4.05 51.35 21 | 5 6.76 58.11 22 | 5 6.76 64.86 23 | 3 4.05 68.92 24 | 4 5.41 74.32 25 | 5 6.76 81.08 26 | 3 4.05 85.14 28 | 3 4.05 89.19 29 | 1 1.35 90.54 30 | 2 2.70 93.24 31 | 1 1.35 94.59 34 | 1 1.35 95.95 35 | 2 2.70 98.65 41 | 1 1.35 100.00 ------------+----------------------------------- Total | 74 100.00 list make price mpg make price mpg 1. AMC Concord 4099 22 2. AMC Pacer 4749 17 3. AMC Spirit 3799 22 4. Buick Century 4816 20 5. Buick Electra 7827 15 6. Buick LeSabre 5788 18 7. Buick Opel 4453 26 8. Buick Regal 5189 20 9. Buick Riviera 10372 16 10. Buick Skylark 4082 19 11. Cad. Deville 11385 14 12. Cad. Eldorado 14500 14 13. Cad. Seville 15906 21 14. Chev. Chevette 3299 29 15. Chev. Impala 5705 16 16. Chev. Malibu 4504 22 17. Chev. Monte Carlo 5104 22 18. Chev. Monza 3667 24 19. Chev. Nova 3955 19 20. Dodge Colt 3984 30 21. Dodge Diplomat 4010 18 22. Dodge Magnum 5886 16 23. Dodge St. Regis 6342 17 24. Ford Fiesta 4389 28 25. Ford Mustang 4187 21 26. Linc. Continental 11497 12 27. Linc. Mark V 13594 12 28. Linc. Versailles 13466 14 29. Merc. Bobcat 3829 22 30. Merc. Cougar 5379 14 31. Merc. Marquis 6165 15 32. Merc. Monarch 4516 18 33. Merc. XR-7 6303 14 34. Merc. Zephyr 3291 20 35. Olds 98 8814 21 36. Olds Cutl Supr 5172 19 37. Olds Cutlass 4733 19 38. Olds Delta 88 4890 18 39. Olds Omega 4181 19 40. Olds Starfire 4195 24 41. Olds Toronado 10371 16 42. Plym. Arrow 4647 28 43. Plym. Champ 4425 34 44. Plym. Horizon 4482 25 45. Plym. Sapporo 6486 26 46. Plym. Volare 4060 18 47. Pont. Catalina 5798 18 48. Pont. Firebird 4934 18 49. Pont. Grand Prix 5222 19 50. Pont. Le Mans 4723 19 51. Pont. Phoenix 4424 19 52. Pont. Sunbird 4172 24 53. Audi 5000 9690 17 54. Audi Fox 6295 23 55. BMW 320i 9735 25 56. Datsun 200 6229 23 57. Datsun 210 4589 35 58. Datsun 510 5079 24 59. Datsun 810 8129 21 60. Fiat Strada 4296 21 61. Honda Accord 5799 25 62. Honda Civic 4499 28 63. Mazda GLC 3995 30 64. Peugeot 604 12990 14 65. Renault Le Car 3895 26 66. Subaru 3798 35 67. Toyota Celica 5899 18 68. Toyota Corolla 3748 31 69. Toyota Corona 5719 18 70. VW Dasher 7140 23 71. VW Diesel 5397 41 72. VW Rabbit 4697 25 73. VW Scirocco 6850 25 74. Volvo 260 11995 17 end of do-file
As you saw, the output of the do command is displayed to the screen one screen-full at a time. Sometimes you want to save all the output so you can examine it later or print it. You can do this with the log command. The do file dofile2.do uses the log command to store the output in the file test2.log and then closes the log file when it is done. Note that the first log command uses the replace option. This is done in case you do dofile2.do a second time. Without this option, Stata would refuse to log to an existing file.
*
Also, the command set more off is used at the top of dofile2.do and set more on at the bottom. The set more off tells Stata not to stop the output and say -more- when output exceeds one screen-full and set more on restores the pausing when the program ends.
type dofile2.do set more off log using test2 , replace use auto describe summarize tabulate mpg list make price mpg log close set more on quietly * temporarily close the log * quietly capture log close
As you would expect, the output is stored in test2.log. Let’s type it out to verify that.
type test2.log use auto describe Contains data from auto.dta obs: 74 vars: 12 17 Feb 1999 10:49 size: 3,108 (98.3% of memory free) ------------------------------------------------------------------------------- 1. make str17 %17s 2. price int %9.0g 3. mpg byte %9.0g 4. rep78 byte %9.0g 5. hdroom float %9.0g 6. trunk byte %9.0g 7. weight int %9.0g 8. length int %9.0g 9. turn byte %9.0g 10. displ int %9.0g 11. gratio float %9.0g 12. foreign byte %9.0g ------------------------------------------------------------------------------- Sorted by: summarize Variable | Obs Mean Std. Dev. Min Max ---------+----------------------------------------------------- make | 0 price | 74 6165.257 2949.496 3291 15906 mpg | 74 21.2973 5.785503 12 41 rep78 | 69 3.405797 .9899323 1 5 hdroom | 74 2.993243 .8459948 1.5 5 trunk | 74 13.75676 4.277404 5 23 weight | 74 3019.459 777.1936 1760 4840 length | 74 187.9324 22.26634 142 233 turn | 74 39.64865 4.399354 31 51 displ | 74 197.2973 91.83722 79 425 gratio | 74 3.014865 .4562871 2.19 3.89 foreign | 74 .2972973 .4601885 0 1 tabulate mpg mpg | Freq. Percent Cum. ------------+----------------------------------- 12 | 2 2.70 2.70 14 | 6 8.11 10.81 15 | 2 2.70 13.51 16 | 4 5.41 18.92 17 | 4 5.41 24.32 18 | 9 12.16 36.49 19 | 8 10.81 47.30 20 | 3 4.05 51.35 21 | 5 6.76 58.11 22 | 5 6.76 64.86 23 | 3 4.05 68.92 24 | 4 5.41 74.32 25 | 5 6.76 81.08 26 | 3 4.05 85.14 28 | 3 4.05 89.19 29 | 1 1.35 90.54 30 | 2 2.70 93.24 31 | 1 1.35 94.59 34 | 1 1.35 95.95 35 | 2 2.70 98.65 41 | 1 1.35 100.00 ------------+----------------------------------- Total | 74 100.00 list make price mpg make price mpg 1. AMC Concord 4099 22 2. AMC Pacer 4749 17 3. AMC Spirit 3799 22 4. Buick Century 4816 20 5. Buick Electra 7827 15 6. Buick LeSabre 5788 18 7. Buick Opel 4453 26 8. Buick Regal 5189 20 9. Buick Riviera 10372 16 10. Buick Skylark 4082 19 11. Cad. Deville 11385 14 12. Cad. Eldorado 14500 14 13. Cad. Seville 15906 21 14. Chev. Chevette 3299 29 15. Chev. Impala 5705 16 16. Chev. Malibu 4504 22 17. Chev. Monte Carlo 5104 22 18. Chev. Monza 3667 24 19. Chev. Nova 3955 19 20. Dodge Colt 3984 30 21. Dodge Diplomat 4010 18 22. Dodge Magnum 5886 16 23. Dodge St. Regis 6342 17 24. Ford Fiesta 4389 28 25. Ford Mustang 4187 21 26. Linc. Continental 11497 12 27. Linc. Mark V 13594 12 28. Linc. Versailles 13466 14 29. Merc. Bobcat 3829 22 30. Merc. Cougar 5379 14 31. Merc. Marquis 6165 15 32. Merc. Monarch 4516 18 33. Merc. XR-7 6303 14 34. Merc. Zephyr 3291 20 35. Olds 98 8814 21 36. Olds Cutl Supr 5172 19 37. Olds Cutlass 4733 19 38. Olds Delta 88 4890 18 39. Olds Omega 4181 19 40. Olds Starfire 4195 24 41. Olds Toronado 10371 16 42. Plym. Arrow 4647 28 43. Plym. Champ 4425 34 44. Plym. Horizon 4482 25 45. Plym. Sapporo 6486 26 46. Plym. Volare 4060 18 47. Pont. Catalina 5798 18 48. Pont. Firebird 4934 18 49. Pont. Grand Prix 5222 19 50. Pont. Le Mans 4723 19 51. Pont. Phoenix 4424 19 52. Pont. Sunbird 4172 24 53. Audi 5000 9690 17 54. Audi Fox 6295 23 55. BMW 320i 9735 25 56. Datsun 200 6229 23 57. Datsun 210 4589 35 58. Datsun 510 5079 24 59. Datsun 810 8129 21 60. Fiat Strada 4296 21 61. Honda Accord 5799 25 62. Honda Civic 4499 28 63. Mazda GLC 3995 30 64. Peugeot 604 12990 14 65. Renault Le Car 3895 26 66. Subaru 3798 35 67. Toyota Celica 5899 18 68. Toyota Corolla 3748 31 69. Toyota Corona 5719 18 70. VW Dasher 7140 23 71. VW Diesel 5397 41 72. VW Rabbit 4697 25 73. VW Scirocco 6850 25 74. Volvo 260 11995 17 log close
In addition to the do command, there is also the run command. If we type run dofile2.do, it does the same thing as do test2.log, except that it runs silently, as you see below.
quietly * temporarily close the log * quietly capture log close
You can write do files using the Stata do file editor. You can bring up the editor by typing doedit and then the name of the file. For example, you can edit dofile1.do by typing doedit dofile1.do.
You can run the program by clicking the menu Tools-Do. You can mark part of the program and click Tools-Do to run just that part of the program.
Summary
*
A typical do file would be a sequence of commands like this.
set more off log using test2 , replace use auto describe summarize tabulate mpg list make price mpg log close set more on
To run do files, you can type. do test1.do
executes the Stata commands in test1.do, displays output. run test1.do
executes the Stata commands in test1.do, but displays no output.
To edit/make do files, you can type. doedit test1.do
brings up do file editor with test1.do in it (windows only). doedit
brings up do file editor for a new file (windows only)
Creating and running .do files in Stata