Announcement

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

  • Updated Issue: recoding a new generated variable

    Hi everyone,
    I have a long form dataset that contains respondents' cognitive functioning and whether they receive care from their spouses. As shown in the below sample data, the data contains both respondent's ID (RespondentID) and their spouse's ID (SpouseID) as well as their household ID (HouseholdID).

    As shown in my goal dataex, I am trying to generate a new caregiver variable (e.g., caregiver_new) that links respondents' cognitive functioning and their spouses' caregiving status. For example, row #1 and #2 (RespondentID=20) provided care for his/her spouse (RespondentID=30) that as shown in row #4 and #5 has a normal cognitive functioning. As such, this person is assigned normal caregiver. Row #3 and #4 is non-caregiver and therefore caregiver_new become simply non-caregiver. As another example, row #5 and #6 provided care for his/her spouses who is demented as such the person is assigned "demented caregiver." Thanks.
    Nader

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    *my current data
    clear
    input float year str20(HouseholdID    RespondentID    SpouseID    cogfunction    CareRecipient    Caregiver)
    2000 010059    020    030    Normal    No    caregiver
    2001 010059    020    030    Normal    No    caregiver
    2000 010059    030    020    Normal    Yes    Non_caregiver
    2001 010059    030    020    Normal    Yes    Non_caregiver
    2000 010075    020    030    Normal    No    caregiver
    2001 010075    020    030    CIND    No    caregiver
    2000 010075    030    020    Dmented    Yes    Non_caregiver
    2001 010075    030    020    Dmented    Yes    Non_caregiver
    end
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    *my goal
    clear
    input float year str20(HouseholdID    RespondentID    SpouseID    cogfunction    CareRecipient    Caregiver    caregiver_new)
    2000 010059    020    030    Normal    No    caregiver    Normal caregiver
    2001 010059    020    030    Normal    No    caregiver    Normal caregiver
    2000 010059    030    020    Normal    Yes    Non_caregiver    Non_caregiver
    2001 010059    030    020    Normal    Yes    Non_caregiver    Non_caregiver
    2000 010075    020    030    Normal    No    caregiver    Dmented caregiver
    2001 010075    020    030    CIND    No    caregiver    Dmented caregiver
    2000 010075    030    020    Dmented    Yes    Non_caregiver    Non_caregiver
    2001 010075    030    020    Dmented    Yes    Non_caregiver    Non_caregiver
    end





  • #2
    Your sample data was not prepared using dataex, apparently, because the values of caregiver_new contain spaces within them, and none of your string variables are surrounded by quotation marks. Had you tried reading in your example data before posting it, you would have seen that what you read in did not match what you expected. In the future, please create Stata datasets and then output them for posting here using the dataex command. Between this topic and your previous posting on this at

    https://www.statalist.org/forums/for...rated-variable

    it has taken more time to try to create and re-created your example data solving the problem given good data did.

    The answer to your current statement of the problem is basically the same as in the previous posting: Create a dataset of spouse data, merge it back to the original dataset, matching the spouse to the corresponding respondent, and then derive what you need.
    Code:
    cls
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float year str20(HouseholdID RespondentID SpouseID cogfunction CareRecipient Caregiver caregiver_new)
    2000 "010059" "020" "030" "Normal"  "No"  "caregiver"     "Normal caregiver"
    2001 "010059" "020" "030" "Normal"  "No"  "caregiver"     "Normal caregiver"
    2000 "010059" "030" "020" "Normal"  "Yes" "Non_caregiver" "Non_caregiver"    
    2001 "010059" "030" "020" "Normal"  "Yes" "Non_caregiver" "Non_caregiver"    
    2000 "010075" "020" "030" "Normal"  "No"  "caregiver"     "Dmented caregiver"
    2001 "010075" "020" "030" "CIND"    "No"  "caregiver"     "Dmented caregiver"
    2000 "010075" "030" "020" "Dmented" "Yes" "Non_caregiver" "Non_caregiver"    
    2001 "010075" "030" "020" "Dmented" "Yes" "Non_caregiver" "Non_caregiver"    
    end
    save wanted, replace
    
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float year str20(HouseholdID RespondentID SpouseID cogfunction CareRecipient Caregiver)
    2000 "010059" "020" "030" "Normal"  "No"  "caregiver"    
    2001 "010059" "020" "030" "Normal"  "No"  "caregiver"    
    2000 "010059" "030" "020" "Normal"  "Yes" "Non_caregiver"
    2001 "010059" "030" "020" "Normal"  "Yes" "Non_caregiver"
    2000 "010075" "020" "030" "Normal"  "No"  "caregiver"    
    2001 "010075" "020" "030" "CIND"    "No"  "caregiver"    
    2000 "010075" "030" "020" "Dmented" "Yes" "Non_caregiver"
    2001 "010075" "030" "020" "Dmented" "Yes" "Non_caregiver"
    end
    save respondent, replace
    
    drop SpouseID CareRecipient
    rename RespondentID SpouseID
    rename cogfunction spouse_cog
    save spouse, replace
    
    use respondent, clear
    merge 1:1 year HouseholdID SpouseID using spouse
    generate caregiver_new = Caregiver
    replace  caregiver_new = spouse_cog+" caregiver" if Caregiver=="caregiver"
    drop spouse_cog _merge
    sort HouseholdID RespondentID year
    
    cf * using wanted, all verbose
    list, clean noobs abbreviate(16)
    Code:
    . cf * using wanted, all verbose
                year:  match
         HouseholdID:  match
        RespondentID:  match
            SpouseID:  match
         cogfunction:  match
       CareRecipient:  match
           Caregiver:  match
       caregiver_new:  match
    
    . list, clean noobs abbreviate(16)
    
        year   HouseholdID   RespondentID   SpouseID   cogfunction   CareRecipient       Caregiver       caregiver_new  
        2000        010059            020        030        Normal              No       caregiver    Normal caregiver  
        2001        010059            020        030        Normal              No       caregiver    Normal caregiver  
        2000        010059            030        020        Normal             Yes   Non_caregiver       Non_caregiver  
        2001        010059            030        020        Normal             Yes   Non_caregiver       Non_caregiver  
        2000        010075            020        030        Normal              No       caregiver   Dmented caregiver  
        2001        010075            020        030          CIND              No       caregiver   Dmented caregiver  
        2000        010075            030        020       Dmented             Yes   Non_caregiver       Non_caregiver  
        2001        010075            030        020       Dmented             Yes   Non_caregiver       Non_caregiver

    Comment

    Working...
    X