Announcement

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

  • Comando collapse

    Pessoal, me surgiu a seguinte situação:

    collapse (mean) renda, by (municipio f_setor)

    e

    collapse (mean) renda, by (municipio f_escolaridade)

    é possível fazer desses dois collapses em apenas um? Pois eu preciso da renda média por setor por município, mas também preciso da renda media por escolaridade por município.... Eu não posso adicionar a escolaridade junto com o setor pois os resultados não viriam de acordo com o que eu busco.
    Alguma sugestão de como poderia fazer?

    Eu pensei em fazer separadamente, exportar para excel e depois unir, porém, acredito que o merge não funcionaria de acordo.

  • #2
    You can expand the dataset and create a variable that includes both sectors and schools. This assumes that there is no nesting between sectors and schools, otherwise more efficient approaches are possible. You should adjust the code slightly if sector and school are strings as I assume that they are numerical variables here.

    Code:
    clear
    set obs 3000
    set seed 03252022
    gen renda=runiformint(3000, 12500)
    gen municipio= runiformint(1,2)
    gen setor=runiformint(1,7)
    gen escolaidade= runiformint(1,4)
    *START HERE
    expand 2, g(new)
    gen which= cond(new, "setor "+string(setor), "escolaidade "+ string(escolaidade))
    collapse renda, by(municipio which)
    Res.:

    Code:
    . l, sepby(municipio)
    
         +-------------------------------------+
         | munici~o           which      renda |
         |-------------------------------------|
      1. |        1   escolaidade 1   8003.952 |
      2. |        1   escolaidade 2    7610.25 |
      3. |        1   escolaidade 3   7880.893 |
      4. |        1   escolaidade 4   7890.811 |
      5. |        1         setor 1   7735.083 |
      6. |        1         setor 2   7975.025 |
      7. |        1         setor 3   8012.841 |
      8. |        1         setor 4   7745.509 |
      9. |        1         setor 5   7575.452 |
     10. |        1         setor 6   7929.906 |
     11. |        1         setor 7   7955.934 |
         |-------------------------------------|
     12. |        2   escolaidade 1   7947.441 |
     13. |        2   escolaidade 2   7511.496 |
     14. |        2   escolaidade 3   7452.875 |
     15. |        2   escolaidade 4   7968.487 |
     16. |        2         setor 1   7835.009 |
     17. |        2         setor 2   7957.878 |
     18. |        2         setor 3   7590.067 |
     19. |        2         setor 4   7416.826 |
     20. |        2         setor 5   7674.058 |
     21. |        2         setor 6   7916.916 |
     22. |        2         setor 7   7703.454 |
         +-------------------------------------+
    If the sectors and schools are labeled, you can generate the variable "which" above as

    Code:
    decode setor, g(lsetor)
    decode escolaridade, g(lescolaridade)
    ...
    gen which= cond(new, lsetor, lescolaridade)
    so that the entries of the variable "which" are more informative. On the other hand, if you have strings, then you can use them directly in generating the variable.
    Last edited by Andrew Musau; 25 Mar 2022, 15:59.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      You can expand the dataset and create a variable that includes both sectors and schools. This assumes that there is no nesting between sectors and schools, otherwise more efficient approaches are possible. You should adjust the code slightly if sector and school are strings as I assume that they are numerical variables here.

      Code:
      clear
      set obs 3000
      set seed 03252022
      gen renda=runiformint(3000, 12500)
      gen municipio= runiformint(1,2)
      gen setor=runiformint(1,7)
      gen escolaidade= runiformint(1,4)
      *START HERE
      expand 2, g(new)
      gen which= cond(new, "setor "+string(setor), "escolaidade "+ string(escolaidade))
      collapse renda, by(municipio which)
      Res.:

      Code:
      . l, sepby(municipio)
      
      +-------------------------------------+
      | munici~o which renda |
      |-------------------------------------|
      1. | 1 escolaidade 1 8003.952 |
      2. | 1 escolaidade 2 7610.25 |
      3. | 1 escolaidade 3 7880.893 |
      4. | 1 escolaidade 4 7890.811 |
      5. | 1 setor 1 7735.083 |
      6. | 1 setor 2 7975.025 |
      7. | 1 setor 3 8012.841 |
      8. | 1 setor 4 7745.509 |
      9. | 1 setor 5 7575.452 |
      10. | 1 setor 6 7929.906 |
      11. | 1 setor 7 7955.934 |
      |-------------------------------------|
      12. | 2 escolaidade 1 7947.441 |
      13. | 2 escolaidade 2 7511.496 |
      14. | 2 escolaidade 3 7452.875 |
      15. | 2 escolaidade 4 7968.487 |
      16. | 2 setor 1 7835.009 |
      17. | 2 setor 2 7957.878 |
      18. | 2 setor 3 7590.067 |
      19. | 2 setor 4 7416.826 |
      20. | 2 setor 5 7674.058 |
      21. | 2 setor 6 7916.916 |
      22. | 2 setor 7 7703.454 |
      +-------------------------------------+
      If the sectors and schools are labeled, you can generate the variable "which" above as

      Code:
      decode setor, g(lsetor)
      decode escolaridade, g(lescolaridade)
      ...
      gen which= cond(new, lsetor, lescolaridade)
      so that the entries of the variable "which" are more informative. On the other hand, if you have strings, then you can use them directly in generating the variable.
      Novamente, Muito obrigado pela sua atenção!

      Comment

      Working...
      X