Announcement

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

  • Creating a subsequent variable after "max" command

    Hi,

    I'm having difficulty in creating a variable after using "max" command. Below illustrates it.

    The initial four variables are ID, Type, p (proportion), and se1 (standard error for p). I used max(p) to assign the maximum of p within each ID and type:

    bys ID type: egen pmax = max(p)

    The last column, se2, is what I want to get. Each se2 value should be the value of se1 that corresponds to pmax (and p). For example, the se2 value for pmax=0.3 should be 0.03 because se1 = 0.03 when p = 0.3.

    What should I code to get the variable, se2? - Thanks in advance.

    ID Type p se1 pmax se2
    1 1 0.5 0.02 0.5 0.02
    1 2 0.3 0.03 0.3 0.03
    1 2 0.1 0.05 0.3 0.03
    2 1 0.4 0.07 0.4 0.07

  • #2
    what do you want to do if p=pmax more that once within an "ID/Type" if the se1's differ?

    note that "max" is a function, not a command (the command is -egen-)

    Comment


    • #3
      Rich,
      First of all, thank you for the correction. I will be mindful of command vs. function in my next postings.
      Regarding your point, I do not have that issue in my dataset. However, if there should be one, I think I need to choose one with a smaller value of se1 for my analysis purpose. I added the 4th row to reflect your point.
      ID Type p se1 pmax se2
      1 1 0.5 0.02 0.5 0.02
      1 2 0.3 0.03 0.3 0.03
      1 2 0.1 0.05 0.3 0.03
      1 2 0.3 0.08 0.3 0.03
      2 1 0.4 0.07 0.4 0.07
      Last edited by Hado Byon; 23 May 2019, 14:16.

      Comment


      • #4
        Can anyone help me with this? Thanks.

        Comment


        • #5
          Hi Hado, I don't know whether this meets your goal. But it at least is right for the data example.
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input byte(ID Type) double(p se1)
          1 1 .5 .02
          1 2 .3 .03
          1 2 .1 .05
          1 2 .3 .08
          2 1 .4 .07
          end
          
          bys ID Type: egen pmax = max(p)
          bys ID Type: gen se2=se1 if pmax==p
          sort ID Type se2
          bys ID Type: replace se2=se2[1]
          2B or not 2B, that's a question!

          Comment


          • #6
            Thanks, Qiang. This is really helpful. An interesting thing is that the syntax does not run as intended with the byte and double option in the input with my STATA/MP 15.1 on Mac. However, without these, (i.e. input ID Type p se1) I get the correct outcome. Does the syntax depend on the type of the variable? The reason I ask this is that the syntax produces no values (i.e. produces missing values) in the se2 column in my own data when it should.

            Comment


            • #7
              I think you'll need

              Code:
               
               bys ID Type: egen double pmax = max(p)
              for this to work properly if p is created double.

              Comment


              • #8
                Thanks, Nick. This solved the issue.

                Comment

                Working...
                X