Announcement

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

  • collapse using same variables for two purposes

    I want to do a collpase per year to get the mean the the total number of observations for all my numeric variables. The problem is that I need to give names for these variables once they are used for (mean) and (count) and so stata cannot keep the same name for both. Is there a smart way of giving vames (like m_variable c_variable) for all of them, without having to write collapse (mean) m_idade = idade m_d_bra = d_bra .....?


    Code:
    collapse (mean) idade d_bra d_fem loc_obit d_asmed d_exa d_cir viol_obit d_tbobit d_graobit puer_obit ///
    d_mat d_fet d_inf m_idad m_escol m_gra m_gest m_d_pn m_qtfv m_qtfm racacor1 racacor2 racacor3 ///
    escol1 escol2 escol3 escol4 loc_obit1 loc_obit2 m_pes1 m_pes2 m_pes3 m_obpar1 m_obpar2 ///
    m_obpar3 tpcid_cau1 tpcid_cau2 tpcid_cau3 tpcid_cau4 tpcid_cau5 tpcid_ori1 tpcid_ori2 ///
    tpcid_ori3 tpcid_ori4 tpcid_ori5 (count) idade d_bra d_fem loc_obit d_asmed d_exa d_cir viol_obit d_tbobit d_graobit puer_obit ///
    d_mat d_fet d_inf m_idad m_escol m_gra m_gest m_d_pn m_qtfv m_qtfm racacor1 racacor2 racacor3 ///
    escol1 escol2 escol3 escol4 loc_obit1 loc_obit2 m_pes1 m_pes2 m_pes3 m_obpar1 m_obpar2 ///
    m_obpar3 tpcid_cau1 tpcid_cau2 tpcid_cau3 tpcid_cau4 tpcid_cau5 tpcid_ori1 tpcid_ori2 ///
    tpcid_ori3 tpcid_ori4 tpcid_ori5, by (anom_obit)

  • #2
    Paula, I don't think your code above is valid. You can't mention the same variable twice in the manner you do in above. Stata will give an error 198 "name conflict".
    Code:
    . collapse (mean) price (count) price
    error:
           price = (mean) price
           price = (count) price
    name conflict
    r(198);
    Instead use new variable names:
    Code:
    clear
    sysuse auto
    collapse (min) minprice=price (max)maxprice=price, by(foreign)
    Code:
         +--------------------------------+
         |  foreign   minprice   maxprice |
         |--------------------------------|
      1. | Domestic      3,291     15,906 |
      2. |  Foreign      3,748     12,990 |
         +--------------------------------+
    And if you have a long variables list, just loop over them:
    Code:
    clear
    sysuse auto
    
    foreach v in price weight length {
     local l "`l' (min) min`v'=`v' (max) max`v'=`v'"
    }
    
    collapse `l', by(foreign)
    Produces:
    Code:
         +----------------------------------------------------------------------------+
         |  foreign   minprice   maxprice   minwei~t   maxwei~t   minlen~h   maxlen~h |
         |----------------------------------------------------------------------------|
      1. | Domestic      3,291     15,906      1,800      4,840        147        233 |
      2. |  Foreign      3,748     12,990      1,760      3,420        142        193 |
         +----------------------------------------------------------------------------+
    Best, Sergiy Radyakin
    Last edited by Sergiy Radyakin; 01 Jul 2015, 19:48.

    Comment


    • #3
      Originally posted by Paula de Souza Leao Spinola View Post
      I want to do a collpase per year to get the mean the the total number of observations for all my numeric variables.
      The code below saves you typing out the names of all numeric variables. In the example the variable "foreign" stands for your year variable. I assume that the year variable is also numeric.
      Code:
      sysuse auto, clear
      
      * Identify numeric variables
      ds, not(type string)
      
      * Remove the numeric variable "foreign" from the list
      local vars = subinstr("`r(varlist)'","foreign","",.)
      
      * Copy and rename all numeric variables
      foreach var of varlist `vars' {
        clonevar c_`var' = `var'
        ren `var' m_`var'
      }
      
      * Collapse data
      collapse (mean) m_* (count) c_*, by(foreign)
      The new dataset has these variables.
      Code:
      . d
      
      Contains data
        obs:             2                          1978 Automobile Data
       vars:            21                          
       size:           162                          (_dta has notes)
      -----------------------------------------------------------------------------------
                    storage   display    value
      variable name   type    format     label      variable label
      -----------------------------------------------------------------------------------
      foreign         byte    %8.0g      origin     Car type
      m_price         float   %8.0gc                (mean) m_price
      m_mpg           float   %8.0g                 (mean) m_mpg
      m_rep78         float   %8.0g                 (mean) m_rep78
      m_headroom      float   %6.1f                 (mean) m_headroom
      m_trunk         float   %8.0g                 (mean) m_trunk
      m_weight        float   %8.0gc                (mean) m_weight
      m_length        float   %8.0g                 (mean) m_length
      m_turn          float   %8.0g                 (mean) m_turn
      m_displacement  float   %8.0g                 (mean) m_displacement
      m_gear_ratio    float   %6.2f                 (mean) m_gear_ratio
      c_price         long    %8.0gc                (count) c_price
      c_mpg           long    %8.0g                 (count) c_mpg
      c_rep78         long    %8.0g                 (count) c_rep78
      c_headroom      long    %6.1f                 (count) c_headroom
      c_trunk         long    %8.0g                 (count) c_trunk
      c_weight        long    %8.0gc                (count) c_weight
      c_length        long    %8.0g                 (count) c_length
      c_turn          long    %8.0g                 (count) c_turn
      c_displacement  long    %8.0g                 (count) c_displacement
      c_gear_ratio    long    %6.2f                 (count) c_gear_ratio
      -----------------------------------------------------------------------------------
      Sorted by: foreign
      Last edited by Friedrich Huebler; 01 Jul 2015, 20:22.

      Comment

      Working...
      X