Announcement

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

  • creating one variable with multiple levels

    Dear all,

    I currently have this data


    I would like to create a variable = mobility for each ID which can take
    -the value of 1 if mobility_1 = 1 if timefromop = prep
    -Value of 2 if mobility_2 = 1 if timefromop = prep
    -Values of 3 if mobility_3 = 1 if timefromop = prep
    -A value of missing if all mobility* = 0


    Therefore I can then calculate a total mobility score for just preop for each id at each timeop point
    i.e for postop at 30 days, 6 months and 12 months




    Code:
    * Example generated by -dataex-. For more info, type help dataex
    
    clear
    
    input int form float timefromop str11 id byte(sex age mobility___1 mobility___2 mobility___3 selfcare___1 selfcare___2 selfcare___3)
    
    1 1 "66607102"  0 . 0 1 0 1 0 0
    
    1 3 ""          . . 0 0 0 0 0 0
    
    2 1 "159309038" 0 . 0 1 0 0 1 0
    
    3 1 "19305580"  1 . 0 1 0 1 0 0
    
    3 3 ""          . . 0 0 0 0 0 0
    
    4 1 "104252077" 0 . 0 1 0 1 0 0
    
    5 1 "172656787" 1 . 0 1 0 1 0 0
    
    6 1 "68009893"  0 . 0 1 0 1 0 0
    
    6 3 ""          . . 0 1 0 1 0 0
    
    7 1 "56151137"  0 . 0 1 0 1 0 0
    
    8 1 "87923470"  0 . 0 1 0 0 1 0
    
    9 1 "58882663"  0 . 0 1 0 1 0 0
    
    10 1 "149272395" 1 . 0 1 0 1 0 0
    
    11 1 "67013300"  0 . 1 0 0 1 0 0
    
    12 1 "51911287"  0 . 0 1 0 1 0 0
    
    end
    
    label values timefromop r10
    
    label def r10 1 "preop", modify
    
    label def r10 3 "postop6M", modify

    I’m not sure if I explained this clearly but I’ve created a draft dataset of what I think it should look like



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    
    clear
    
    input float(timefromop id mobility)
    
    1 66607104 2
    
    2 66607104 .
    
    . 66607104 .
    
    1 15930938 2
    
    end
    
    label values timefromop r0
    
    label def r0 1 "preop", modify
    
    label def r0 2 "postop30days", modify



    Appreciate if you can help me solve the problem in terms of how I should best sort out the dataset
    Should I just keep preop values, work on this dataset with code and repeat for different time periods followed by append the datasets?







  • #2
    Well, in your example data it is always the case that exactly one of the variables mobility___1 through mobility___3 equals 1. You do not say what you want to do if more than one of them equals 1, or if none of them do. As your question is incomplete, here is an incomplete answer:
    Code:
    gen byte mobility = .
    forvalues i = 1/3 {
        replace mobility = `i' if mobility___`i' == 1 & timefromop == "preop":r10
    }
    This code leaves mobility as missing value if none of the mobility___* variables takes on a 1 value, and it uses the number of the last mobility___* variables to have a 1 value if there is more than one such.

    Comment

    Working...
    X