Announcement

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

  • Encode

    Hello,

    I have a string variable. I have to collapse the data, so I'm encoding it using:

    Code:
    encode state, gen(state1)
    When I'm collapsing the data, the value labels generated by encode are being displayed. How can I get the names instead of value labels.

    An example dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str35 ind_id str25 state
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001001" "Tamil Nadu"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001002" "Goa"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    "TUS10001106201913310301382332001003" "Bihar"
    end
    Thank you.

  • #2
    Varsha,

    Feel free to adapt this code which I stole from somewhere in this forum but unfortunately can't recall who to attribute it to. Perhaps Nick Cox

    The idea is:
    1. copy existing variable and value labels to a do file
    2. collapse the file
    3. relabel using the saved do file labels
    Code:
    label save using p53label-v2.do, replace
    
    // COPY VARIABLE LABELS PRECOLLAPSE
    
    foreach v of var * {
        local l`v' : variable label `v'
        local vl`v': value label `v'
        if `"`l`v''"' == "" {
            local l`v' "`v'"
        }
    }
    
    // Collapsing/re-label re-value label row level data for final merges.
    gsort obs mutno
    collapse (firstnm) mrn gender hb anc wbc plt bcount hits hit mhit diag1yes tnaive1 denovo1 secclass  sec3 sec2 taml  p53pos p53tested kargood kstr mono  anymono monomore nmono mono2 kar50 karclonperc comp del5 loss17 loss7 c17loss loss_tot loss_7 loss_5 loss_17 rmono del12p del17q kmar trisom mis2  maxmut maxvaf vafcat maxmis mis1hit m3 iccmhit icc2 b20 b3 bicc  ea74  anymut splice dne_lofclass dnelof dneclass hspot structural_motif revel95 revel bayesdel bayes55  EAP53 rfs rfs_c sift top10 (max) phantmx smotl1 anymis nmis mvaf-vaf50, by(obs)
    
    // Reuse copied variable labels
    
    foreach v of var * {
        label var `v' `"`l`v''"'
    
    }    
    
    ds, has(type numeric)
    foreach v of varlist `r(varlist)'{
        label values `v' `vl`v''    
    }
    // Save data file.

    Comment


    • #3
      What names would those be?

      Comment


      • #4
        Thanks, Girish. I'll check them.
        Nick, I'm referring to the state names (Tamil Nadu, Goa, Bihar and others in the dataset).

        Comment


        • #5
          The names are the value labels and also in the original string variable. Sorry, but I don't understand what you want that isn't accessible to you.

          Comment


          • #6
            Nick, I tried the following codes, but I'm still not able to attach the saved labels.

            Code:
            encode state, gen(state1)
            encode dist, gen(dist1)
            
            foreach v of var state1 dist1 {
                local l`v' : variable label `v'
                   if `"`l`v''"' == "" {
                    local l`v' "`v'"
                 }
             }
             
            collapse (mean) state1 dist1, by(ind_id)
            
            foreach v of var state1 dist1 {
                label var `v' `"`l`v''"'
             }

            Comment

            Working...
            X