Announcement

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

  • Table and strings

    Hello,

    I am wondering whether possible to build a table and attach a string value to a statistics. Here below the structure of the dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id int year long(enc_subcategory enc_country) str90 glbown float(glb_rnk1_sh glb_rnk2_sh) str53 glb_name_1_ str91 glb_name_2_
     169 2016  1 14 "Agorà Network Srl" 17.867418 15.239153 "Sigma Società Cooperativa"                    " CONAD - Consorzio Nazionale Dettaglianti Scrl"
    2994 2017 12 14 "Agorà Network Srl"   9.37035  8.527388 "Coop Italia scarl"                             " CONAD - Consorzio Nazionale Dettaglianti Scrl"
     169 2020  1 14 "Agorà Network Srl"  16.10198  9.249485 "CONAD - Consorzio Nazionale Dettaglianti Scrl" " D IT Distribuzione Italiana Soc Coop"         
     169 2021  1 14 "Agorà Network Srl" 18.268377   10.3335 "CONAD - Consorzio Nazionale Dettaglianti Scrl" " D IT Distribuzione Italiana Soc Coop"         
    2978 2022 12 14 "Agorà Network Srl" 10.914592   9.13078 "CONAD - Consorzio Nazionale Dettaglianti Scrl" " Coop Italia scarl"                            
     169 2024  1 14 "Agorà Network Srl" 19.001884  9.727362 "CONAD - Consorzio Nazionale Dettaglianti Scrl" " D IT Distribuzione Italiana Soc Coop"         
    2623 2015 12  7 "Aldar Eesti OÜ"    19.763355  18.01085 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
    2647 2016 12  7 "Aldar Eesti OÜ"     19.65514 17.672955 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
    2647 2017 12  7 "Aldar Eesti OÜ"    20.582115  17.51535 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
    2625 2018 12  7 "Aldar Eesti OÜ"    21.220295 17.525837 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
    2647 2019 12  7 "Aldar Eesti OÜ"     21.96116 17.065823 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
    2647 2020 12  7 "Aldar Eesti OÜ"    25.031956 18.157715 "Coop Estonia"                                  " Tallinna Kaubamaja AS"                        
    2625 2021 12  7 "Aldar Eesti OÜ"    25.444847 18.902826 "Coop Estonia"                                  " Tallinna Kaubamaja AS"                        
    2623 2022 12  7 "Aldar Eesti OÜ"     24.92241 17.692562 "Coop Estonia"                                  " Tallinna Kaubamaja AS"                        
    2647 2023 12  7 "Aldar Eesti OÜ"    25.006985 16.922829 "Coop Estonia"                                  " Tallinna Kaubamaja AS"                        
    end
    label values enc_subcategory enc_subcategory
    label def enc_subcategory 1 "1.     Convenience Stores", modify
    label def enc_subcategory 12 "12.   Grocery Retailers", modify
    label values enc_country enc2_country
    label def enc2_country 7 "EE", modify
    label def enc2_country 14 "IT", modify
    I tried the following (hopefully this will help readers to understand what output I am looking for)

    Code:
    levels glb_name_1_, local(C) /*this would be repeated for all glb_name_*_* variables */
    foreach c of local C {
    table (enc_country) (enc_subcategory year ), statistic(mean glb_rnk1_sh) nformat(%19.2fc) sformat("`c'" mean) nototals
    }
    But get the following error:
    option sformat() incorrectly specified; Aegean Oil SA does not conform to the Mata printf() format requirement for a single string argument
    r(7);

    The following option works, but it lists ALL the names stored in glb_name_*_* variables, i.e. also for countries in which it does not exist, and I will have to build one set of tables for each glb_name_*_* variable separately.

    Code:
    table (glb_name_1_ ) (country subcategory year ), statistic(mean glb_rnk1_sh) nformat(%19.2fc) nototals

    All in all, what I would like to have is

    Code:
    table (enc_country ) (enc_subcategory year ) , statistic(mean glb_rnk1_sh) statistic(mean glb_rnk2_sh) nformat(%19.2fc)
    displaying the name stored in glb_name_*_* for each corresponding statistics.

    Thanks anyway,

    cheers

  • #2
    Your question is not at all clear. In the code below, even though you are looping over elements of the local "C", you are just repeatedly creating the same table. If you want to restrict observations to each level of the grouping variable, you need to include an -if- restriction. If your question is how to add text to identify the particular level, that can be done using a title or a note. However, I'm still not sure if that is what you're asking, so I won't attempt to provide a solution until you clarify.


    levels glb_name_1_, local(C) /*this would be repeated for all glb_name_*_* variables */
    foreach c of local C {
    table (enc_country) (enc_subcategory year ), statistic(mean glb_rnk1_sh) nformat(%19.2fc) sformat("`c'" mean) nototal
    }
    Last edited by Andrew Musau; 01 Aug 2025, 06:31.

    Comment


    • #3
      Apologies, I meant the following
      Code:
      table (glb_name_1_ ) (country subcategory year ), statistic(mean glb_rnk1_sh) nformat(%19.2fc) nototals
      but this generates a list of ALL names for each country (many are non-existing, ie missing values), while I would rather have only those names that exist and their associated value of glb_rnk1_sh.

      Ideally I would like to have
      Code:
      table (enc_country ) (enc_subcategory year ) , statistic(mean glb_rnk1_sh) statistic(mean glb_rnk2_sh) nformat(%19.2fc)
      with the names contained in glb_name_1_ glb_name_2_ somehow appearing in the table.
      That is what I tried in
      Code:
       levels glb_name_1_, local(C) /*this would be repeated for all glb_name_*_* variables */
      foreach c of local C {
      table (enc_country) (enc_subcategory year ), statistic(mean glb_rnk1_sh) nformat(%19.2fc) sformat("`c'" mean) nototals
      }
      ) with the
      Code:
      sformat("`c'" mean)
      option, with no success of course.
      Thanks again!

      Comment


      • #4
        If I run

        table (glb_name_1_ ) (enc_country enc_subcategory year ), statistic(mean glb_rnk1_sh) nformat(%19.2fc) nototals
        this is the resulting table:

        Code:
        . table (glb_name_1_ ) (enc_country enc_subcategory year ), statistic(mean glb_rnk1_sh) nformat(%19.2fc) nototals
        
        --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                                                        |                                                              enc_country                                                            
                                                        |                                    EE                                                                 IT                            
                                                        |                             enc_subcategory                                                    enc_subcategory                      
                                                        |                         12.   Grocery Retailers                             1.     Convenience Stores       12.   Grocery Retailers 
                                                        |                                   year                                                 year                           year          
                                                        |   2015    2016    2017    2018    2019    2020    2021    2022    2023     2016     2020    2021    2024          2017          2022
        ------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------
        glb_name_1_                                     |                                                                                                                                     
          CONAD - Consorzio Nazionale Dettaglianti Scrl |                                                                                    16.10   18.27   19.00                       10.91
          Coop Estonia                                  |  19.76   19.66   20.58   21.22   21.96   25.03   25.44   24.92   25.01                                                              
          Coop Italia scarl                             |                                                                                                                   9.37              
          Sigma Società Cooperativa                     |                                                                           17.87                                                     
        --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        There is no row that is completely empty. For each glb_name_1_ value, there is at least one non-empty column. I still don't follow what you are asking. Perhaps illustrate how you want the above table to look like.

        Comment


        • #5
          What I would like to have is something like this (for each country)

          Click image for larger version

Name:	Screenshot 2025-08-04 112215.png
Views:	1
Size:	12.8 KB
ID:	1780655
          Last edited by Federico Antonioli; 04 Aug 2025, 03:23.

          Comment


          • #6
            Like this?

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float id int year long(enc_subcategory enc_country) str90 glbown float(glb_rnk1_sh glb_rnk2_sh) str53 glb_name_1_ str91 glb_name_2_
             169 2016  1 14 "Agorà Network Srl" 17.867418 15.239153 "Sigma Società Cooperativa"                    " CONAD - Consorzio Nazionale Dettaglianti Scrl"
            2994 2017 12 14 "Agorà Network Srl"   9.37035  8.527388 "Coop Italia scarl"                             " CONAD - Consorzio Nazionale Dettaglianti Scrl"
             169 2020  1 14 "Agorà Network Srl"  16.10198  9.249485 "CONAD - Consorzio Nazionale Dettaglianti Scrl" " D IT Distribuzione Italiana Soc Coop"         
             169 2021  1 14 "Agorà Network Srl" 18.268377   10.3335 "CONAD - Consorzio Nazionale Dettaglianti Scrl" " D IT Distribuzione Italiana Soc Coop"         
            2978 2022 12 14 "Agorà Network Srl" 10.914592   9.13078 "CONAD - Consorzio Nazionale Dettaglianti Scrl" " Coop Italia scarl"                            
             169 2024  1 14 "Agorà Network Srl" 19.001884  9.727362 "CONAD - Consorzio Nazionale Dettaglianti Scrl" " D IT Distribuzione Italiana Soc Coop"         
            2623 2015 12  7 "Aldar Eesti OÜ"    19.763355  18.01085 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
            2647 2016 12  7 "Aldar Eesti OÜ"     19.65514 17.672955 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
            2647 2017 12  7 "Aldar Eesti OÜ"    20.582115  17.51535 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
            2625 2018 12  7 "Aldar Eesti OÜ"    21.220295 17.525837 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
            2647 2019 12  7 "Aldar Eesti OÜ"     21.96116 17.065823 "Coop Estonia"                                  " Vilniaus Prekyba UAB"                         
            2647 2020 12  7 "Aldar Eesti OÜ"    25.031956 18.157715 "Coop Estonia"                                  " Tallinna Kaubamaja AS"                        
            2625 2021 12  7 "Aldar Eesti OÜ"    25.444847 18.902826 "Coop Estonia"                                  " Tallinna Kaubamaja AS"                        
            2623 2022 12  7 "Aldar Eesti OÜ"     24.92241 17.692562 "Coop Estonia"                                  " Tallinna Kaubamaja AS"                        
            2647 2023 12  7 "Aldar Eesti OÜ"    25.006985 16.922829 "Coop Estonia"                                  " Tallinna Kaubamaja AS"                        
            end
            label values enc_subcategory enc_subcategory
            label def enc_subcategory 1 "1.     Convenience Stores", modify
            label def enc_subcategory 12 "12.   Grocery Retailers", modify
            label values enc_country enc2_country
            label def enc2_country 7 "EE", modify
            label def enc2_country 14 "IT", modify
            
            
            reshape long glb_rnk@_sh glb_name_@_, i(id year) j(which)
            levelsof enc_country, local(countries)
            foreach c of local countries{
                table ( glb_name__ ) (enc_country enc_subcategory year ) if enc_country==`c', statistic(mean glb_rnk_sh ) nformat(%19.2fc) nototals
            }
            Res.:

            Code:
            -------------------------------------------------------------------------------------------------
                                     |                               enc_country                             
                                     |                                    EE                                 
                                     |                             enc_subcategory                           
                                     |                         12.   Grocery Retailers                       
                                     |                                   year                                
                                     |   2015    2016    2017    2018    2019    2020    2021    2022    2023
            -------------------------+-----------------------------------------------------------------------
            glb_name__               |                                                                       
               Tallinna Kaubamaja AS |                                          18.16   18.90   17.69   16.92
               Vilniaus Prekyba UAB  |  18.01   17.67   17.52   17.53   17.07                                
              Coop Estonia           |  19.76   19.66   20.58   21.22   21.96   25.03   25.44   24.92   25.01
            -------------------------------------------------------------------------------------------------
            
            ---------------------------------------------------------------------------------------------------------------
                                                             |                          enc_country                        
                                                             |                               IT                            
                                                             |                        enc_subcategory                      
                                                             |     1.     Convenience Stores       12.   Grocery Retailers 
                                                             |                year                           year          
                                                             |    2016     2020    2021    2024          2017          2022
            -------------------------------------------------+-------------------------------------------------------------
            glb_name__                                       |                                                             
               CONAD - Consorzio Nazionale Dettaglianti Scrl |   15.24                                   8.53              
               Coop Italia scarl                             |                                                         9.13
               D IT Distribuzione Italiana Soc Coop          |             9.25   10.33    9.73                            
              CONAD - Consorzio Nazionale Dettaglianti Scrl  |            16.10   18.27   19.00                       10.91
              Coop Italia scarl                              |                                           9.37              
              Sigma Società Cooperativa                      |   17.87                                                     
            ---------------------------------------------------------------------------------------------------------------

            Comment


            • #7
              Grand!
              Many thanks Andrew!

              Comment

              Working...
              X