Announcement

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

  • Create new variables using the median

    Dear all.
    Please, How can I create new two variables (Z and Y) using the median of variable (X) ? . I mean I want to create variable Z equal to the values above the median of variable X and Y otherwise. Thanks a lot.

  • #2
    If you

    Code:
    summarize X, detail
    then the median of X is accessible afterwards as r(p50) to refer to in later commands. Sometimes it is worth putting this in a scalar for safety say

    Code:
    scalar Xmedian = r(p50)
    I don't understand your definition confidently but you seem to be asking for

    Code:
    gen Z = cond(X > Xmedian, X, Y)

    Comment


    • #3
      Code:
      clear all
      sysuse auto
      
      centile price
      local median=r(c_1)
      generate expensive=price if price>=`median'
      generate cheap=price if price <`median'
      
      sort price
      list price cheap expensive

      Comment


      • #4
        Crossed with Nick's answer, but I understood the question differently, in that it should be TWO new variables from ONE source, not ONE new variable from TWO sources. The question was not very precisely written.

        Comment


        • #5
          Indeed, "two new variables" could mean #3 although in that case separate would work too.

          Comment


          • #6
            Dear Nick Cox & Sergiy Radyakin
            Thanks for your help.
            I mean I have one variable called X and I want to create two variables form X using the median of it. One of them equal to and above the median of it and other one will be the values below the median of it . Thanks

            Comment


            • #7
              OK, so either that is already answered by #3 or #5 or you are still unclear. Which is it?

              Comment


              • #8
                Dear Nick Cox
                No . Every thing is clear now . I thought that you are still not understand my question. Now OK. Thanks a lot and of Course Thanks a lot Dear Sergiy Radyakin.

                Comment


                • #9
                  Indeed; it's a parsing question: where do the braces { } go?

                  I mean I want to create variable Z equal to { the values above the median of variable X and Y otherwise}.

                  I mean I want to create variable Z equal to the {values above the median of variable X} and Y otherwise.

                  My contribution to the question was writing
                  Code:
                  separate
                  ....

                  Comment


                  • #10
                    Dear Nick Cox . I am Sorry . YES, it was my mistake. I was meaning for (Y otherwise ) that Y equal to the values below the median of X.

                    Comment


                    • #11
                      All fine now.

                      Comment


                      • #12
                        Dear Nick Cox
                        Please, I understood the comment from author wrong . Please, I have panel data ( 500 firms , 10 years) . I want to divide the firms into two group based on one of my independent variable "X1" ( using the median of X1) . Group 1 = banks below the median of X1 and group 2 = banks above the median of X1. Please, how can I do it using STATA? Thanks a lot.

                        Comment


                        • #13
                          #3 would be my answer to #12, which I don't understand to be a different question.

                          Comment

                          Working...
                          X