Announcement

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

  • Calculate averages by creating year group & bar graph


    Code:
         +--------------------------------------------+
         | Year   Greece    Italy   Portugal    Spain |
         |--------------------------------------------|
      1. | 2009   -4.301   -5.482     -2.978   -3.573 |
      2. | 2010   -5.478    1.687      1.899     .015 |
      3. | 2011   -9.132     .577     -1.827    -.997 |
      4. | 2012     -7.3   -2.819     -4.028   -2.929 |
      5. | 2013   -3.242   -1.728      -1.13   -1.706 |
         |--------------------------------------------|
      6. | 2014      .74     .114       .893    1.379 |
      7. | 2015    -.438     .924      1.822     3.65 |
      8. | 2016    -.191    1.119      1.926    3.169 |
      9. | 2017    1.505    1.597      2.795    2.983 |
     10. | 2018    2.098     .881       2.08    2.529 |
         |--------------------------------------------|
     11. | 2019    2.404     .087      1.677    2.131 |
     12. | 2020    2.158     .909        1.5    1.875 |
         +--------------------------------------------+
    I want to do two things:

    # For each country, calculate averages by creating year group : 2009-2013, 2014-2018, 2019 and 2020. In this case, 2019 and 2020 do not need any calculation.

    # Then generate a bar graph, where for each country there will be 4 bars.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 Year double(Greece Italy Portugal Spain)
    "2009" -4.301 -5.482 -2.978 -3.573
    "2010" -5.478  1.687  1.899   .015
    "2011" -9.132   .577 -1.827  -.997
    "2012"   -7.3 -2.819 -4.028 -2.929
    "2013" -3.242 -1.728  -1.13 -1.706
    "2014"    .74   .114   .893  1.379
    "2015"  -.438   .924  1.822   3.65
    "2016"  -.191  1.119  1.926  3.169
    "2017"  1.505  1.597  2.795  2.983
    "2018"  2.098   .881   2.08  2.529
    "2019"  2.404   .087  1.677  2.131
    "2020"  2.158   .909    1.5  1.875
    end

  • #2
    "a bar graph" doesn't pin the question down. This may help. Run the entire script to see three


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 Year double(Greece Italy Portugal Spain)
    "2009" -4.301 -5.482 -2.978 -3.573
    "2010" -5.478  1.687  1.899   .015
    "2011" -9.132   .577 -1.827  -.997
    "2012"   -7.3 -2.819 -4.028 -2.929
    "2013" -3.242 -1.728  -1.13 -1.706
    "2014"    .74   .114   .893  1.379
    "2015"  -.438   .924  1.822   3.65
    "2016"  -.191  1.119  1.926  3.169
    "2017"  1.505  1.597  2.795  2.983
    "2018"  2.098   .881   2.08  2.529
    "2019"  2.404   .087  1.677  2.131
    "2020"  2.158   .909    1.5  1.875
    end
    
    destring Year, replace 
    
    gen Period = cond(Year <= 2013, 2013, cond(Year <= 2018, 2018, Year))
    
    collapse Greece-Spain, by(Period)
    
    foreach v of var Greece-Spain { 
        label var `v'
    }
    
    label def Period 2013 "2009-13" 2018 "2014-18"
    
    label val Period Period 
    
    list 
    
    set scheme s1color 
    
    graph bar (asis) Greece-Spain, over(Period)  yla(, ang(h)) name(G1, replace)
    
    rename (Greece-Spain) Whatever=
    
    reshape long Whatever, i(Period) j(Country) string 
    
    graph hbar (asis) Whatever, over(Country) over(Period) name(G2, replace)
    
    graph hbar (asis) Whatever, over(Period) over(Country) name(G3, replace)

    Comment


    • #3
      Nick Cox Thanks a lot. Got what I wanted! Also learned some cool tricks. Thanks again!

      Comment

      Working...
      X