3  Elements of Stata Syntax

If you haven’t already, load the automobile data set that comes with Stata by running:

sysuse auto
(1978 automobile data)

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:

3.1 Stata Commands

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.

The browse command opens the Data Editor in browse mode so you can see your data. That’s a very useful thing. It’s also great for demonstrating Stata’s syntax, because you can see the effects of the syntax elements right away. Unfortunately a web book can’t open the Data Editor, so I’ll use list instead. It lists the data in the Results window.

That frequently makes for long output. I’ve put list commands that create a lot of output in collapsible boxes so you can see how they work, then collapse them to read on rather than having to do a lot of scrolling.

Feel free to use browse instead of list when you run the example code yourself.

Collapse this box using the arrow to the right after looking over the output
list

     +----------------------------------------------------------------------+
  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    |
     +----------------------------------------------------------------------+

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.

3.2 Variable Lists

Listing one or more variables after a command tells the command it should only act on the variables listed:

Output
list make

     +-------------------+
     | 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         |
     +-------------------+
Output
list make price mpg

     +----------------------------------+
     | 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.

Exercise

List or browse the make and displacement of each car. (Displacement is a measure of the size of the car’s engine.)

list make displacement

     +------------------------------+
     | make                displa~t |
     |------------------------------|
  1. | AMC Concord              121 |
  2. | AMC Pacer                258 |
  3. | AMC Spirit               121 |
  4. | Buick Century            196 |
  5. | Buick Electra            350 |
     |------------------------------|
  6. | Buick LeSabre            231 |
  7. | Buick Opel               304 |
  8. | Buick Regal              196 |
  9. | Buick Riviera            231 |
 10. | Buick Skylark            231 |
     |------------------------------|
 11. | Cad. Deville             425 |
 12. | Cad. Eldorado            350 |
 13. | Cad. Seville             350 |
 14. | Chev. Chevette           231 |
 15. | Chev. Impala             250 |
     |------------------------------|
 16. | Chev. Malibu             200 |
 17. | Chev. Monte Carlo        200 |
 18. | Chev. Monza              151 |
 19. | Chev. Nova               250 |
 20. | Dodge Colt                98 |
     |------------------------------|
 21. | Dodge Diplomat           318 |
 22. | Dodge Magnum             318 |
 23. | Dodge St. Regis          225 |
 24. | Ford Fiesta               98 |
 25. | Ford Mustang             140 |
     |------------------------------|
 26. | Linc. Continental        400 |
 27. | Linc. Mark V             400 |
 28. | Linc. Versailles         302 |
 29. | Merc. Bobcat             140 |
 30. | Merc. Cougar             302 |
     |------------------------------|
 31. | Merc. Marquis            302 |
 32. | Merc. Monarch            250 |
 33. | Merc. XR-7               302 |
 34. | Merc. Zephyr             140 |
 35. | Olds 98                  350 |
     |------------------------------|
 36. | Olds Cutl Supr           231 |
 37. | Olds Cutlass             231 |
 38. | Olds Delta 88            231 |
 39. | Olds Omega               231 |
 40. | Olds Starfire            151 |
     |------------------------------|
 41. | Olds Toronado            350 |
 42. | Plym. Arrow              156 |
 43. | Plym. Champ               86 |
 44. | Plym. Horizon            105 |
 45. | Plym. Sapporo            119 |
     |------------------------------|
 46. | Plym. Volare             225 |
 47. | Pont. Catalina           231 |
 48. | Pont. Firebird           231 |
 49. | Pont. Grand Prix         231 |
 50. | Pont. Le Mans            231 |
     |------------------------------|
 51. | Pont. Phoenix            231 |
 52. | Pont. Sunbird            151 |
 53. | Audi 5000                131 |
 54. | Audi Fox                  97 |
 55. | BMW 320i                 121 |
     |------------------------------|
 56. | Datsun 200               119 |
 57. | Datsun 210                85 |
 58. | Datsun 510               119 |
 59. | Datsun 810               146 |
 60. | Fiat Strada              105 |
     |------------------------------|
 61. | Honda Accord             107 |
 62. | Honda Civic               91 |
 63. | Mazda GLC                 86 |
 64. | Peugeot 604              163 |
 65. | Renault Le Car            79 |
     |------------------------------|
 66. | Subaru                    97 |
 67. | Toyota Celica            134 |
 68. | Toyota Corolla            97 |
 69. | Toyota Corona            134 |
 70. | VW Dasher                 97 |
     |------------------------------|
 71. | VW Diesel                 90 |
 72. | VW Rabbit                 89 |
 73. | VW Scirocco               97 |
 74. | Volvo 260                163 |
     +------------------------------+

Note how with list, the variable name displacement is abbreviated because it is longer than eight characters. You’ll learn how to change that later.

3.3 If Conditions

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:

Output
list make foreign if foreign==1

     +--------------------------+
     | 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:

Output
list make if foreign

     +----------------+
     | 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      |
     +----------------+
Output
list make if !foreign

     +-------------------+
     | 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.

3.3.1 Combining Conditions

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:

Output
list make price mpg if mpg>25 & price<5000

     +------------------------------+
     | 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:

Output
list make price mpg if mpg>25 | price<5000

     +------------------------------+
     | 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:

list make rep78 if rep78==1 | rep78==2

     +---------------------------+
     | 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 |
     +---------------------------+

Also note that no matter how many conditions you have, the word if appears only once.

3.3.2 Missing Values

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:

Output
list make rep78 if rep78>3

     +------------------------+
     | 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:

Output
list make rep78 if rep78>3 & rep78<.

     +------------------------+
     | 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.

Exercise

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.

list make foreign mpg rep78 if !foreign & mpg>25 & rep78>3 & rep78<.

     +--------------------------------------+
     | 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.

Exercise

Browse or list foreign cars that cost less than $5,000 and are not known to have poor repair records (rep78 less than 3). Explain why you handled missing values the way you did.

list make foreign price rep78 if foreign & price<5000 & rep78>=3

     +------------------------------------------+
     | 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.

3.4 Options

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:

Output
list make foreign

     +------------------------------+
     | 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:

Output
list make foreign, nolabel

     +-----------------------------+
     | 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, switch to 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. Do not put a space between the option name and the parentheses.

By default, list will abbreviate any variable names that are longer than eight characters. You can change that using the abbreviate option, followed by parentheses containing the number of characters you want to allow before a variable name is abbreviated. A big enough number will prevent any abbreviation:

Output
list make displacement, abbreviate(30)

     +----------------------------------+
     | make                displacement |
     |----------------------------------|
  1. | AMC Concord                  121 |
  2. | AMC Pacer                    258 |
  3. | AMC Spirit                   121 |
  4. | Buick Century                196 |
  5. | Buick Electra                350 |
     |----------------------------------|
  6. | Buick LeSabre                231 |
  7. | Buick Opel                   304 |
  8. | Buick Regal                  196 |
  9. | Buick Riviera                231 |
 10. | Buick Skylark                231 |
     |----------------------------------|
 11. | Cad. Deville                 425 |
 12. | Cad. Eldorado                350 |
 13. | Cad. Seville                 350 |
 14. | Chev. Chevette               231 |
 15. | Chev. Impala                 250 |
     |----------------------------------|
 16. | Chev. Malibu                 200 |
 17. | Chev. Monte Carlo            200 |
 18. | Chev. Monza                  151 |
 19. | Chev. Nova                   250 |
 20. | Dodge Colt                    98 |
     |----------------------------------|
 21. | Dodge Diplomat               318 |
 22. | Dodge Magnum                 318 |
 23. | Dodge St. Regis              225 |
 24. | Ford Fiesta                   98 |
 25. | Ford Mustang                 140 |
     |----------------------------------|
 26. | Linc. Continental            400 |
 27. | Linc. Mark V                 400 |
 28. | Linc. Versailles             302 |
 29. | Merc. Bobcat                 140 |
 30. | Merc. Cougar                 302 |
     |----------------------------------|
 31. | Merc. Marquis                302 |
 32. | Merc. Monarch                250 |
 33. | Merc. XR-7                   302 |
 34. | Merc. Zephyr                 140 |
 35. | Olds 98                      350 |
     |----------------------------------|
 36. | Olds Cutl Supr               231 |
 37. | Olds Cutlass                 231 |
 38. | Olds Delta 88                231 |
 39. | Olds Omega                   231 |
 40. | Olds Starfire                151 |
     |----------------------------------|
 41. | Olds Toronado                350 |
 42. | Plym. Arrow                  156 |
 43. | Plym. Champ                   86 |
 44. | Plym. Horizon                105 |
 45. | Plym. Sapporo                119 |
     |----------------------------------|
 46. | Plym. Volare                 225 |
 47. | Pont. Catalina               231 |
 48. | Pont. Firebird               231 |
 49. | Pont. Grand Prix             231 |
 50. | Pont. Le Mans                231 |
     |----------------------------------|
 51. | Pont. Phoenix                231 |
 52. | Pont. Sunbird                151 |
 53. | Audi 5000                    131 |
 54. | Audi Fox                      97 |
 55. | BMW 320i                     121 |
     |----------------------------------|
 56. | Datsun 200                   119 |
 57. | Datsun 210                    85 |
 58. | Datsun 510                   119 |
 59. | Datsun 810                   146 |
 60. | Fiat Strada                  105 |
     |----------------------------------|
 61. | Honda Accord                 107 |
 62. | Honda Civic                   91 |
 63. | Mazda GLC                     86 |
 64. | Peugeot 604                  163 |
 65. | Renault Le Car                79 |
     |----------------------------------|
 66. | Subaru                        97 |
 67. | Toyota Celica                134 |
 68. | Toyota Corolla                97 |
 69. | Toyota Corona                134 |
 70. | VW Dasher                     97 |
     |----------------------------------|
 71. | VW Diesel                     90 |
 72. | VW Rabbit                     89 |
 73. | VW Scirocco                   97 |
 74. | Volvo 260                    163 |
     +----------------------------------+

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.

3.5 By Groups

By groups allows you to execute a command separately for subgroups within your data. Try:

Output
by foreign: list make

-------------------------------------------------------------------------------
-> 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:

  1. Identify the unique values of foreign (in this case, 0 and 1 or “Domestic” and “Foreign”)
  2. Temporarily split the data set into groups based on their value of foreign
  3. Run the subsequent command (list make) separately for each group

You’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:

Output
sort rep78
by rep78: list make

-------------------------------------------------------------------------------
-> rep78 = 1

     +----------------+
     | make           |
     |----------------|
  1. | Pont. Firebird |
  2. | Olds Starfire  |
     +----------------+

-------------------------------------------------------------------------------
-> rep78 = 2

     +-------------------+
     | make              |
     |-------------------|
  1. | Plym. Volare      |
  2. | Pont. Sunbird     |
  3. | Dodge Diplomat    |
  4. | Cad. Eldorado     |
  5. | Dodge St. Regis   |
     |-------------------|
  6. | Chev. Monza       |
  7. | Dodge Magnum      |
  8. | Chev. Monte Carlo |
     +-------------------+

-------------------------------------------------------------------------------
-> rep78 = 3

     +-------------------+
     | make              |
     |-------------------|
  1. | Pont. Grand Prix  |
  2. | Olds Toronado     |
  3. | Olds Cutl Supr    |
  4. | Ford Mustang      |
  5. | Buick Regal       |
     |-------------------|
  6. | AMC Pacer         |
  7. | AMC Concord       |
  8. | Buick Century     |
  9. | Olds Cutlass      |
 10. | Linc. Continental |
     |-------------------|
 11. | Buick LeSabre     |
 12. | Buick Riviera     |
 13. | Pont. Le Mans     |
 14. | Linc. Versailles  |
 15. | Merc. Zephyr      |
     |-------------------|
 16. | Merc. Monarch     |
 17. | Cad. Deville      |
 18. | Merc. Marquis     |
 19. | Renault Le Car    |
 20. | Linc. Mark V      |
     |-------------------|
 21. | Chev. Malibu      |
 22. | Cad. Seville      |
 23. | Plym. Arrow       |
 24. | Fiat Strada       |
 25. | Buick Skylark     |
     |-------------------|
 26. | Chev. Chevette    |
 27. | Chev. Nova        |
 28. | Audi Fox          |
 29. | Olds Omega        |
 30. | Plym. Horizon     |
     +-------------------+

-------------------------------------------------------------------------------
-> rep78 = 4

     +----------------+
     | make           |
     |----------------|
  1. | Olds Delta 88  |
  2. | Datsun 200     |
  3. | VW Dasher      |
  4. | Honda Civic    |
  5. | Ford Fiesta    |
     |----------------|
  6. | Datsun 810     |
  7. | Buick Electra  |
  8. | Merc. Cougar   |
  9. | Merc. XR-7     |
 10. | VW Scirocco    |
     |----------------|
 11. | Olds 98        |
 12. | Pont. Catalina |
 13. | BMW 320i       |
 14. | VW Rabbit      |
 15. | Chev. Impala   |
     |----------------|
 16. | Mazda GLC      |
 17. | Datsun 510     |
 18. | Merc. Bobcat   |
     +----------------+

-------------------------------------------------------------------------------
-> rep78 = 5

     +----------------+
     | make           |
     |----------------|
  1. | Audi 5000      |
  2. | Subaru         |
  3. | Volvo 260      |
  4. | Dodge Colt     |
  5. | Toyota Corona  |
     |----------------|
  6. | Honda Accord   |
  7. | VW Diesel      |
  8. | Datsun 210     |
  9. | Plym. Champ    |
 10. | Toyota Corolla |
     |----------------|
 11. | Toyota Celica  |
     +----------------+

-------------------------------------------------------------------------------
-> rep78 = .

     +---------------+
     | make          |
     |---------------|
  1. | Buick Opel    |
  2. | Pont. Phoenix |
  3. | Plym. Sapporo |
  4. | AMC Spirit    |
  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.