Announcement

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

  • Creating the list of macros from STATA variables

    Dear Stata Users,
    I need your help in creating local macros for all three variables in the given dataset.
    For instance, a local macro i of the variable "overall" should display the following: 1"Diesel:49.01%" 2"Fuelwood and traditional biomass: 27.07%" 3"Electricity: 16.66%" .............................. 10"Kerosene: 0.00%"
    Similarly, a local macro j of the variable "agri" should display: 1"Diesel:70.95%" 2"Fuelwood and traditional biomass: 22.61%" 3"Electricity: 5.11%" 4"LPG:1.09%" 5"Petrol:0.24" 6"Solar PV (Self-generated): 0.00%". Please note that there are missing values in "agri" so the numbering has to be done accordingly.

    Here is the example dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str40(overall agri manuf)
    "Diesel: 49.01%"                           "Diesel: 70.95%"                           "Diesel: 49.16%"                          
    "Fuelwood and traditional biomass: 27.07%" "Fuelwood and traditional biomass: 22.61%" "Fuelwood and traditional biomass: 28.97%"
    "Electricity: 16.66%"                      "Electricity: 5.11%"                       "Electricity: 15.32%"                    
    "Furnace-oil: 4.27%"                       ""                                         "Furnace-oil: 4.63%"                      
    "Coal: 1.26%"                              ""                                         "Coal: 1.37%"                            
    "LPG: 1.15%"                               "LPG: 1.09%"                               "LPG: 0.33%"                              
    "Petrol: 0.38%"                            "Petrol: 0.24%"                            "Petrol: 0.19%"                          
    "Solar PV (Self-generated): 0.19%"         "Solar PV (Self-generated): 0.00%"         "Solar PV (Self-generated): 0.03%"        
    "Wind energy (self-generated): 0.00%"      ""                                         "Wind energy (self-generated): 0.01%"    
    "Kerosene: 0.00%"                          ""                                         "Kerosene: 0.00%"                        
    end

    Thank you in advance!!
    Last edited by Pankaj Pokhrel; 15 Jan 2025, 02:00.

  • #2
    So you essentially want to put your dateset into local macros? Why? What's the ultimate goal here?

    Comment


    • #3
      That is not going to work. There is a limit to the number of characters that can be stored in a macro. So for many datasets you would easily exceed those limits.

      So we need another solution. This is a classic XY problem ( https://en.wikipedia.org/wiki/XY_problem ). Nobody wants to store the content of a variable in a macro just because they want to do that and than they are done. It is always a step to achieve some other goal. Since this step is extremely problematic, we need to take another step, but we can only advise you on what that step might be if we know what your real goal is. So can you tell us what the real problem is you want to solve?
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        daniel klein and Maarten Buis
        I wanted these macros to label the legends in a pie chart. Some of the slices of a pie are extremely small and their percentage values do not fit well into them. Therefore I wanted to label the legends that have information on % values.
        For the following data where "Overall" is the percentage value corresponding to different "fueltypes_":
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input double Overall long fueltypes_
         49.0062  1
        27.07401  2
        16.65944  3
        4.267997  4
        1.262856  5
        1.150751  6
        .3820442  7
        .1894862  8
        .0049685  9
        .0022517 10
        end
        label values fueltypes_ fuel
        label def fuel 1 "Diesel", modify
        label def fuel 2 "Fuelwood and traditional biomass", modify
        label def fuel 3 "Electricity", modify
        label def fuel 4 "Furnace-oil", modify
        label def fuel 5 "Coal", modify
        label def fuel 6 "LPG", modify
        label def fuel 7 "Petrol", modify
        label def fuel 8 "Wind energy (self-generated)", modify
        label def fuel 9 "Kerosene", modify
        label def fuel 10 "Solar PV (Self-generated)", modify
        I need to create a pie chart that has the legends with percentage values with the following code:

        Code:
        colorpalette d3 10, select(1/10) nograph //d3.js collor schemes (d3 10) nograph
        return list
        
            
        graph pie Overall, over(fueltypes_) pie(1, color("`r(p1)'")) pie(2, color("`r(p2)'")) pie(3, color("`r(p3)'")) pie(4, color("`r(p4)'")) pie(5, color("`r(p5)'")) pie(6, color("`r(p6)'")) pie(7, color("`r(p7)'")) pie(8, color("`r(p8)'")) pie(9, color("`r(p9)'")) pie(10, color("`r(p10)'")) graphregion(fcolor(white))plabel(_all percent) legend(size(*0.6) region(lstyle(none)) ring(0.5) pos(3) col(1)   order (`j'))
        where the local macro `j' contains the value labels of "fueltypes_" with their percentage value. It would also be great to have percentage values, for example, in %4.2f format. Also including the percentage sign "%" in the tail.
        Last edited by Pankaj Pokhrel; 15 Jan 2025, 03:29.

        Comment


        • #5
          This is still a x-y problem. You don't need local macros at all to get a pie chart, or any other graph, of these data.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input double Overall long fueltypes_
           49.0062  1
          27.07401  2
          16.65944  3
          4.267997  4
          1.262856  5
          1.150751  6
          .3820442  7
          .1894862  8
          .0049685  9
          .0022517 10
          end
          label values fueltypes_ fuel
          label def fuel 1 "Diesel", modify
          label def fuel 2 "Fuelwood and traditional biomass", modify
          label def fuel 3 "Electricity", modify
          label def fuel 4 "Furnace-oil", modify
          label def fuel 5 "Coal", modify
          label def fuel 6 "LPG", modify
          label def fuel 7 "Petrol", modify
          label def fuel 8 "Wind energy (self-generated)", modify
          label def fuel 9 "Kerosene", modify
          label def fuel 10 "Solar PV (Self-generated)", modify
          
          graph pie [aw=Overall], over(fueltypes_)
          Now throw that graph away, with its awkward legend obliging the reader to repeated back and forth, and go for something more direct such as

          Code:
          graph hbar [aw=Overall], over(fueltypes_) blabel(bar, format(%4.3f)) ysc(r(0 52))
          Getting colours you like is extra.

          Note that you could do something like this

          Code:
          clear
          input double Overall long fueltypes_
           49.0062  1
          27.07401  2
          16.65944  3
          4.267997  4
          1.262856  5
          1.150751  6
          .3820442  7
          .1894862  8
          .0049685  9
          .0022517 10
          end
          label values fueltypes_ fuel
          label def fuel 1 "Diesel", modify
          label def fuel 2 "Fuelwood and traditional biomass", modify
          label def fuel 3 "Electricity", modify
          label def fuel 4 "Furnace-oil", modify
          label def fuel 5 "Coal", modify
          label def fuel 6 "LPG", modify
          label def fuel 7 "Petrol", modify
          label def fuel 8 "Wind energy (self-generated)", modify
          label def fuel 9 "Kerosene", modify
          label def fuel 10 "Solar PV (Self-generated)", modify
          
          forval j = 1/10 { 
              local this : label (fueltypes_) `j'
              local that = "`this' (" + strofreal(Overall[`j'], "%4.3f") + ")"
              label def fuel2 `j' "`that'", add 
          }
          
          label val fueltypes_ fuel2 
          
          graph pie [aw=Overall], over(fueltypes_)
          but now you have to shrink the legend or the pie chart to get it readable. Sorry, but I still think it's a really bad idea. Or if it is someone else's bad idea, that's still my view.
          Last edited by Nick Cox; 15 Jan 2025, 03:54.

          Comment


          • #6
            Thank you for your ideas.

            Comment


            • #7
              For anyone interested, I tweaked the display format. As often, there is a delicate trade-off between displaying too many digits and too few.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input double Overall long fueltypes_
               49.0062  1
              27.07401  2
              16.65944  3
              4.267997  4
              1.262856  5
              1.150751  6
              .3820442  7
              .1894862  8
              .0049685  9
              .0022517 10
              end
              label values fueltypes_ fuel
              label def fuel 1 "Diesel", modify
              label def fuel 2 "Fuelwood and traditional biomass", modify
              label def fuel 3 "Electricity", modify
              label def fuel 4 "Furnace-oil", modify
              label def fuel 5 "Coal", modify
              label def fuel 6 "LPG", modify
              label def fuel 7 "Petrol", modify
              label def fuel 8 "Wind energy (self-generated)", modify
              label def fuel 9 "Kerosene", modify
              label def fuel 10 "Solar PV (Self-generated)", modify
              
              graph hbar [aw=Overall], over(fueltypes_) blabel(bar, format(%4.3f)) ysc(r(0 52))
              
              graph hbar [aw=Overall], over(fueltypes_) blabel(bar, format(%8.4g)) ysc(r(0 52))
              With the second display format, you get about 4 significant figures.

              A bigger deal as compared with the pie chart is better use of space -- while maintaining all important detail of both text and magnitudes -- that comes with cutting a legend and using direct labelling of all categories.

              Click image for larger version

Name:	hbar.png
Views:	1
Size:	48.8 KB
ID:	1770966

              Comment

              Working...
              X