Announcement

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

  • #16
    Just smaller than any value that occurs in the variables concerned.

    Comment


    • #17
      Nick, for what i want to do i need to use your code within a while loop. However, for some reason the last step does not work. The code i am using is the following:

      Code:
      clear
      set obs 10
      set seed 2803
      
      local var = 0
      local ind = 0
       
      while `var' <= 1000000 {
          
          local ++ind
          local var = `var' + ( 1000000 * 0.01 )
      
          forval j = 1/5 {
              gen x`j'`var' = ceil(100 * runiform()) * `var'
          }
      
          gen which_max`var' = ""
          gen max`var' = 0
          unab xvars : x*
      
          quietly foreach x of local xvars {
              replace which_max`var' = "`x'" if `x' > max`var'
              replace max`var' = `x' if `x' > max`var'
          }
      
          quietly foreach x of local xvars {
              gen new`x'`var' = `x' == max`var'
          }
      
      }
      If you run the above code you will see that the last step instead of generating five new variables in each and every iteration, it generates 5 in the first iteration, 5+5 in the second, 5+5+5 in the third and so on.

      I have been trying all day to figure out why this is happening but so far i have been frustrated by failure. Is there a way to prevent this from happening?
      Last edited by Cynthia Inglesias; 22 May 2016, 18:13.

      Comment


      • #18
        Focus on this:

        Code:
          
        unab xvars : x*
        That's working on the names of all your x variables, and the list gets longer each time. Try

        Code:
        while `var' <= 1000000 {
            
            local ++ind
            local var = `var' +  10000
        
            local xvars
        
            forval j = 1/5 {
                gen x`j'`var' = ceil(100 * runiform()) * `var'
                local xvars `xvars' x`j'`var'
            }
        
            gen which_max`var' = ""
            gen max`var' = 0
        
            quietly foreach x of local xvars {
                replace which_max`var' = "`x'" if `x' > max`var'
                replace max`var' = `x' if `x' > max`var'
            }
        
            quietly foreach x of local xvars {
                gen new`x'`var' = `x' == max`var'
            }
        
        }
        Last edited by Nick Cox; 22 May 2016, 23:59.

        Comment


        • #19
          Hilary B. also asked how she could identify the second or third highest variable but apparently that hasn't been discussed. (I guess it was implied in #10?) I was wondering if I could get some help with extending the codes Nick wrote in #10 -- I am new to loops, having just finished reading Nick's short intro piece titled "How to face lists with fortitude."

          Comment


          • #20
            That's trickier, not least because how you would handle ties. I think it needs a different approach. Here's some technique.

            Code:
            clear 
            set seed 2803
            set obs 7 
            forval j = 1/5 { 
                gen y`j' = ceil(12 * runiform()) 
            }
            
            list
            
            gen id = _n 
            sort id 
            save myorigdata, replace  
            reshape long y, i(id) 
            gsort id -y 
            by id: gen rank = _n 
            rename _j which 
            drop y 
            reshape wide which, i(id) j(rank) 
            list 
            sort id 
            merge 1:1 id using myorigdata

            Comment


            • #21
              See also http://www.stata-journal.com/sjpdf.h...iclenum=pr0046 and programs rowsort and rowranks

              Comment


              • #22
                Thanks, Nick! I'd somehow thought it could be simply incorporated into the loop but I guess not.

                Comment


                • #23
                  Also see vorter (SSC).

                  Best Daniel

                  Comment


                  • #24
                    Thanks to Daniel as well.

                    With my data, I had to identify variables, not values so I ended up running the loop in #10 to get the second/third/fourth largest variables. Luckily there are not many ties so I am going to look into individual cases.

                    Comment


                    • #25
                      Originally posted by Nick Cox View Post
                      Thanks for the fix.
                      Dear Nick, how could I find the minimal value of a row and get its var name and value based on this code below?
                      Code:
                      unab xvars: rice wheat crscereals maize pulses oilseeds sugarcane cotton fruitsveg rootstubers fodder
                      
                      gen which_max = "" 
                      gen max = 0 
                      
                      quietly foreach x of local xvars { 
                          replace which_max = "`x'" if `x' > max
                          replace max = `x' if `x' > max
                      }
                      Thanks!

                      Comment


                      • #26
                        #25 You need to reverse the inequalities.... Also, #16 answered a previous question that may or may not apply to you.

                        Comment


                        • #27
                          Here's a sketch:

                          Code:
                          gen which_min = ""  
                          gen min = .  
                          
                          quietly foreach x of local xvars {      
                              replace which_min = "`x'" if `x' < min      
                              replace min = `x' if `x' < min
                          }
                          Note with this that say if x1 x2 x3 x4 are 5 4 3 3 then x3 will be recorded as the variable with the minimum, the first variable to equal the minimum.

                          If you want
                          x4 to be recorded (the last variable) then you need


                          Code:
                          quietly foreach x of local xvars {      
                              replace which_min = "`x'" if `x' <= min      
                              replace min = `x' if `x' < min  
                          }
                          If you want x3 x4 to be recorded (all variables equal to the minimum) then you want

                          Code:
                          quietly foreach x of local xvars {  
                              replace which_min = "`x'" if `x' < min    
                              replace which_min = which_min + " `x'" if `x' == min  
                              replace min = `x' if `x' < min
                          }
                          I think this is along the right lines, but I have not tested it.
                          Last edited by Nick Cox; 25 Jan 2020, 04:16.

                          Comment


                          • #28
                            #12 Added comment

                            You may also wish to eliminate missing values with a condition if your data contains them.

                            Suggested addition template: replace which_max = "`x'" if `x' > max & `x' != .
                            Repeat for second line and others as needed.

                            Comment


                            • #29
                              I have similiar problem from old post above. I tried applying the syntax provided where I am interested in getting maximum value from a range of variables and listing the variable name with the maximum value. However the syntax does not seem to work, please assist:

                              unab xvars: tot_wethos_1rough90 tot_wethos_2emerg90 tot_wethos_3temp90 tot_wethos_6institution90 tot_wethos_7support90 tot_wethos_8insecure90 tot_wethos_9eviction90 tot_wethos_10violence90 tot_wethos_11unconv90

                              gen which_max = ""
                              gen max = 0

                              quietly foreach x of local xvars {
                              replace which_max = "`x'" if `x' > max
                              replace which_max = which_max + " `x'" if `x' == max
                              replace max = `x' if `x' > max
                              }

                              Code:
                              * Example generated by -dataex-. To install: ssc install dataex
                              clear
                              input float(tot_wethos_1rough90 tot_wethos_2emerg90 tot_wethos_3temp90 tot_wethos_6institution90 tot_wethos_7support90)
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . 1
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . 3 . .
                              3 . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              3 . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . 3 . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . 3 . .
                              . . . . .
                              3 . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . 1 . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              . . . . .
                              end
                              ------------------ copy up to and including the previous line ------------------

                              Comment

                              Working...
                              X