Announcement

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

  • Summing the values of specific observations within one variable

    Dear all,

    I am new to STATA and looking for a command to sum specific observations within one variable of my dataset.

    I have the following dataset:

    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str22 fuel double allocated_output
    "Cluster" 1107364
    "Hard coal" 42241315.596366525
    "Hydro" 83835
    "Lignite" 27428060.908967614
    "Liquid" 667447
    "N/A" 20886959.56677246
    "Natural gas" 11916637
    "Nuclear" 4076733
    "RES" 124680
    "Photovoltaik" 31
    "Pumped storage" 3350304.921482086
    "Wind" 9916255
    end
    [/CODE]

    My goal is to sum the "allocated_output" for some observations within my "fuel" variable. For example Photovoltaik and Wind. I tried:
    gen zero-emissions = sum(allocated_output) if fuel==("Photovoltaik, "Wind")]

    However, it does not work and tells me:
    invalid syntax
    r(198);

    Can someone help me to find the correct command? I appreciate your help.

    Best,
    Bianca


  • #2
    The error that bit first was possibly unbalanced quotation marks, but you need inlist() there

    The Stata function sum() is for cumulative or running sums, not what you want here.

    Code:
    clear
    input str22 fuel double allocated_output
    "Cluster" 1107364
    "Hard coal" 42241315.596366525
    "Hydro" 83835
    "Lignite" 27428060.908967614
    "Liquid" 667447
    "N/A" 20886959.56677246
    "Natural gas" 11916637
    "Nuclear" 4076733
    "RES" 124680
    "Photovoltaik" 31
    "Pumped storage" 3350304.921482086
    "Wind" 9916255
    end
    
    egen wanted = total(allocated_output) if inlist(fuel, "Photovoltaik", "Wind")
    
    list, sep(0)
        +--------------------------------------+
         |           fuel   allocat~t    wanted |
         |--------------------------------------|
      1. |        Cluster     1107364         . |
      2. |      Hard coal    42241316         . |
      3. |          Hydro       83835         . |
      4. |        Lignite    27428061         . |
      5. |         Liquid      667447         . |
      6. |            N/A    20886960         . |
      7. |    Natural gas    11916637         . |
      8. |        Nuclear     4076733         . |
      9. |            RES      124680         . |
     10. |   Photovoltaik          31   9916286 |
     11. | Pumped storage   3350304.9         . |
     12. |           Wind     9916255   9916286 |
         +--------------------------------------+

    Comment

    Working...
    X