Announcement

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

  • How to create a categorical variable from a list?

    Hello members,

    I would like to seek your suggestion please on how best I can create a categorical variable from the list of the list below, so that I can have a breakdown of a surveyed population into 4 distinct categories: IDP, nomads, urban (u) and rural (r)?

    The goal is to use the categorical variable to create a dummy variable which I can use to compare some estimates for the different categories.

    Thanks,
    Mohamud

    Code:
    tab astrata
    
                      Analytical strata |      Freq.     Percent        Cum.
    ------------------------------------+-----------------------------------
                                    IDP |     48,632        9.02        9.02
                                Nomadic |     53,847        9.99       19.01
                            (u):Banadir |     94,493       17.53       36.55
                             (u):Nugaal |      4,554        0.84       37.39
                         (u):Bari+Mudug |     40,523        7.52       44.91
                    (u):Woqooyi_Galbeed |     20,783        3.86       48.77
         (u):Awdal+Sanaag+Sool+Togdheer |     29,108        5.40       54.17
    (u):Hiraan+MiddleShabelle+Galgaduud |     62,693       11.63       65.80
           (u):Bay+Bakool+LowerShabelle |     74,231       13.77       79.58
             (r):Awdal+Togdheer+Woqooyi |      5,292        0.98       80.56
    (r):Hiraan+MiddleShabelle+Galgaduud |     61,334       11.38       91.94
           (r):Bay+Bakool+LowerShabelle |     43,450        8.06      100.00
    ------------------------------------+-----------------------------------
                                  Total |    538,940      100.00

  • #2
    Muhamud:
    you may want to try:
    Code:
    tab astrata, gen(astrata_cat)
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Code:
      dataex astrata // data shared via -dataex-. To install: ssc install dataex
      clear
      input str36 astrata
      "IDP"                                
      "Nomadic"                            
      "(u):Banadir"                        
      "(u):Nugaal"                        
      "(u):Bari+Mudug"                    
      "(u):Woqooyi_Galbeed"                
      "(u):Awdal+Sanaag+Sool+Togdheer"    
      "(u):Hiraan+MiddleShabelle+Galgaduud"
      "(u):Bay+Bakool+LowerShabelle"      
      "(r):Awdal+Togdheer+Woqooyi"        
      "(r):Hiraan+MiddleShabelle+Galgaduud"
      "(r):Bay+Bakool+LowerShabelle"      
      end

      Code:
      gen astrata_cat = "IDP" if astrata=="IDP"
      replace astrata_cat = "nomads" if astrata=="Nomadic"
      replace astrata_cat = "urban"  if strpos(astrata, "(u)") ==1
      replace astrata_cat = "rural"  if strpos(astrata, "(r)") ==1
      
      . list, noobs abbrev(12)
      
        +---------------------------------------------------+
        |                             astrata   astrata_cat |
        |---------------------------------------------------|
        |                                 IDP           IDP |
        |                             Nomadic        nomads |
        |                         (u):Banadir         urban |
        |                          (u):Nugaal         urban |
        |                      (u):Bari+Mudug         urban |
        |---------------------------------------------------|
        |                 (u):Woqooyi_Galbeed         urban |
        |      (u):Awdal+Sanaag+Sool+Togdheer         urban |
        | (u):Hiraan+MiddleShabelle+Galgaduud         urban |
        |        (u):Bay+Bakool+LowerShabelle         urban |
        |          (r):Awdal+Togdheer+Woqooyi         rural |
        |---------------------------------------------------|
        | (r):Hiraan+MiddleShabelle+Galgaduud         rural |
        |        (r):Bay+Bakool+LowerShabelle         rural |
        +---------------------------------------------------+
      
      
      tabulate astrata_cat, gen(d)
      
      astrata_cat |      Freq.     Percent        Cum.
      ------------+-----------------------------------
              IDP |          1        8.33        8.33
           nomads |          1        8.33       16.67
            rural |          3       25.00       41.67
            urban |          7       58.33      100.00
      ------------+-----------------------------------
            Total |         12      100.00
      
      
      
      sort astrata_cat astrata
      list, noobs abbrev(12) sepby( astrata_cat)
      
        +-----------------------------------------------------------------------+
        |                             astrata   astrata_cat   d1   d2   d3   d4 |
        |-----------------------------------------------------------------------|
        |                                 IDP           IDP    1    0    0    0 |
        |-----------------------------------------------------------------------|
        |                             Nomadic        nomads    0    1    0    0 |
        |-----------------------------------------------------------------------|
        |          (r):Awdal+Togdheer+Woqooyi         rural    0    0    1    0 |
        |        (r):Bay+Bakool+LowerShabelle         rural    0    0    1    0 |
        | (r):Hiraan+MiddleShabelle+Galgaduud         rural    0    0    1    0 |
        |-----------------------------------------------------------------------|
        |      (u):Awdal+Sanaag+Sool+Togdheer         urban    0    0    0    1 |
        |                         (u):Banadir         urban    0    0    0    1 |
        |                      (u):Bari+Mudug         urban    0    0    0    1 |
        |        (u):Bay+Bakool+LowerShabelle         urban    0    0    0    1 |
        | (u):Hiraan+MiddleShabelle+Galgaduud         urban    0    0    0    1 |
        |                          (u):Nugaal         urban    0    0    0    1 |
        |                 (u):Woqooyi_Galbeed         urban    0    0    0    1 |
        +-----------------------------------------------------------------------+
      Last edited by David Benson; 28 Jan 2019, 00:56.

      Comment


      • #4
        If you want to collapse the rural and urban subcategories, then you can try something like the following.
        Code:
        generate byte category = .
        local index 0
        foreach item in ID No "(u" "(r" {
            replace category = `index' if substr(strtrim(astrata), 1, 2) == "`item'"
            local ++index
        }
        Recently, Nick Cox showed an elegant approach using the : word extended macro function (or something similar—I can't remember the details). You might also want to search for that on the forum.

        Comment


        • #5
          Oh, and one final touch: add value labels, if desired.
          Code:
          label define Categories 0 IDP 1 Nomadic 2 Urban 3 Rural
          label values category Categories
          <Stata estimation command> quantitative_attribute i.category

          Comment


          • #6
            Thanks Carlo, David and Joseph.
            Last edited by Mohamud Hussein; 28 Jan 2019, 05:36.

            Comment

            Working...
            X