Announcement

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

  • Identifying variable that contains rowmax value

    I am trying to identify the variable that equals the rowmax variable, but am running into issues.

    I have school district-level data that contains three variables indicating the percentage of students in the district receiving a certain instruction type: remote, in-person and hybrid. Each row is a unique district. I do the following:

    Code:
    egen max = rowmax(in_person remote hybrid)
    gen inst_type = ""
    replace inst_typ = "in_person" if max == in_person
    replace inst_typ = "hybrid" if max == hybrid
    replace inst_typ = "remote" if max == remote
    Here's a table to illustrate:
    district_id remote_pct hybrid_pct in_person_pct max inst_type
    1 88.5 11.5 0 88.5 remote
    2 11.5 77.8 5.1 77.8 ""
    3 94.4 0 5.6 94.4 ""
    4 23.1 74.1 2.8 74.1 ""
    The variable max is correctly generated. The problem is that inst_type isn't modified the way I want. For each district, I am trying to indicate in which instruction mode most students participated. For example a district for which max==remote will indeed have the correct values but inst_type will be missing instead of inst_typ == "remote". What could I be doing wrong? Apologies if this is a rudimentary question, but I've been at this for over an hour. I am using STATA 16.

    Thanks for the help.

  • #2
    I used the following codes:

    Code:
    clear
    input district_id     remote_pct     hybrid_pct     in_person_pct     max     str30 inst_type
    1     88.5     11.5     0     88.5     remote
    2     11.5     77.8     5.1     77.8     ""
    3     94.4     0     5.6     94.4     ""
    4     23.1     74.1     2.8     74.1     ""
    end
    list
    
    drop max inst_*
    
    egen max = rowmax(in_person remote hybrid)
    gen inst_type = ""
    replace inst_typ = "in_person" if max == in_person
    replace inst_typ = "hybrid" if max == hybrid
    replace inst_typ = "remote" if max == remote
    
    list, sep(0)
    And I was not able to replicate your errors. This is what I got:

    Code:
         +-------------------------------------------------------------+
         | distri~d   remote~t   hybrid~t   in_per~t    max   inst_t~e |
         |-------------------------------------------------------------|
      1. |        1       88.5       11.5          0   88.5     remote |
      2. |        2       11.5       77.8        5.1   77.8     hybrid |
      3. |        3       94.4          0        5.6   94.4     remote |
      4. |        4       23.1       74.1        2.8   74.1     hybrid |
         +-------------------------------------------------------------+
    Consider using -dataex- to post some sample data that can let us reproduce that error.

    Comment


    • #3
      Try:
      Code:
      egen double max = rowmax(in_person remote hybrid)
      which should get you the results you're looking for.

      See also -help precision- for more information why you are encountering these problems.
      Last edited by Ali Atia; 12 Jul 2021, 20:11.

      Comment


      • #4
        Originally posted by Ali Atia View Post
        Try:
        Code:
        egen double max = rowmax(in_person remote hybrid)
        which should get you the results you're looking for.

        See also -help precision- for more information why you are encountering these problems.
        Thank you! This was exactly the issue. I had a feeling it had something to do with the format...

        Comment


        • #5
          Originally posted by Ken Chui View Post
          I used the following codes:

          Code:
          clear
          input district_id remote_pct hybrid_pct in_person_pct max str30 inst_type
          1 88.5 11.5 0 88.5 remote
          2 11.5 77.8 5.1 77.8 ""
          3 94.4 0 5.6 94.4 ""
          4 23.1 74.1 2.8 74.1 ""
          end
          list
          
          drop max inst_*
          
          egen max = rowmax(in_person remote hybrid)
          gen inst_type = ""
          replace inst_typ = "in_person" if max == in_person
          replace inst_typ = "hybrid" if max == hybrid
          replace inst_typ = "remote" if max == remote
          
          list, sep(0)
          And I was not able to replicate your errors. This is what I got:

          Code:
          +-------------------------------------------------------------+
          | distri~d remote~t hybrid~t in_per~t max inst_t~e |
          |-------------------------------------------------------------|
          1. | 1 88.5 11.5 0 88.5 remote |
          2. | 2 11.5 77.8 5.1 77.8 hybrid |
          3. | 3 94.4 0 5.6 94.4 remote |
          4. | 4 23.1 74.1 2.8 74.1 hybrid |
          +-------------------------------------------------------------+
          Consider using -dataex- to post some sample data that can let us reproduce that error.
          Thanks for the help! I was able to solve the issue with Ali's suggestion.

          Comment

          Working...
          X