sysuse auto
(1978 automobile data)
Introduction to Stata
If you haven’t already, load the automobile data set that comes with Stata by running:
Almost all Stata commands use a standard syntax. This syntax allows you to control what part of the data set the command acts on, modify what the command does, and more.
We’ll discuss five syntax elements:
Stata is a command-based language. Most Stata commands are verbs. They tell Stata to do something: summarize
, tabulate
, regress
, etc. Normally the command itself comes first and then you tell Stata the details of what you want it to do after.
Many commands can be abbreviated: sum
instead of summarize
, tab
instead of tabulate
, reg
instead of regress
. Commands that can destroy data, like replace
, cannot be abbreviated.
We’ll explore the elements of Stata syntax using a command that makes it easy to see what they do and works well in a web book, list
. It lists your data set in the Results window. (I apologize for all the scrolling you’ll need to do in this chapter! If anyone from Quarto is reading this, “output folding” would be very helpful.)
+----------------------------------------------------------------------+
1. | make | price | mpg | rep78 | headroom | trunk | weight |
| AMC Concord | 4,099 | 22 | 3 | 2.5 | 11 | 2,930 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 186 | 40 | 121 | 3.58 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
2. | make | price | mpg | rep78 | headroom | trunk | weight |
| AMC Pacer | 4,749 | 17 | 3 | 3.0 | 11 | 3,350 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 173 | 40 | 258 | 2.53 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
3. | make | price | mpg | rep78 | headroom | trunk | weight |
| AMC Spirit | 3,799 | 22 | . | 3.0 | 12 | 2,640 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 168 | 35 | 121 | 3.08 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
4. | make | price | mpg | rep78 | headroom | trunk | weight |
| Buick Century | 4,816 | 20 | 3 | 4.5 | 16 | 3,250 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 196 | 40 | 196 | 2.93 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
5. | make | price | mpg | rep78 | headroom | trunk | weight |
| Buick Electra | 7,827 | 15 | 4 | 4.0 | 20 | 4,080 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 222 | 43 | 350 | 2.41 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
6. | make | price | mpg | rep78 | headroom | trunk | weight |
| Buick LeSabre | 5,788 | 18 | 3 | 4.0 | 21 | 3,670 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 218 | 43 | 231 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
7. | make | price | mpg | rep78 | headroom | trunk | weight |
| Buick Opel | 4,453 | 26 | . | 3.0 | 10 | 2,230 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 170 | 34 | 304 | 2.87 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
8. | make | price | mpg | rep78 | headroom | trunk | weight |
| Buick Regal | 5,189 | 20 | 3 | 2.0 | 16 | 3,280 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 200 | 42 | 196 | 2.93 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
9. | make | price | mpg | rep78 | headroom | trunk | weight |
| Buick Riviera | 10,372 | 16 | 3 | 3.5 | 17 | 3,880 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 207 | 43 | 231 | 2.93 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
10. | make | price | mpg | rep78 | headroom | trunk | weight |
| Buick Skylark | 4,082 | 19 | 3 | 3.5 | 13 | 3,400 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 200 | 42 | 231 | 3.08 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
11. | make | price | mpg | rep78 | headroom | trunk | weight |
| Cad. Deville | 11,385 | 14 | 3 | 4.0 | 20 | 4,330 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 221 | 44 | 425 | 2.28 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
12. | make | price | mpg | rep78 | headroom | trunk | weight |
| Cad. Eldorado | 14,500 | 14 | 2 | 3.5 | 16 | 3,900 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 204 | 43 | 350 | 2.19 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
13. | make | price | mpg | rep78 | headroom | trunk | weight |
| Cad. Seville | 15,906 | 21 | 3 | 3.0 | 13 | 4,290 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 204 | 45 | 350 | 2.24 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
14. | make | price | mpg | rep78 | headroom | trunk | weight |
| Chev. Chevette | 3,299 | 29 | 3 | 2.5 | 9 | 2,110 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 163 | 34 | 231 | 2.93 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
15. | make | price | mpg | rep78 | headroom | trunk | weight |
| Chev. Impala | 5,705 | 16 | 4 | 4.0 | 20 | 3,690 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 212 | 43 | 250 | 2.56 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
16. | make | price | mpg | rep78 | headroom | trunk | weight |
| Chev. Malibu | 4,504 | 22 | 3 | 3.5 | 17 | 3,180 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 193 | 31 | 200 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
17. | make | price | mpg | rep78 | headroom | trunk | weight |
| Chev. Monte Carlo | 5,104 | 22 | 2 | 2.0 | 16 | 3,220 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 200 | 41 | 200 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
18. | make | price | mpg | rep78 | headroom | trunk | weight |
| Chev. Monza | 3,667 | 24 | 2 | 2.0 | 7 | 2,750 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 179 | 40 | 151 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
19. | make | price | mpg | rep78 | headroom | trunk | weight |
| Chev. Nova | 3,955 | 19 | 3 | 3.5 | 13 | 3,430 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 197 | 43 | 250 | 2.56 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
20. | make | price | mpg | rep78 | headroom | trunk | weight |
| Dodge Colt | 3,984 | 30 | 5 | 2.0 | 8 | 2,120 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 163 | 35 | 98 | 3.54 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
21. | make | price | mpg | rep78 | headroom | trunk | weight |
| Dodge Diplomat | 4,010 | 18 | 2 | 4.0 | 17 | 3,600 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 206 | 46 | 318 | 2.47 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
22. | make | price | mpg | rep78 | headroom | trunk | weight |
| Dodge Magnum | 5,886 | 16 | 2 | 4.0 | 17 | 3,600 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 206 | 46 | 318 | 2.47 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
23. | make | price | mpg | rep78 | headroom | trunk | weight |
| Dodge St. Regis | 6,342 | 17 | 2 | 4.5 | 21 | 3,740 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 220 | 46 | 225 | 2.94 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
24. | make | price | mpg | rep78 | headroom | trunk | weight |
| Ford Fiesta | 4,389 | 28 | 4 | 1.5 | 9 | 1,800 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 147 | 33 | 98 | 3.15 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
25. | make | price | mpg | rep78 | headroom | trunk | weight |
| Ford Mustang | 4,187 | 21 | 3 | 2.0 | 10 | 2,650 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 179 | 43 | 140 | 3.08 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
26. | make | price | mpg | rep78 | headroom | trunk | weight |
| Linc. Continental | 11,497 | 12 | 3 | 3.5 | 22 | 4,840 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 233 | 51 | 400 | 2.47 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
27. | make | price | mpg | rep78 | headroom | trunk | weight |
| Linc. Mark V | 13,594 | 12 | 3 | 2.5 | 18 | 4,720 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 230 | 48 | 400 | 2.47 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
28. | make | price | mpg | rep78 | headroom | trunk | weight |
| Linc. Versailles | 13,466 | 14 | 3 | 3.5 | 15 | 3,830 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 201 | 41 | 302 | 2.47 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
29. | make | price | mpg | rep78 | headroom | trunk | weight |
| Merc. Bobcat | 3,829 | 22 | 4 | 3.0 | 9 | 2,580 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 169 | 39 | 140 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
30. | make | price | mpg | rep78 | headroom | trunk | weight |
| Merc. Cougar | 5,379 | 14 | 4 | 3.5 | 16 | 4,060 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 221 | 48 | 302 | 2.75 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
31. | make | price | mpg | rep78 | headroom | trunk | weight |
| Merc. Marquis | 6,165 | 15 | 3 | 3.5 | 23 | 3,720 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 212 | 44 | 302 | 2.26 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
32. | make | price | mpg | rep78 | headroom | trunk | weight |
| Merc. Monarch | 4,516 | 18 | 3 | 3.0 | 15 | 3,370 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 198 | 41 | 250 | 2.43 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
33. | make | price | mpg | rep78 | headroom | trunk | weight |
| Merc. XR-7 | 6,303 | 14 | 4 | 3.0 | 16 | 4,130 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 217 | 45 | 302 | 2.75 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
34. | make | price | mpg | rep78 | headroom | trunk | weight |
| Merc. Zephyr | 3,291 | 20 | 3 | 3.5 | 17 | 2,830 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 195 | 43 | 140 | 3.08 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
35. | make | price | mpg | rep78 | headroom | trunk | weight |
| Olds 98 | 8,814 | 21 | 4 | 4.0 | 20 | 4,060 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 220 | 43 | 350 | 2.41 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
36. | make | price | mpg | rep78 | headroom | trunk | weight |
| Olds Cutl Supr | 5,172 | 19 | 3 | 2.0 | 16 | 3,310 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 198 | 42 | 231 | 2.93 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
37. | make | price | mpg | rep78 | headroom | trunk | weight |
| Olds Cutlass | 4,733 | 19 | 3 | 4.5 | 16 | 3,300 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 198 | 42 | 231 | 2.93 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
38. | make | price | mpg | rep78 | headroom | trunk | weight |
| Olds Delta 88 | 4,890 | 18 | 4 | 4.0 | 20 | 3,690 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 218 | 42 | 231 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
39. | make | price | mpg | rep78 | headroom | trunk | weight |
| Olds Omega | 4,181 | 19 | 3 | 4.5 | 14 | 3,370 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 200 | 43 | 231 | 3.08 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
40. | make | price | mpg | rep78 | headroom | trunk | weight |
| Olds Starfire | 4,195 | 24 | 1 | 2.0 | 10 | 2,730 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 180 | 40 | 151 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
41. | make | price | mpg | rep78 | headroom | trunk | weight |
| Olds Toronado | 10,371 | 16 | 3 | 3.5 | 17 | 4,030 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 206 | 43 | 350 | 2.41 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
42. | make | price | mpg | rep78 | headroom | trunk | weight |
| Plym. Arrow | 4,647 | 28 | 3 | 2.0 | 11 | 3,260 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 170 | 37 | 156 | 3.05 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
43. | make | price | mpg | rep78 | headroom | trunk | weight |
| Plym. Champ | 4,425 | 34 | 5 | 2.5 | 11 | 1,800 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 157 | 37 | 86 | 2.97 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
44. | make | price | mpg | rep78 | headroom | trunk | weight |
| Plym. Horizon | 4,482 | 25 | 3 | 4.0 | 17 | 2,200 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 165 | 36 | 105 | 3.37 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
45. | make | price | mpg | rep78 | headroom | trunk | weight |
| Plym. Sapporo | 6,486 | 26 | . | 1.5 | 8 | 2,520 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 182 | 38 | 119 | 3.54 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
46. | make | price | mpg | rep78 | headroom | trunk | weight |
| Plym. Volare | 4,060 | 18 | 2 | 5.0 | 16 | 3,330 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 201 | 44 | 225 | 3.23 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
47. | make | price | mpg | rep78 | headroom | trunk | weight |
| Pont. Catalina | 5,798 | 18 | 4 | 4.0 | 20 | 3,700 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 214 | 42 | 231 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
48. | make | price | mpg | rep78 | headroom | trunk | weight |
| Pont. Firebird | 4,934 | 18 | 1 | 1.5 | 7 | 3,470 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 198 | 42 | 231 | 3.08 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
49. | make | price | mpg | rep78 | headroom | trunk | weight |
| Pont. Grand Prix | 5,222 | 19 | 3 | 2.0 | 16 | 3,210 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 201 | 45 | 231 | 2.93 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
50. | make | price | mpg | rep78 | headroom | trunk | weight |
| Pont. Le Mans | 4,723 | 19 | 3 | 3.5 | 17 | 3,200 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 199 | 40 | 231 | 2.93 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
51. | make | price | mpg | rep78 | headroom | trunk | weight |
| Pont. Phoenix | 4,424 | 19 | . | 3.5 | 13 | 3,420 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 203 | 43 | 231 | 3.08 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
52. | make | price | mpg | rep78 | headroom | trunk | weight |
| Pont. Sunbird | 4,172 | 24 | 2 | 2.0 | 7 | 2,690 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 179 | 41 | 151 | 2.73 | Domestic |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
53. | make | price | mpg | rep78 | headroom | trunk | weight |
| Audi 5000 | 9,690 | 17 | 5 | 3.0 | 15 | 2,830 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 189 | 37 | 131 | 3.20 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
54. | make | price | mpg | rep78 | headroom | trunk | weight |
| Audi Fox | 6,295 | 23 | 3 | 2.5 | 11 | 2,070 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 174 | 36 | 97 | 3.70 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
55. | make | price | mpg | rep78 | headroom | trunk | weight |
| BMW 320i | 9,735 | 25 | 4 | 2.5 | 12 | 2,650 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 177 | 34 | 121 | 3.64 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
56. | make | price | mpg | rep78 | headroom | trunk | weight |
| Datsun 200 | 6,229 | 23 | 4 | 1.5 | 6 | 2,370 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 170 | 35 | 119 | 3.89 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
57. | make | price | mpg | rep78 | headroom | trunk | weight |
| Datsun 210 | 4,589 | 35 | 5 | 2.0 | 8 | 2,020 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 165 | 32 | 85 | 3.70 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
58. | make | price | mpg | rep78 | headroom | trunk | weight |
| Datsun 510 | 5,079 | 24 | 4 | 2.5 | 8 | 2,280 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 170 | 34 | 119 | 3.54 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
59. | make | price | mpg | rep78 | headroom | trunk | weight |
| Datsun 810 | 8,129 | 21 | 4 | 2.5 | 8 | 2,750 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 184 | 38 | 146 | 3.55 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
60. | make | price | mpg | rep78 | headroom | trunk | weight |
| Fiat Strada | 4,296 | 21 | 3 | 2.5 | 16 | 2,130 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 161 | 36 | 105 | 3.37 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
61. | make | price | mpg | rep78 | headroom | trunk | weight |
| Honda Accord | 5,799 | 25 | 5 | 3.0 | 10 | 2,240 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 172 | 36 | 107 | 3.05 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
62. | make | price | mpg | rep78 | headroom | trunk | weight |
| Honda Civic | 4,499 | 28 | 4 | 2.5 | 5 | 1,760 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 149 | 34 | 91 | 3.30 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
63. | make | price | mpg | rep78 | headroom | trunk | weight |
| Mazda GLC | 3,995 | 30 | 4 | 3.5 | 11 | 1,980 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 154 | 33 | 86 | 3.73 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
64. | make | price | mpg | rep78 | headroom | trunk | weight |
| Peugeot 604 | 12,990 | 14 | . | 3.5 | 14 | 3,420 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 192 | 38 | 163 | 3.58 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
65. | make | price | mpg | rep78 | headroom | trunk | weight |
| Renault Le Car | 3,895 | 26 | 3 | 3.0 | 10 | 1,830 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 142 | 34 | 79 | 3.72 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
66. | make | price | mpg | rep78 | headroom | trunk | weight |
| Subaru | 3,798 | 35 | 5 | 2.5 | 11 | 2,050 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 164 | 36 | 97 | 3.81 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
67. | make | price | mpg | rep78 | headroom | trunk | weight |
| Toyota Celica | 5,899 | 18 | 5 | 2.5 | 14 | 2,410 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 174 | 36 | 134 | 3.06 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
68. | make | price | mpg | rep78 | headroom | trunk | weight |
| Toyota Corolla | 3,748 | 31 | 5 | 3.0 | 9 | 2,200 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 165 | 35 | 97 | 3.21 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
69. | make | price | mpg | rep78 | headroom | trunk | weight |
| Toyota Corona | 5,719 | 18 | 5 | 2.0 | 11 | 2,670 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 175 | 36 | 134 | 3.05 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
70. | make | price | mpg | rep78 | headroom | trunk | weight |
| VW Dasher | 7,140 | 23 | 4 | 2.5 | 12 | 2,160 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 172 | 36 | 97 | 3.74 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
71. | make | price | mpg | rep78 | headroom | trunk | weight |
| VW Diesel | 5,397 | 41 | 5 | 3.0 | 15 | 2,040 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 155 | 35 | 90 | 3.78 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
72. | make | price | mpg | rep78 | headroom | trunk | weight |
| VW Rabbit | 4,697 | 25 | 4 | 3.0 | 15 | 1,930 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 155 | 35 | 89 | 3.78 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
73. | make | price | mpg | rep78 | headroom | trunk | weight |
| VW Scirocco | 6,850 | 25 | 4 | 2.0 | 16 | 1,990 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 156 | 36 | 97 | 3.78 | Foreign |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
74. | make | price | mpg | rep78 | headroom | trunk | weight |
| Volvo 260 | 11,995 | 17 | 5 | 2.5 | 14 | 3,170 |
|----------------------------------------------------------------------|
| length | turn | displa~t | gear_r~o | foreign |
| 193 | 37 | 163 | 2.98 | Foreign |
+----------------------------------------------------------------------+
For most purposes browse
is more useful, so feel free to replace list
with browse
in the examples. It opens the Data Editor in browse mode, which is the mode you should always use unless you’re doing data entry. Browse mode won’t let you accidentally change your data.
Looking at your data is a great way to get a basic understanding of it, but even with this small data set you don’t want to look at all of it. The key to using list
or the data browser effectively is being able to view the parts of the data set you care about, and the next two syntax elements will help us do that.
Listing one or more variables after a command tells the command it should only act on the variables listed:
+-------------------+
| make |
|-------------------|
1. | AMC Concord |
2. | AMC Pacer |
3. | AMC Spirit |
4. | Buick Century |
5. | Buick Electra |
|-------------------|
6. | Buick LeSabre |
7. | Buick Opel |
8. | Buick Regal |
9. | Buick Riviera |
10. | Buick Skylark |
|-------------------|
11. | Cad. Deville |
12. | Cad. Eldorado |
13. | Cad. Seville |
14. | Chev. Chevette |
15. | Chev. Impala |
|-------------------|
16. | Chev. Malibu |
17. | Chev. Monte Carlo |
18. | Chev. Monza |
19. | Chev. Nova |
20. | Dodge Colt |
|-------------------|
21. | Dodge Diplomat |
22. | Dodge Magnum |
23. | Dodge St. Regis |
24. | Ford Fiesta |
25. | Ford Mustang |
|-------------------|
26. | Linc. Continental |
27. | Linc. Mark V |
28. | Linc. Versailles |
29. | Merc. Bobcat |
30. | Merc. Cougar |
|-------------------|
31. | Merc. Marquis |
32. | Merc. Monarch |
33. | Merc. XR-7 |
34. | Merc. Zephyr |
35. | Olds 98 |
|-------------------|
36. | Olds Cutl Supr |
37. | Olds Cutlass |
38. | Olds Delta 88 |
39. | Olds Omega |
40. | Olds Starfire |
|-------------------|
41. | Olds Toronado |
42. | Plym. Arrow |
43. | Plym. Champ |
44. | Plym. Horizon |
45. | Plym. Sapporo |
|-------------------|
46. | Plym. Volare |
47. | Pont. Catalina |
48. | Pont. Firebird |
49. | Pont. Grand Prix |
50. | Pont. Le Mans |
|-------------------|
51. | Pont. Phoenix |
52. | Pont. Sunbird |
53. | Audi 5000 |
54. | Audi Fox |
55. | BMW 320i |
|-------------------|
56. | Datsun 200 |
57. | Datsun 210 |
58. | Datsun 510 |
59. | Datsun 810 |
60. | Fiat Strada |
|-------------------|
61. | Honda Accord |
62. | Honda Civic |
63. | Mazda GLC |
64. | Peugeot 604 |
65. | Renault Le Car |
|-------------------|
66. | Subaru |
67. | Toyota Celica |
68. | Toyota Corolla |
69. | Toyota Corona |
70. | VW Dasher |
|-------------------|
71. | VW Diesel |
72. | VW Rabbit |
73. | VW Scirocco |
74. | Volvo 260 |
+-------------------+
+----------------------------------+
| make price mpg |
|----------------------------------|
1. | AMC Concord 4,099 22 |
2. | AMC Pacer 4,749 17 |
3. | AMC Spirit 3,799 22 |
4. | Buick Century 4,816 20 |
5. | Buick Electra 7,827 15 |
|----------------------------------|
6. | Buick LeSabre 5,788 18 |
7. | Buick Opel 4,453 26 |
8. | Buick Regal 5,189 20 |
9. | Buick Riviera 10,372 16 |
10. | Buick Skylark 4,082 19 |
|----------------------------------|
11. | Cad. Deville 11,385 14 |
12. | Cad. Eldorado 14,500 14 |
13. | Cad. Seville 15,906 21 |
14. | Chev. Chevette 3,299 29 |
15. | Chev. Impala 5,705 16 |
|----------------------------------|
16. | Chev. Malibu 4,504 22 |
17. | Chev. Monte Carlo 5,104 22 |
18. | Chev. Monza 3,667 24 |
19. | Chev. Nova 3,955 19 |
20. | Dodge Colt 3,984 30 |
|----------------------------------|
21. | Dodge Diplomat 4,010 18 |
22. | Dodge Magnum 5,886 16 |
23. | Dodge St. Regis 6,342 17 |
24. | Ford Fiesta 4,389 28 |
25. | Ford Mustang 4,187 21 |
|----------------------------------|
26. | Linc. Continental 11,497 12 |
27. | Linc. Mark V 13,594 12 |
28. | Linc. Versailles 13,466 14 |
29. | Merc. Bobcat 3,829 22 |
30. | Merc. Cougar 5,379 14 |
|----------------------------------|
31. | Merc. Marquis 6,165 15 |
32. | Merc. Monarch 4,516 18 |
33. | Merc. XR-7 6,303 14 |
34. | Merc. Zephyr 3,291 20 |
35. | Olds 98 8,814 21 |
|----------------------------------|
36. | Olds Cutl Supr 5,172 19 |
37. | Olds Cutlass 4,733 19 |
38. | Olds Delta 88 4,890 18 |
39. | Olds Omega 4,181 19 |
40. | Olds Starfire 4,195 24 |
|----------------------------------|
41. | Olds Toronado 10,371 16 |
42. | Plym. Arrow 4,647 28 |
43. | Plym. Champ 4,425 34 |
44. | Plym. Horizon 4,482 25 |
45. | Plym. Sapporo 6,486 26 |
|----------------------------------|
46. | Plym. Volare 4,060 18 |
47. | Pont. Catalina 5,798 18 |
48. | Pont. Firebird 4,934 18 |
49. | Pont. Grand Prix 5,222 19 |
50. | Pont. Le Mans 4,723 19 |
|----------------------------------|
51. | Pont. Phoenix 4,424 19 |
52. | Pont. Sunbird 4,172 24 |
53. | Audi 5000 9,690 17 |
54. | Audi Fox 6,295 23 |
55. | BMW 320i 9,735 25 |
|----------------------------------|
56. | Datsun 200 6,229 23 |
57. | Datsun 210 4,589 35 |
58. | Datsun 510 5,079 24 |
59. | Datsun 810 8,129 21 |
60. | Fiat Strada 4,296 21 |
|----------------------------------|
61. | Honda Accord 5,799 25 |
62. | Honda Civic 4,499 28 |
63. | Mazda GLC 3,995 30 |
64. | Peugeot 604 12,990 14 |
65. | Renault Le Car 3,895 26 |
|----------------------------------|
66. | Subaru 3,798 35 |
67. | Toyota Celica 5,899 18 |
68. | Toyota Corolla 3,748 31 |
69. | Toyota Corona 5,719 18 |
70. | VW Dasher 7,140 23 |
|----------------------------------|
71. | VW Diesel 5,397 41 |
72. | VW Rabbit 4,697 25 |
73. | VW Scirocco 6,850 25 |
74. | Volvo 260 11,995 17 |
+----------------------------------+
There are shortcuts for creating long lists of variables without typing them all, or variable lists containing variables that match a pattern, but we’ll discuss them in Data Wrangling in Stata.
List or browse the make and weight of each car.
+----------------------------+
| make weight |
|----------------------------|
1. | AMC Concord 2,930 |
2. | AMC Pacer 3,350 |
3. | AMC Spirit 2,640 |
4. | Buick Century 3,250 |
5. | Buick Electra 4,080 |
|----------------------------|
6. | Buick LeSabre 3,670 |
7. | Buick Opel 2,230 |
8. | Buick Regal 3,280 |
9. | Buick Riviera 3,880 |
10. | Buick Skylark 3,400 |
|----------------------------|
11. | Cad. Deville 4,330 |
12. | Cad. Eldorado 3,900 |
13. | Cad. Seville 4,290 |
14. | Chev. Chevette 2,110 |
15. | Chev. Impala 3,690 |
|----------------------------|
16. | Chev. Malibu 3,180 |
17. | Chev. Monte Carlo 3,220 |
18. | Chev. Monza 2,750 |
19. | Chev. Nova 3,430 |
20. | Dodge Colt 2,120 |
|----------------------------|
21. | Dodge Diplomat 3,600 |
22. | Dodge Magnum 3,600 |
23. | Dodge St. Regis 3,740 |
24. | Ford Fiesta 1,800 |
25. | Ford Mustang 2,650 |
|----------------------------|
26. | Linc. Continental 4,840 |
27. | Linc. Mark V 4,720 |
28. | Linc. Versailles 3,830 |
29. | Merc. Bobcat 2,580 |
30. | Merc. Cougar 4,060 |
|----------------------------|
31. | Merc. Marquis 3,720 |
32. | Merc. Monarch 3,370 |
33. | Merc. XR-7 4,130 |
34. | Merc. Zephyr 2,830 |
35. | Olds 98 4,060 |
|----------------------------|
36. | Olds Cutl Supr 3,310 |
37. | Olds Cutlass 3,300 |
38. | Olds Delta 88 3,690 |
39. | Olds Omega 3,370 |
40. | Olds Starfire 2,730 |
|----------------------------|
41. | Olds Toronado 4,030 |
42. | Plym. Arrow 3,260 |
43. | Plym. Champ 1,800 |
44. | Plym. Horizon 2,200 |
45. | Plym. Sapporo 2,520 |
|----------------------------|
46. | Plym. Volare 3,330 |
47. | Pont. Catalina 3,700 |
48. | Pont. Firebird 3,470 |
49. | Pont. Grand Prix 3,210 |
50. | Pont. Le Mans 3,200 |
|----------------------------|
51. | Pont. Phoenix 3,420 |
52. | Pont. Sunbird 2,690 |
53. | Audi 5000 2,830 |
54. | Audi Fox 2,070 |
55. | BMW 320i 2,650 |
|----------------------------|
56. | Datsun 200 2,370 |
57. | Datsun 210 2,020 |
58. | Datsun 510 2,280 |
59. | Datsun 810 2,750 |
60. | Fiat Strada 2,130 |
|----------------------------|
61. | Honda Accord 2,240 |
62. | Honda Civic 1,760 |
63. | Mazda GLC 1,980 |
64. | Peugeot 604 3,420 |
65. | Renault Le Car 1,830 |
|----------------------------|
66. | Subaru 2,050 |
67. | Toyota Celica 2,410 |
68. | Toyota Corolla 2,200 |
69. | Toyota Corona 2,670 |
70. | VW Dasher 2,160 |
|----------------------------|
71. | VW Diesel 2,040 |
72. | VW Rabbit 1,930 |
73. | VW Scirocco 1,990 |
74. | Volvo 260 3,170 |
+----------------------------+
An if condition tells a command which observations it should act on. It will only act on those observations where the condition is true. This allows you to do things with subsets of the data. An if condition comes after a variable list:
+--------------------------+
| make foreign |
|--------------------------|
53. | Audi 5000 Foreign |
54. | Audi Fox Foreign |
55. | BMW 320i Foreign |
56. | Datsun 200 Foreign |
57. | Datsun 210 Foreign |
|--------------------------|
58. | Datsun 510 Foreign |
59. | Datsun 810 Foreign |
60. | Fiat Strada Foreign |
61. | Honda Accord Foreign |
62. | Honda Civic Foreign |
|--------------------------|
63. | Mazda GLC Foreign |
64. | Peugeot 604 Foreign |
65. | Renault Le Car Foreign |
66. | Subaru Foreign |
67. | Toyota Celica Foreign |
|--------------------------|
68. | Toyota Corolla Foreign |
69. | Toyota Corona Foreign |
70. | VW Dasher Foreign |
71. | VW Diesel Foreign |
72. | VW Rabbit Foreign |
|--------------------------|
73. | VW Scirocco Foreign |
74. | Volvo 260 Foreign |
+--------------------------+
Note the two equals signs! In Stata you use one equals sign when you’re setting something equal to something else (see Creating and Changing Variables) and two equals signs when you’re asking if two things are equal. Other operators you can use are:
Symbol | Meaning |
---|---|
== | Equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
!= | Not equals |
!
all by itself means “not” and reverses whatever condition follows it.
Internally, Stata equates true and false with one and zero. That means you can write:
+----------------+
| make |
|----------------|
53. | Audi 5000 |
54. | Audi Fox |
55. | BMW 320i |
56. | Datsun 200 |
57. | Datsun 210 |
|----------------|
58. | Datsun 510 |
59. | Datsun 810 |
60. | Fiat Strada |
61. | Honda Accord |
62. | Honda Civic |
|----------------|
63. | Mazda GLC |
64. | Peugeot 604 |
65. | Renault Le Car |
66. | Subaru |
67. | Toyota Celica |
|----------------|
68. | Toyota Corolla |
69. | Toyota Corona |
70. | VW Dasher |
71. | VW Diesel |
72. | VW Rabbit |
|----------------|
73. | VW Scirocco |
74. | Volvo 260 |
+----------------+
+-------------------+
| make |
|-------------------|
1. | AMC Concord |
2. | AMC Pacer |
3. | AMC Spirit |
4. | Buick Century |
5. | Buick Electra |
|-------------------|
6. | Buick LeSabre |
7. | Buick Opel |
8. | Buick Regal |
9. | Buick Riviera |
10. | Buick Skylark |
|-------------------|
11. | Cad. Deville |
12. | Cad. Eldorado |
13. | Cad. Seville |
14. | Chev. Chevette |
15. | Chev. Impala |
|-------------------|
16. | Chev. Malibu |
17. | Chev. Monte Carlo |
18. | Chev. Monza |
19. | Chev. Nova |
20. | Dodge Colt |
|-------------------|
21. | Dodge Diplomat |
22. | Dodge Magnum |
23. | Dodge St. Regis |
24. | Ford Fiesta |
25. | Ford Mustang |
|-------------------|
26. | Linc. Continental |
27. | Linc. Mark V |
28. | Linc. Versailles |
29. | Merc. Bobcat |
30. | Merc. Cougar |
|-------------------|
31. | Merc. Marquis |
32. | Merc. Monarch |
33. | Merc. XR-7 |
34. | Merc. Zephyr |
35. | Olds 98 |
|-------------------|
36. | Olds Cutl Supr |
37. | Olds Cutlass |
38. | Olds Delta 88 |
39. | Olds Omega |
40. | Olds Starfire |
|-------------------|
41. | Olds Toronado |
42. | Plym. Arrow |
43. | Plym. Champ |
44. | Plym. Horizon |
45. | Plym. Sapporo |
|-------------------|
46. | Plym. Volare |
47. | Pont. Catalina |
48. | Pont. Firebird |
49. | Pont. Grand Prix |
50. | Pont. Le Mans |
|-------------------|
51. | Pont. Phoenix |
52. | Pont. Sunbird |
+-------------------+
This makes for simple and readable code. Just be careful: anything other than zero will also be interpreted as true, including missing.
You can combine conditions with &
(logical and) or |
(logical or). The character used for logical or is called the “pipe” character and you type it by pressing Shift
-Backslash
, the key right above Enter
. Try:
+------------------------------+
| make price mpg |
|------------------------------|
7. | Buick Opel 4,453 26 |
14. | Chev. Chevette 3,299 29 |
20. | Dodge Colt 3,984 30 |
24. | Ford Fiesta 4,389 28 |
42. | Plym. Arrow 4,647 28 |
|------------------------------|
43. | Plym. Champ 4,425 34 |
57. | Datsun 210 4,589 35 |
62. | Honda Civic 4,499 28 |
63. | Mazda GLC 3,995 30 |
65. | Renault Le Car 3,895 26 |
|------------------------------|
66. | Subaru 3,798 35 |
68. | Toyota Corolla 3,748 31 |
+------------------------------+
This shows you cars that get more than 25 miles per gallon and cost less than $5000 (in 1978 dollars). In set theory terms it is the intersection of the two sets. Now try:
+------------------------------+
| make price mpg |
|------------------------------|
1. | AMC Concord 4,099 22 |
2. | AMC Pacer 4,749 17 |
3. | AMC Spirit 3,799 22 |
4. | Buick Century 4,816 20 |
7. | Buick Opel 4,453 26 |
|------------------------------|
10. | Buick Skylark 4,082 19 |
14. | Chev. Chevette 3,299 29 |
16. | Chev. Malibu 4,504 22 |
18. | Chev. Monza 3,667 24 |
19. | Chev. Nova 3,955 19 |
|------------------------------|
20. | Dodge Colt 3,984 30 |
21. | Dodge Diplomat 4,010 18 |
24. | Ford Fiesta 4,389 28 |
25. | Ford Mustang 4,187 21 |
29. | Merc. Bobcat 3,829 22 |
|------------------------------|
32. | Merc. Monarch 4,516 18 |
34. | Merc. Zephyr 3,291 20 |
37. | Olds Cutlass 4,733 19 |
38. | Olds Delta 88 4,890 18 |
39. | Olds Omega 4,181 19 |
|------------------------------|
40. | Olds Starfire 4,195 24 |
42. | Plym. Arrow 4,647 28 |
43. | Plym. Champ 4,425 34 |
44. | Plym. Horizon 4,482 25 |
45. | Plym. Sapporo 6,486 26 |
|------------------------------|
46. | Plym. Volare 4,060 18 |
48. | Pont. Firebird 4,934 18 |
50. | Pont. Le Mans 4,723 19 |
51. | Pont. Phoenix 4,424 19 |
52. | Pont. Sunbird 4,172 24 |
|------------------------------|
57. | Datsun 210 4,589 35 |
60. | Fiat Strada 4,296 21 |
62. | Honda Civic 4,499 28 |
63. | Mazda GLC 3,995 30 |
65. | Renault Le Car 3,895 26 |
|------------------------------|
66. | Subaru 3,798 35 |
68. | Toyota Corolla 3,748 31 |
71. | VW Diesel 5,397 41 |
72. | VW Rabbit 4,697 25 |
+------------------------------+
This shows you cars that get more than 25 miles per gallon or cost less than $5000. A car only needs to meet one of the two conditions to be shown (meeting both is fine too). In set theory terms it is the union of the two sets.
All the conditions to be combined must be complete. If you wanted to list the cars that have a 1 or a 2 for rep78 you should not use:
list make rep78 if rep78==1 | 2
What this does and why is left to the reader, but it’s not what you want. Instead, you should use:
+---------------------------+
| make rep78 |
|---------------------------|
12. | Cad. Eldorado 2 |
17. | Chev. Monte Carlo 2 |
18. | Chev. Monza 2 |
21. | Dodge Diplomat 2 |
22. | Dodge Magnum 2 |
|---------------------------|
23. | Dodge St. Regis 2 |
40. | Olds Starfire 1 |
46. | Plym. Volare 2 |
48. | Pont. Firebird 1 |
52. | Pont. Sunbird 2 |
+---------------------------+
If you have missing values in your data, you need to keep them in mind when writing if conditions. Internally, missing values are stored using the 27 largest possible numbers, starting with the generic missing value (.) and the extended missing values (.a, .b, etc.) after that in alphabetical order, so the following inequalities hold:
any observed value < .
< .a
< .b
< .c
… < .x
< .y
< .z
If you want a list of cars that are known to have good repair records, you won’t get it with:
+------------------------+
| make rep78 |
|------------------------|
3. | AMC Spirit . |
5. | Buick Electra 4 |
7. | Buick Opel . |
15. | Chev. Impala 4 |
20. | Dodge Colt 5 |
|------------------------|
24. | Ford Fiesta 4 |
29. | Merc. Bobcat 4 |
30. | Merc. Cougar 4 |
33. | Merc. XR-7 4 |
35. | Olds 98 4 |
|------------------------|
38. | Olds Delta 88 4 |
43. | Plym. Champ 5 |
45. | Plym. Sapporo . |
47. | Pont. Catalina 4 |
51. | Pont. Phoenix . |
|------------------------|
53. | Audi 5000 5 |
55. | BMW 320i 4 |
56. | Datsun 200 4 |
57. | Datsun 210 5 |
58. | Datsun 510 4 |
|------------------------|
59. | Datsun 810 4 |
61. | Honda Accord 5 |
62. | Honda Civic 4 |
63. | Mazda GLC 4 |
64. | Peugeot 604 . |
|------------------------|
66. | Subaru 5 |
67. | Toyota Celica 5 |
68. | Toyota Corolla 5 |
69. | Toyota Corona 5 |
70. | VW Dasher 4 |
|------------------------|
71. | VW Diesel 5 |
72. | VW Rabbit 4 |
73. | VW Scirocco 4 |
74. | Volvo 260 5 |
+------------------------+
An easy shortcut is to think of missing values as (positive) infinity, and since infinity is greater than 3 cars with a missing value for rep78
are included in the list. So add a second condition to exclude them:
+------------------------+
| make rep78 |
|------------------------|
5. | Buick Electra 4 |
15. | Chev. Impala 4 |
20. | Dodge Colt 5 |
24. | Ford Fiesta 4 |
29. | Merc. Bobcat 4 |
|------------------------|
30. | Merc. Cougar 4 |
33. | Merc. XR-7 4 |
35. | Olds 98 4 |
38. | Olds Delta 88 4 |
43. | Plym. Champ 5 |
|------------------------|
47. | Pont. Catalina 4 |
53. | Audi 5000 5 |
55. | BMW 320i 4 |
56. | Datsun 200 4 |
57. | Datsun 210 5 |
|------------------------|
58. | Datsun 510 4 |
59. | Datsun 810 4 |
61. | Honda Accord 5 |
62. | Honda Civic 4 |
63. | Mazda GLC 4 |
|------------------------|
66. | Subaru 5 |
67. | Toyota Celica 5 |
68. | Toyota Corolla 5 |
69. | Toyota Corona 5 |
70. | VW Dasher 4 |
|------------------------|
71. | VW Diesel 5 |
72. | VW Rabbit 4 |
73. | VW Scirocco 4 |
74. | Volvo 260 5 |
+------------------------+
Why <.
rather than !=.
? In this data set it makes no difference. But if the data set included extended missing values (.a
, .b
, etc.), the condition !=.
would not exclude them. The condition <.
excludes them because extended missing values are greater than the generic missing value. Thus using <.
ensures you’re excluding all missing values.
Browse or list domestic cars that get more than 25 miles per gallon and are known to have good repair records (rep78 greater than 3). Include the variables used in the conditions so you can spot-check your results. Explain why you handled missing values the way you did.
+--------------------------------------+
| make foreign mpg rep78 |
|--------------------------------------|
20. | Dodge Colt Domestic 30 5 |
24. | Ford Fiesta Domestic 28 4 |
43. | Plym. Champ Domestic 34 5 |
+--------------------------------------+
Don’t forget the !foreign
condition implied by listing domestic cars. You could use foreign==0
instead.
Cars with a missing values for rep78
are excluded (& rep78<.
) because they are not known to have a good repair record.
Browse or list foreign cars that cost less than $5,000 and are not known to have poor repair records (rep78 less than or equal to 3). Explain why you handled missing values the way you did.
+------------------------------------------+
| make foreign price rep78 |
|------------------------------------------|
57. | Datsun 210 Foreign 4,589 5 |
60. | Fiat Strada Foreign 4,296 3 |
62. | Honda Civic Foreign 4,499 4 |
63. | Mazda GLC Foreign 3,995 4 |
65. | Renault Le Car Foreign 3,895 3 |
|------------------------------------------|
66. | Subaru Foreign 3,798 5 |
68. | Toyota Corolla Foreign 3,748 5 |
72. | VW Rabbit Foreign 4,697 4 |
+------------------------------------------+
You could translate “not known to have poor repair records” more directly with & !(rep78<3)
but the parentheses are essential due to the order of operations for Boolean operators like !
and <
. The expression !rep78<3
without parentheses is equivalent to (!rep78)<3
, and it’s left to the reader to figure out what that does and why.
This time, cars with a missing value for rep78
are not excluded because they are not known to have a poor repair record.
Options change how a command works. They go after any variable list or if condition, following a comma. The comma means “everything after this is options” so you only type one comma no matter how many options you’re using.
Consider:
+------------------------------+
| make foreign |
|------------------------------|
1. | AMC Concord Domestic |
2. | AMC Pacer Domestic |
3. | AMC Spirit Domestic |
4. | Buick Century Domestic |
5. | Buick Electra Domestic |
|------------------------------|
6. | Buick LeSabre Domestic |
7. | Buick Opel Domestic |
8. | Buick Regal Domestic |
9. | Buick Riviera Domestic |
10. | Buick Skylark Domestic |
|------------------------------|
11. | Cad. Deville Domestic |
12. | Cad. Eldorado Domestic |
13. | Cad. Seville Domestic |
14. | Chev. Chevette Domestic |
15. | Chev. Impala Domestic |
|------------------------------|
16. | Chev. Malibu Domestic |
17. | Chev. Monte Carlo Domestic |
18. | Chev. Monza Domestic |
19. | Chev. Nova Domestic |
20. | Dodge Colt Domestic |
|------------------------------|
21. | Dodge Diplomat Domestic |
22. | Dodge Magnum Domestic |
23. | Dodge St. Regis Domestic |
24. | Ford Fiesta Domestic |
25. | Ford Mustang Domestic |
|------------------------------|
26. | Linc. Continental Domestic |
27. | Linc. Mark V Domestic |
28. | Linc. Versailles Domestic |
29. | Merc. Bobcat Domestic |
30. | Merc. Cougar Domestic |
|------------------------------|
31. | Merc. Marquis Domestic |
32. | Merc. Monarch Domestic |
33. | Merc. XR-7 Domestic |
34. | Merc. Zephyr Domestic |
35. | Olds 98 Domestic |
|------------------------------|
36. | Olds Cutl Supr Domestic |
37. | Olds Cutlass Domestic |
38. | Olds Delta 88 Domestic |
39. | Olds Omega Domestic |
40. | Olds Starfire Domestic |
|------------------------------|
41. | Olds Toronado Domestic |
42. | Plym. Arrow Domestic |
43. | Plym. Champ Domestic |
44. | Plym. Horizon Domestic |
45. | Plym. Sapporo Domestic |
|------------------------------|
46. | Plym. Volare Domestic |
47. | Pont. Catalina Domestic |
48. | Pont. Firebird Domestic |
49. | Pont. Grand Prix Domestic |
50. | Pont. Le Mans Domestic |
|------------------------------|
51. | Pont. Phoenix Domestic |
52. | Pont. Sunbird Domestic |
53. | Audi 5000 Foreign |
54. | Audi Fox Foreign |
55. | BMW 320i Foreign |
|------------------------------|
56. | Datsun 200 Foreign |
57. | Datsun 210 Foreign |
58. | Datsun 510 Foreign |
59. | Datsun 810 Foreign |
60. | Fiat Strada Foreign |
|------------------------------|
61. | Honda Accord Foreign |
62. | Honda Civic Foreign |
63. | Mazda GLC Foreign |
64. | Peugeot 604 Foreign |
65. | Renault Le Car Foreign |
|------------------------------|
66. | Subaru Foreign |
67. | Toyota Celica Foreign |
68. | Toyota Corolla Foreign |
69. | Toyota Corona Foreign |
70. | VW Dasher Foreign |
|------------------------------|
71. | VW Diesel Foreign |
72. | VW Rabbit Foreign |
73. | VW Scirocco Foreign |
74. | Volvo 260 Foreign |
+------------------------------+
We know that value labels have been applied to the foreign
variable, so the words “Domestic” and “Foreign” are not the actual values. We can see the values instead of the labels by adding the nolabel
option:
+-----------------------------+
| make foreign |
|-----------------------------|
1. | AMC Concord 0 |
2. | AMC Pacer 0 |
3. | AMC Spirit 0 |
4. | Buick Century 0 |
5. | Buick Electra 0 |
|-----------------------------|
6. | Buick LeSabre 0 |
7. | Buick Opel 0 |
8. | Buick Regal 0 |
9. | Buick Riviera 0 |
10. | Buick Skylark 0 |
|-----------------------------|
11. | Cad. Deville 0 |
12. | Cad. Eldorado 0 |
13. | Cad. Seville 0 |
14. | Chev. Chevette 0 |
15. | Chev. Impala 0 |
|-----------------------------|
16. | Chev. Malibu 0 |
17. | Chev. Monte Carlo 0 |
18. | Chev. Monza 0 |
19. | Chev. Nova 0 |
20. | Dodge Colt 0 |
|-----------------------------|
21. | Dodge Diplomat 0 |
22. | Dodge Magnum 0 |
23. | Dodge St. Regis 0 |
24. | Ford Fiesta 0 |
25. | Ford Mustang 0 |
|-----------------------------|
26. | Linc. Continental 0 |
27. | Linc. Mark V 0 |
28. | Linc. Versailles 0 |
29. | Merc. Bobcat 0 |
30. | Merc. Cougar 0 |
|-----------------------------|
31. | Merc. Marquis 0 |
32. | Merc. Monarch 0 |
33. | Merc. XR-7 0 |
34. | Merc. Zephyr 0 |
35. | Olds 98 0 |
|-----------------------------|
36. | Olds Cutl Supr 0 |
37. | Olds Cutlass 0 |
38. | Olds Delta 88 0 |
39. | Olds Omega 0 |
40. | Olds Starfire 0 |
|-----------------------------|
41. | Olds Toronado 0 |
42. | Plym. Arrow 0 |
43. | Plym. Champ 0 |
44. | Plym. Horizon 0 |
45. | Plym. Sapporo 0 |
|-----------------------------|
46. | Plym. Volare 0 |
47. | Pont. Catalina 0 |
48. | Pont. Firebird 0 |
49. | Pont. Grand Prix 0 |
50. | Pont. Le Mans 0 |
|-----------------------------|
51. | Pont. Phoenix 0 |
52. | Pont. Sunbird 0 |
53. | Audi 5000 1 |
54. | Audi Fox 1 |
55. | BMW 320i 1 |
|-----------------------------|
56. | Datsun 200 1 |
57. | Datsun 210 1 |
58. | Datsun 510 1 |
59. | Datsun 810 1 |
60. | Fiat Strada 1 |
|-----------------------------|
61. | Honda Accord 1 |
62. | Honda Civic 1 |
63. | Mazda GLC 1 |
64. | Peugeot 604 1 |
65. | Renault Le Car 1 |
|-----------------------------|
66. | Subaru 1 |
67. | Toyota Celica 1 |
68. | Toyota Corolla 1 |
69. | Toyota Corona 1 |
70. | VW Dasher 1 |
|-----------------------------|
71. | VW Diesel 1 |
72. | VW Rabbit 1 |
73. | VW Scirocco 1 |
74. | Volvo 260 1 |
+-----------------------------+
Options must always be one word. Here the words “no” and “label” are combined because otherwise Stata would think they were two different options.
Note that browse
has very few options (nolabel
is one of them). If you’ve been replacing list
with browse
in your code, stick with list
for the rest of the chapter.
Many options require additional information, such as a number or a variable they apply to. This additional information goes in parentheses directly after the option name. The string()
option tells the list command to truncate string variables after a given number of characters, with the number going in the parentheses:
+---------+
| make |
|---------|
1. | AMC C.. |
2. | AMC P.. |
3. | AMC S.. |
4. | Buick.. |
5. | Buick.. |
|---------|
6. | Buick.. |
7. | Buick.. |
8. | Buick.. |
9. | Buick.. |
10. | Buick.. |
|---------|
11. | Cad. .. |
12. | Cad. .. |
13. | Cad. .. |
14. | Chev... |
15. | Chev... |
|---------|
16. | Chev... |
17. | Chev... |
18. | Chev... |
19. | Chev... |
20. | Dodge.. |
|---------|
21. | Dodge.. |
22. | Dodge.. |
23. | Dodge.. |
24. | Ford .. |
25. | Ford .. |
|---------|
26. | Linc... |
27. | Linc... |
28. | Linc... |
29. | Merc... |
30. | Merc... |
|---------|
31. | Merc... |
32. | Merc... |
33. | Merc... |
34. | Merc... |
35. | Olds 98 |
|---------|
36. | Olds .. |
37. | Olds .. |
38. | Olds .. |
39. | Olds .. |
40. | Olds .. |
|---------|
41. | Olds .. |
42. | Plym... |
43. | Plym... |
44. | Plym... |
45. | Plym... |
|---------|
46. | Plym... |
47. | Pont... |
48. | Pont... |
49. | Pont... |
50. | Pont... |
|---------|
51. | Pont... |
52. | Pont... |
53. | Audi .. |
54. | Audi .. |
55. | BMW 3.. |
|---------|
56. | Datsu.. |
57. | Datsu.. |
58. | Datsu.. |
59. | Datsu.. |
60. | Fiat .. |
|---------|
61. | Honda.. |
62. | Honda.. |
63. | Mazda.. |
64. | Peuge.. |
65. | Renau.. |
|---------|
66. | Subaru |
67. | Toyot.. |
68. | Toyot.. |
69. | Toyot.. |
70. | VW Da.. |
|---------|
71. | VW Di.. |
72. | VW Ra.. |
73. | VW Sc.. |
74. | Volvo.. |
+---------+
You might use the string()
option to save space, or if the first part of the string contains all the information you really need. But it’s mostly here as an example of the “additional information goes in parentheses” syntax that you’ll use regularly.
Stata reuses option names wherever it makes sense. Thus many commands take a nolabel
option that prompts them to ignore value labels. Other common options include gen()
to generate a new variable (with the name of the new variable going in parentheses), by()
to act on groups, and vce()
to tell regression commands how to estimate the variance-covariance matrix.
By groups allows you to execute a command separately for subgroups within your data. Try:
-------------------------------------------------------------------------------
-> foreign = Domestic
+-------------------+
| make |
|-------------------|
1. | AMC Concord |
2. | AMC Pacer |
3. | AMC Spirit |
4. | Buick Century |
5. | Buick Electra |
|-------------------|
6. | Buick LeSabre |
7. | Buick Opel |
8. | Buick Regal |
9. | Buick Riviera |
10. | Buick Skylark |
|-------------------|
11. | Cad. Deville |
12. | Cad. Eldorado |
13. | Cad. Seville |
14. | Chev. Chevette |
15. | Chev. Impala |
|-------------------|
16. | Chev. Malibu |
17. | Chev. Monte Carlo |
18. | Chev. Monza |
19. | Chev. Nova |
20. | Dodge Colt |
|-------------------|
21. | Dodge Diplomat |
22. | Dodge Magnum |
23. | Dodge St. Regis |
24. | Ford Fiesta |
25. | Ford Mustang |
|-------------------|
26. | Linc. Continental |
27. | Linc. Mark V |
28. | Linc. Versailles |
29. | Merc. Bobcat |
30. | Merc. Cougar |
|-------------------|
31. | Merc. Marquis |
32. | Merc. Monarch |
33. | Merc. XR-7 |
34. | Merc. Zephyr |
35. | Olds 98 |
|-------------------|
36. | Olds Cutl Supr |
37. | Olds Cutlass |
38. | Olds Delta 88 |
39. | Olds Omega |
40. | Olds Starfire |
|-------------------|
41. | Olds Toronado |
42. | Plym. Arrow |
43. | Plym. Champ |
44. | Plym. Horizon |
45. | Plym. Sapporo |
|-------------------|
46. | Plym. Volare |
47. | Pont. Catalina |
48. | Pont. Firebird |
49. | Pont. Grand Prix |
50. | Pont. Le Mans |
|-------------------|
51. | Pont. Phoenix |
52. | Pont. Sunbird |
+-------------------+
-------------------------------------------------------------------------------
-> foreign = Foreign
+----------------+
| make |
|----------------|
1. | Audi 5000 |
2. | Audi Fox |
3. | BMW 320i |
4. | Datsun 200 |
5. | Datsun 210 |
|----------------|
6. | Datsun 510 |
7. | Datsun 810 |
8. | Fiat Strada |
9. | Honda Accord |
10. | Honda Civic |
|----------------|
11. | Mazda GLC |
12. | Peugeot 604 |
13. | Renault Le Car |
14. | Subaru |
15. | Toyota Celica |
|----------------|
16. | Toyota Corolla |
17. | Toyota Corona |
18. | VW Dasher |
19. | VW Diesel |
20. | VW Rabbit |
|----------------|
21. | VW Scirocco |
22. | Volvo 260 |
+----------------+
The by foreign:
prefix tells Stata to:
foreign
(in this case, 0 and 1 or “Domestic” and “Foreign”)list make
) separately for each groupYou’ll see how powerful by
is later.
In order for by
to work, the data must be sorted by the same variable. You can do that with the sort command:
-------------------------------------------------------------------------------
-> rep78 = 1
+----------------+
| make |
|----------------|
1. | Pont. Firebird |
2. | Olds Starfire |
+----------------+
-------------------------------------------------------------------------------
-> rep78 = 2
+-------------------+
| make |
|-------------------|
1. | Dodge St. Regis |
2. | Chev. Monza |
3. | Dodge Magnum |
4. | Plym. Volare |
5. | Dodge Diplomat |
|-------------------|
6. | Chev. Monte Carlo |
7. | Pont. Sunbird |
8. | Cad. Eldorado |
+-------------------+
-------------------------------------------------------------------------------
-> rep78 = 3
+-------------------+
| make |
|-------------------|
1. | AMC Concord |
2. | Chev. Nova |
3. | Olds Omega |
4. | AMC Pacer |
5. | Cad. Deville |
|-------------------|
6. | Olds Cutl Supr |
7. | Pont. Le Mans |
8. | Buick Skylark |
9. | Linc. Versailles |
10. | Plym. Arrow |
|-------------------|
11. | Merc. Monarch |
12. | Olds Cutlass |
13. | Merc. Marquis |
14. | Cad. Seville |
15. | Plym. Horizon |
|-------------------|
16. | Fiat Strada |
17. | Renault Le Car |
18. | Chev. Chevette |
19. | Chev. Malibu |
20. | Linc. Mark V |
|-------------------|
21. | Buick LeSabre |
22. | Pont. Grand Prix |
23. | Audi Fox |
24. | Olds Toronado |
25. | Buick Century |
|-------------------|
26. | Merc. Zephyr |
27. | Linc. Continental |
28. | Ford Mustang |
29. | Buick Regal |
30. | Buick Riviera |
+-------------------+
-------------------------------------------------------------------------------
-> rep78 = 4
+----------------+
| make |
|----------------|
1. | Olds 98 |
2. | VW Rabbit |
3. | Honda Civic |
4. | Buick Electra |
5. | Datsun 510 |
|----------------|
6. | Olds Delta 88 |
7. | Datsun 200 |
8. | Merc. Cougar |
9. | Merc. XR-7 |
10. | VW Scirocco |
|----------------|
11. | VW Dasher |
12. | Pont. Catalina |
13. | Chev. Impala |
14. | Ford Fiesta |
15. | Datsun 810 |
|----------------|
16. | Merc. Bobcat |
17. | Mazda GLC |
18. | BMW 320i |
+----------------+
-------------------------------------------------------------------------------
-> rep78 = 5
+----------------+
| make |
|----------------|
1. | Subaru |
2. | Toyota Celica |
3. | Honda Accord |
4. | Dodge Colt |
5. | Toyota Corolla |
|----------------|
6. | Plym. Champ |
7. | VW Diesel |
8. | Audi 5000 |
9. | Toyota Corona |
10. | Volvo 260 |
|----------------|
11. | Datsun 210 |
+----------------+
-------------------------------------------------------------------------------
-> rep78 = .
+---------------+
| make |
|---------------|
1. | Pont. Phoenix |
2. | AMC Spirit |
3. | Plym. Sapporo |
4. | Buick Opel |
5. | Peugeot 604 |
+---------------+
Alternatively, you can have by
do this for you either by adding the sort
option to the by
prefix or just saying bysort
:
by rep78, sort: list make
bysort rep78: list make
Of course you don’t need to have Stata sort the data every time you use by
. Once the data are sorted, you can just say:
by rep78: list make
You can have more than one variable in the by
list. In that case, Stata will split the data set up into one group for each unique combination of the variables. The data set must still be sorted in the same order.