Announcement

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

  • Creating column which pulls string variable based on the maximum of a float

    I am trying to create a variable that captures which states have the most hospital admissions by hospital systems. I created a fake dataset here. I have variables for system ID, adjusted admissions by hospital, and state. I then created variables for total admissions by state at the system level and the max value at the system and state level. I am having trouble creating a column that has the state itself as opposed to a float of admissions

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int SystemID long AdjustedAdmissionsByHosp str2 State float(state_admission max_state_admission)
    2100  594682 "DE"  594682  594682
    2100  209468 "MI"  209468  594682
    2100  128958 "NC"  128958  594682
    2100  349834 "WI"  349834  594682
    4500   12095 "CA"   12095  147367
    4500  123958 "MN"  147367  147367
    4500   23409 "MN"  147367  147367
    4500   50968 "TX"   50968  147367
    6500 2387350 "KS" 2387350 2387350
    6500    2395 "MS"    2395 2387350
    end
    label var SystemID "SystemID" 
    label var AdjustedAdmissionsByHosp "AdjustedAdmissionsByHosp" 
    label var State "State "
    Last edited by Sneha Verma; 26 Oct 2021, 10:26.

  • #2
    Code:
    bysort SystemID (Adjusted) : gen wanted = State[_N]

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      bysort SystemID (Adjusted) : gen wanted = State[_N]
      Thank you for this! I had a quick question about what the [_N] part does?

      Comment


      • #4
        _N indexes the last observation (in a block of observations). See for example

        Code:
        help _variables 
        
        help by
        https://www.stata-journal.com/articl...article=pr0004

        #1 was cross-posted at https://stackoverflow.com/questions/...f-a-float-in-s

        Please note our policy on cross-posting, explicit in the FAQ Advice, that you are asked to tell us about it.

        Comment

        Working...
        X