Announcement

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

  • recoding a new generated variable

    Hi everyone,
    I have a 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



    *my current data
    clear
    input str20(HouseholdID RespondentID SpouseID cogfunction CareRecipient Caregiver)
    010059 020 030 Normal No caregiver
    010059 020 030 Normal No caregiver
    010059 030 020 Normal Yes Non_caregiver
    010059 030 020 Normal Yes Non_caregiver
    010075 020 030 Normal No caregiver
    010075 020 030 CIND No caregiver
    010075 030 020 Dmented Yes Non_caregiver
    010075 030 020 Dmented Yes Non_caregiver
    end


    *my goal
    clear
    input str20(HouseholdID RespondentID SpouseID cogfunction CareRecipient Caregiver caregiver_new)
    010059 020 030 Normal No caregiver Normal caregiver
    010059 020 030 Normal No caregiver Normal caregiver
    010059 030 020 Normal Yes Non_caregiver Non_caregiver
    010059 030 020 Normal Yes Non_caregiver Non_caregiver
    010075 020 030 Normal No caregiver Dmented caregiver
    010075 020 030 CIND No caregiver Dmented caregiver
    010075 030 020 Dmented Yes Non_caregiver Non_caregiver
    010075 030 020 Dmented Yes Non_caregiver Non_caregiver

    end















  • #2
    One problem is that your data have no unique identifiers. What distinguishes the first two identical observations in your input data from each other? More completely, your example input data contains four observations for respondent 020, three Normal and one CIND and four for respondent 030, two Normal and 2 Dmented. I don't see how these all match up to create your example output data. Were there dates as part of your data that distinguish repeated observations of the same couple?

    And a technical note about using the dataex command. When you copy the dataex output from Stata's Results window, you need to include everything starting with the line containing [CODE] and ending with the line conaining [/CODE]. So if you copy

    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20(HouseholdID RespondentID SpouseID cogfunction CareRecipient Caregiver)
    "010059" "020" "030" "Normal" "No" "caregiver"
    "010059" "020" "030" "Normal" "No" "caregiver"
    "010059" "030" "020" "Normal" "Yes" "Non_caregiver"
    "010059" "030" "020" "Normal" "Yes" "Non_caregiver"
    "010075" "020" "030" "Normal" "No" "caregiver"
    "010075" "020" "030" "CIND" "No" "caregiver"
    "010075" "030" "020" "Dmented" "Yes" "Non_caregiver"
    "010075" "030" "020" "Dmented" "Yes" "Non_caregiver"
    end
    [/CODE]

    and paste it into your post, it will be shown (in the Preview, if you hit the Preview button at the bottom, and after posting) as
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20(HouseholdID RespondentID SpouseID cogfunction CareRecipient Caregiver)
    "010059" "020" "030" "Normal"  "No"  "caregiver"    
    "010059" "020" "030" "Normal"  "No"  "caregiver"    
    "010059" "030" "020" "Normal"  "Yes" "Non_caregiver"
    "010059" "030" "020" "Normal"  "Yes" "Non_caregiver"
    "010075" "020" "030" "Normal"  "No"  "caregiver"    
    "010075" "020" "030" "CIND"    "No"  "caregiver"    
    "010075" "030" "020" "Dmented" "Yes" "Non_caregiver"
    "010075" "030" "020" "Dmented" "Yes" "Non_caregiver"
    end

    Comment


    • #3
      Thanks for the clarification. The observations are not uniquely identified because the data is in a long format. I have updated my example dataset by including the variable of year in the dataex.

      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
      2002 010059    030    020    Normal    Yes    Non_caregiver
      2003 010059    030    020    Normal    Yes    Non_caregiver
      2000 010075    020    030    Normal    No    caregiver
      2001 010075    020    030    CIND    No    caregiver
      2002 010075    030    020    Dmented    Yes    Non_caregiver
      2003 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
      2002 010059    030    020    Normal    Yes    Non_caregiver    Non_caregiver
      2003 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
      2002 010075    030    020    Dmented    Yes    Non_caregiver    Non_caregiver
      2003 010075    030    020    Dmented    Yes    Non_caregiver    Non_caregiver
      end










      Comment


      • #4
        I see now that in your example data your spouse and respondent are not in the same household. You have observations of respondent 020 in 2000 and 2001 in both households, and of respondent 030 in 2002 and 2003 in both households.

        I have transformed your example data into something that I think represents what you intended it to be. Perhaps the code below will start you in a useful direction.
        Code:
        * 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"    
        2002 "010059" "020" "030" "Normal"  "No"  "caregiver"    
        2003 "010059" "020" "030" "CIND"    "No"  "caregiver"    
        2000 "010059" "030" "020" "Normal"  "Yes" "Non_caregiver"
        2001 "010059" "030" "020" "Normal"  "Yes" "Non_caregiver"
        2002 "010059" "030" "020" "Dmented" "Yes" "Non_caregiver"
        2003 "010059" "030" "020" "Dmented" "Yes" "Non_caregiver"
        end
        
        save respondent, replace
        
        drop SpouseID cogfunction CareRecipient
        rename RespondentID SpouseID
        rename Caregiver Spouse_Caregiver
        save spouse, replace
        list, clean noobs abbreviate(16)
        
        use respondent, clear
        merge 1:1 year HouseholdID SpouseID using spouse
        sort year HouseholdID RespondentID
        list, clean noobs abbreviate(16)
        Code:
        . save respondent, replace
        file respondent.dta saved
        
        . 
        . drop SpouseID cogfunction CareRecipient
        
        . rename RespondentID SpouseID
        
        . rename Caregiver Spouse_Caregiver
        
        . save spouse, replace
        file spouse.dta saved
        
        . list, clean noobs abbreviate(16)
        
            year   HouseholdID   SpouseID   Spouse_Caregiver  
            2000        010059        020          caregiver  
            2001        010059        020          caregiver  
            2002        010059        020          caregiver  
            2003        010059        020          caregiver  
            2000        010059        030      Non_caregiver  
            2001        010059        030      Non_caregiver  
            2002        010059        030      Non_caregiver  
            2003        010059        030      Non_caregiver  
        
        . 
        . use respondent, clear
        
        . merge 1:1 year HouseholdID SpouseID using spouse
        
            Result                           # of obs.
            -----------------------------------------
            not matched                             0
            matched                                 8  (_merge==3)
            -----------------------------------------
        
        . sort year HouseholdID RespondentID
        
        . list, clean noobs abbreviate(16)
        
            year   HouseholdID   RespondentID   SpouseID   cogfunction   CareRecipient       Caregiver   Spouse_Caregiver        _merge  
            2000        010059            020        030        Normal              No       caregiver      Non_caregiver   matched (3)  
            2000        010059            030        020        Normal             Yes   Non_caregiver          caregiver   matched (3)  
            2001        010059            020        030        Normal              No       caregiver      Non_caregiver   matched (3)  
            2001        010059            030        020        Normal             Yes   Non_caregiver          caregiver   matched (3)  
            2002        010059            020        030        Normal              No       caregiver      Non_caregiver   matched (3)  
            2002        010059            030        020       Dmented             Yes   Non_caregiver          caregiver   matched (3)  
            2003        010059            020        030          CIND              No       caregiver      Non_caregiver   matched (3)  
            2003        010059            030        020       Dmented             Yes   Non_caregiver          caregiver   matched (3)

        Comment


        • #5
          Thanks for this. I think it would be better if I repost my question with the updated data.

          Comment


          • #6
            This topic is continued at

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

            Comment

            Working...
            X