Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Select a range of observations to perform actions.

    Hello. In the dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str18 make int(price mpg rep78) float headroom int(trunk weight length turn displacement) float gear_ratio byte foreign
    "AMC Concord"     4099 22 3 2.5 11 2930 186 40 121 3.58 0
    "AMC Pacer"       4749 17 3   3 11 3350 173 40 258 2.53 0
    "AMC Spirit"      3799 22 .   3 12 2640 168 35 121 3.08 0
    "Buick Century"   4816 20 3 4.5 16 3250 196 40 196 2.93 0
    "Buick Electra"   7827 15 4   4 20 4080 222 43 350 2.41 0
    "Buick LeSabre"   5788 18 3   4 21 3670 218 43 231 2.73 0
    "Buick Opel"      4453 26 .   3 10 2230 170 34 304 2.87 0
    "Buick Regal"     5189 20 3   2 16 3280 200 42 196 2.93 0
    "Buick Riviera"  10372 16 3 3.5 17 3880 207 43 231 2.93 0
    "Buick Skylark"   4082 19 3 3.5 13 3400 200 42 231 3.08 0
    "Cad. Deville"   11385 14 3   4 20 4330 221 44 425 2.28 0
    "Cad. Eldorado"  14500 14 2 3.5 16 3900 204 43 350 2.19 0
    "Cad. Seville"   15906 21 3   3 13 4290 204 45 350 2.24 0
    "Chev. Chevette"  3299 29 3 2.5  9 2110 163 34 231 2.93 0
    "Chev. Impala"    5705 16 4   4 20 3690 212 43 250 2.56 0
    end
    label values foreign origin
    label def origin 0 "Domestic", modify
    Suppose I want to generate an empty variable called "whatever". I want to select a specific range of observation between, say, "AMC Concord" and "Cad. Deville", the two included, to perform actions for them in the variable "watever". There is no specific pattern to know besides their position. How can I do it?

    Thansk for the help.

  • #2
    Unless you have a sorting variable, observation numbers are arbitrary as they change when the data is sorted.

    Code:
    gen wanted= sum(sum(make== "Cad. Deville"))<=1
    Res.:

    Code:
    . l, sep(0)
    
         +----------------------------------------------------------------------------------------------------------------------------+
         |           make   price   mpg   rep78   headroom   trunk   weight   length   turn   displa~t   gear_r~o    foreign   wanted |
         |----------------------------------------------------------------------------------------------------------------------------|
      1. |    AMC Concord    4099    22       3        2.5      11     2930      186     40        121       3.58   Domestic        1 |
      2. |      AMC Pacer    4749    17       3          3      11     3350      173     40        258       2.53   Domestic        1 |
      3. |     AMC Spirit    3799    22       .          3      12     2640      168     35        121       3.08   Domestic        1 |
      4. |  Buick Century    4816    20       3        4.5      16     3250      196     40        196       2.93   Domestic        1 |
      5. |  Buick Electra    7827    15       4          4      20     4080      222     43        350       2.41   Domestic        1 |
      6. |  Buick LeSabre    5788    18       3          4      21     3670      218     43        231       2.73   Domestic        1 |
      7. |     Buick Opel    4453    26       .          3      10     2230      170     34        304       2.87   Domestic        1 |
      8. |    Buick Regal    5189    20       3          2      16     3280      200     42        196       2.93   Domestic        1 |
      9. |  Buick Riviera   10372    16       3        3.5      17     3880      207     43        231       2.93   Domestic        1 |
     10. |  Buick Skylark    4082    19       3        3.5      13     3400      200     42        231       3.08   Domestic        1 |
     11. |   Cad. Deville   11385    14       3          4      20     4330      221     44        425       2.28   Domestic        1 |
     12. |  Cad. Eldorado   14500    14       2        3.5      16     3900      204     43        350       2.19   Domestic        0 |
     13. |   Cad. Seville   15906    21       3          3      13     4290      204     45        350       2.24   Domestic        0 |
     14. | Chev. Chevette    3299    29       3        2.5       9     2110      163     34        231       2.93   Domestic        0 |
     15. |   Chev. Impala    5705    16       4          4      20     3690      212     43        250       2.56   Domestic        0 |
         +----------------------------------------------------------------------------------------------------------------------------+

    Comment


    • #3

      Code:
      gen wanted = inrange(make, "AMC Concord", "Cad. Deville") 
      would work too, regardless of sort order, if what you want is an alphabetic range.

      Comment

      Working...
      X