Announcement

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

  • Creating Dummy variables for all Variables on the basis of median values

    I have 10 variables and want to create dummy for them based on their median values, is there any easy solution or loop to do that.
    Thanks
    Attaullah Shah
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    It depends what you mean by
    based on their median values
    , if you wanted an indicator for, say, whether they were less than the median, you could do something like:

    Code:
    local varnames = "var1 var2 ... var10"
    
    foreach var of local varnames {
    
    egen temp = median(`var')
    gen `var'_dummy = 0
    replace `var'_dummy = 1 if `var' < temp
    drop temp
    
    }
    Mike

    Comment


    • #3
      An example:
      Code:
      sysuse auto, clear
      foreach var of varlist rep78 turn    {
          qui su `var',d
          gen `var'_m=`var'==`r(p50)'
      }
      Last edited by Aspen Chen; 15 Sep 2014, 13:29.

      Comment


      • #4
        Aspen Chen: I think you want >= not ==

        Comment


        • #5
          Nick, I really thought the OP had meant "exactly the median". But you're probably right. The >= qualifier seems a more useful application.

          Comment


          • #6
            Thank you Aspen Chen, mmurphy, and Nick for your wonderful solution. One last question, how would the code treat missing values, do we need to make some adjustment in that code. My best guess would be, for example in the Aspen Chen code, to use
            sysuse auto, clear
            foreach var of varlist rep78 turn {
            qui su `var',d
            gen `var'_m=`var'>=`r(p50)' if `var'!=.
            }
            Last edited by Attaullah Shah; 15 Sep 2014, 21:46.
            Regards
            --------------------------------------------------
            Attaullah Shah, PhD.
            Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
            FinTechProfessor.com
            https://asdocx.com
            Check out my asdoc program, which sends outputs to MS Word.
            For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

            Comment


            • #7
              Or perhaps simpler is

              foreach v of varlist [whatever] {
              xtile `v'_msplit = `v', nq(2)
              }

              These indicator variables will be coded as 1 and 2 rather than 0/1, but you can always subtract 1 if that's necessary.

              Comment

              Working...
              X