Announcement

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

  • find the max value in rows.

    Dear All,
    How could I get the max value in rows (sr11-sr12 s02-s13), and sr1-s13 all types are long. I use the code:

    egen s1=rowmin(sr11 sr12 s02 s03 s04 s05 s06 s07 s08 s09 s10 s11 s12 s13)
    egen s2=rowmax(sr11 sr12 s02 s03 s04 s05 s06 s07 s08 s09 s10 s11 s12 s13)

    but in the first row the final s1=8, the s2=86.

    apparently, I want to get the value in the first row, s1=10%, s2=33%. I am wondering how I should do it.

    symbol enddate1 sr11 sr12 s02 s03 s04 s05 s06 s07 s08 s09 s10 s11 s12 s13
    1 2007 10% 15% 33%
    1 2008 18% 25%
    1 2009 20% 25%
    1 2010 22% 25%
    1 2011 24% 25%
    1 2012 25%
    1 2013 25.00%
    1 2014 25%
    1 2015 25%
    1 2016 25%
    1 2017 25%
    1 2018 25%
    1 2019 25%

  • #2
    The egen functions rowmin() and rowmax() are exactly appropriate for your need.

    We need a data example that reproduces the problem you think you have. For that you should please follow https://www.statalist.org/forums/help#stata and use dataex.

    My guess is that you have encoded string variables that hold percent values together with % characters, which is quite wrong. For example, string values

    1% 10% 11% 12% 2% 20%

    would be encoded to 1 2 3 4 5 6. Encoding follows alphanumeric sort order by default; it doesn't otherwise exploit numeric content.

    If so, you'd need to go back to the original variables and remove the % characters using destring. More at
    https://journals.sagepub.com/doi/pdf...867X1801800413

    (More puzzling yet until it is familiar, but the strings "25%" "25.00%" would not even be encoded by the same value!)

    In a nutshell,

    encode is for mapping string values such as "male" "female" or "frog" "toad" "newt" or "strongly disagree" and so forth to numeric values.

    destring is for extracting numeric values that are wrapped in a string body for some specific reason. Odd characters such as
    Code:
    %
    would be one specific reason, but there could be many others.
    Last edited by Nick Cox; 23 Apr 2024, 12:25.

    Comment


    • #3
      Hi Nick,
      Thanks for your response, I think what you said is right, may it is wrong when I use encode, and I attached the original data at here , please help me.
      I use the code:

      split fn00410,parse("、") destring ignore("、")
      rename fn004101 statrate1
      split statrate1,parse("-") destring ignore("-")

      drop fn00410 statrate1

      encode statrate11,gen (sr11)
      encode statrate12,gen (sr12)
      encode fn004102,gen (s02)
      encode fn004103,gen (s03)
      encode fn004104,gen (s04)
      ......
      egen s1=rowmin(sr11 sr12 s02 s03 s04 s05 s06 s07 s08 s09 s10 s11 s12 s13)
      egen s2=rowmax(sr11 sr12 s02 s03 s04 s05 s06 s07 s08 s09 s10 s11 s12 s13)

      Bests
      Hazel
      Attached Files

      Comment


      • #4
        Thanks for the code, In fact, the problem is worse than said because the results of separate encodes wouldn't usually even be consistent with each other.

        But otherwise, sorry no. A spreadsheet file is not something I am going to look at. Precisely this point is included in the explanation the first link I gave in #2.

        Code:
        dataex fn00410 fn004101 
        is what is needed here by any experienced users willing to address your question.
        Last edited by Nick Cox; 23 Apr 2024, 12:39.

        Comment


        • #5
          . dataex fn00410

          copy starting from the next line ------- ---------------
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str123 fn00410
          "10%、15%、33%"          
          "18%、25%"                
          "20%、25%"                
          "22%、25%"                
          "24%、25%"                
          "25%"                      
          "25.00%"                   
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "33%、15%、17.5%、30.0%"
          "18%、25%"                
          "16.5%、20%、25%"        
          "22%-16.5%"              
          "24%"                      
          "25%"                      
          "25.00%"                   
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%、0%"                 
          "25%"                      
          "33%、15%"                
          "25%、18%"                
          "15%、20%、25%"          
          "25%、22%、15%"          
          "24%"                      
          "25%"                      
          "25%"                      
          "25%、20%、15%"          
          "25%、20%、15%"          
          "25%、15%"                
          "25%"                      
          "25%"                      
          "25%、15%、12.5%"        
          "25%"                      
          "25%"                      
          "25%"                      
          "15%"                      
          "18%、25%"                
          "17.5%、20%、25%"        
          "17.5%、20%、25%"        
          "24%"                      
          "25%"                      
          "25%"                      
          "25%、16.5%"              
          "15%、25%"                
          "15%、25%"                
          "25%"                      
          "15%、25%"                
          "15%、25%"                
          "15%、25%"                
          "15%、25%"                
          "25%"                      
          "15%、33%"                
          "18%、25%"                
          "20%、25%"                
          "22%、25%"                
          "24%"                      
          "25%"                      
          "25.00%"                   
          "25%、10%"                
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%、20%"                
          "25%、20%"                
          "25%、20%、12.5%"        
          "25%、20%、12.5%"        
          "15%、33%"                
          "18%、25%"                
          "25%"                      
          "22%、25%"                
          "24%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          "25%"                      
          ""                         
          "25%"                      
          "25%、5%、2.5%"          
          "25%"                      
          "15%"                      
          "18%"                      
          "20%"                      
          "22%"                      
          end
          copy up to and including the previous line
          Last edited by Hazel Ling; 23 Apr 2024, 12:57.

          Comment


          • #6
            Nick,
            Thank you, I tried to use dataex, but the result is different from original data. so I attached the excel. like you said, the encode and other type, % , all of these make the wrong result.
            so please help me see the excel if you are willing to.

            Hazel
            Last edited by Hazel Ling; 23 Apr 2024, 12:48.

            Comment


            • #7
              The implication of the problem here is that the encoded values are quite useless without value labels. That is linked to why I asked for a dataex of the original string variables. It is not at all good data management to get rid of them. You need to repeat the data import.

              I’ve explained the problem as best as I can. I am at a loss what else to say. Perhaps you need to talk this through at your workplace with someone with a good grasp of Stata.

              Comment


              • #8
                Nick,
                Thank you helpful explanation and quick response, you give me a very good direction I will deal with later, I look for the method to make it by myself at my workplace.

                Best
                Hazel

                Comment


                • #9
                  You revised some earlier posts while I was writing. That was helpful, but for a while we were at cross purposes.

                  The data example shows that you have yet more problems. I used chartab from SSC to find which non-numeric characters exist and are a problem.

                  Code:
                  . chartab f
                  
                     decimal  hexadecimal   character |     frequency    unique name
                  ------------------------------------+-----------------------------------
                          37       \u0025       %     |           154    PERCENT SIGN
                          45       \u002d       -     |             1    HYPHEN-MINUS
                          46       \u002e       .     |            14    FULL STOP
                          48       \u0030       0     |            24    DIGIT ZERO
                          49       \u0031       1     |            37    DIGIT ONE
                          50       \u0032       2     |           118    DIGIT TWO
                          51       \u0033       3     |            11    DIGIT THREE
                          52       \u0034       4     |             6    DIGIT FOUR
                          53       \u0035       5     |           113    DIGIT FIVE
                          54       \u0036       6     |             3    DIGIT SIX
                          55       \u0037       7     |             3    DIGIT SEVEN
                          56       \u0038       8     |             7    DIGIT EIGHT
                      12,289       \u3001       、    |            54    IDEOGRAPHIC COMMA
                  ------------------------------------+-----------------------------------
                  
                                                      freq. count   distinct
                  ASCII characters              =             491         12
                  Multibyte UTF-8 characters    =              54          1
                  Unicode replacement character =               0          0
                  Total Unicode characters      =             545         13
                  The % character we know about. The minus sign looks wrong -- please confirm!

                  In total I did this:

                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input str123 fn00410
                  "10%、15%、33%"          
                  "18%、25%"                
                  "20%、25%"                
                  "22%、25%"                
                  "24%、25%"                
                  "25%"                      
                  "25.00%"                   
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "33%、15%、17.5%、30.0%"
                  "18%、25%"                
                  "16.5%、20%、25%"        
                  "22%-16.5%"              
                  "24%"                      
                  "25%"                      
                  "25.00%"                   
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%、0%"                 
                  "25%"                      
                  "33%、15%"                
                  "25%、18%"                
                  "15%、20%、25%"          
                  "25%、22%、15%"          
                  "24%"                      
                  "25%"                      
                  "25%"                      
                  "25%、20%、15%"          
                  "25%、20%、15%"          
                  "25%、15%"                
                  "25%"                      
                  "25%"                      
                  "25%、15%、12.5%"        
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "15%"                      
                  "18%、25%"                
                  "17.5%、20%、25%"        
                  "17.5%、20%、25%"        
                  "24%"                      
                  "25%"                      
                  "25%"                      
                  "25%、16.5%"              
                  "15%、25%"                
                  "15%、25%"                
                  "25%"                      
                  "15%、25%"                
                  "15%、25%"                
                  "15%、25%"                
                  "15%、25%"                
                  "25%"                      
                  "15%、33%"                
                  "18%、25%"                
                  "20%、25%"                
                  "22%、25%"                
                  "24%"                      
                  "25%"                      
                  "25.00%"                   
                  "25%、10%"                
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%、20%"                
                  "25%、20%"                
                  "25%、20%、12.5%"        
                  "25%、20%、12.5%"        
                  "15%、33%"                
                  "18%、25%"                
                  "25%"                      
                  "22%、25%"                
                  "24%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  "25%"                      
                  ""                         
                  "25%"                      
                  "25%、5%、2.5%"          
                  "25%"                      
                  "15%"                      
                  "18%"                      
                  "20%"                      
                  "22%"                      
                  end
                  
                  chartab fn00410 
                  
                  foreach x in 37 45 12289 { 
                      replace fn00410 = usubinstr(fn00410, uchar(`x'), " ", .)
                  }
                  
                  tab fn00410
                  
                  split fn00410, destring 
                  
                  egen max = rowmax(`r(varlist)')
                  
                  tab max 
                  
                          max |      Freq.     Percent        Cum.
                  ------------+-----------------------------------
                           15 |          2        2.02        2.02
                           18 |          1        1.01        3.03
                           20 |          1        1.01        4.04
                           22 |          2        2.02        6.06
                           24 |          5        5.05       11.11
                           25 |         83       83.84       94.95
                           33 |          5        5.05      100.00
                  ------------+-----------------------------------
                        Total |         99      100.00


                  Comment


                  • #10
                    Hi, Nick. I made an apology about making revision when you were writing your response. the original data set has the type of minus, here, "-" in "22%-16.5%" represents the "、",I need to split it to two numbers, 22% and 16.5%.
                    Thank you for your kind help.
                    Hazel
                    Last edited by Hazel Ling; 24 Apr 2024, 08:13.

                    Comment

                    Working...
                    X