Announcement

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

  • New Count Variable of Repeated (Daily) Data

    Hello all, I'd welcome help troubleshooting the creation of a new variable. I have included an example data set below. I am using STATA 16.1 on a MAC
    For context - this is a long format dataset with repeated daily observations at the patient level (e.g., each day "caresdate" has multiple rows of data with assigned times "time2")

    1. I would like to create a variable that "flags" daily visitation (Yes=1, No=0) - this would be combination of the "caresdate" and "response_text" = "visitation" - so for any caresdate where "visitation" is noted = yes. Example, if there were 5 days listed (1) baby_id but only 3 of 5 days had 'visitation' charted - then then the rows on those 3 days would get a 1 (which would repeat b/c long format) and then other 2 days =0.
    Then,
    2. Would it be possible to create a second variable that counts the total number of documented "visitation" per day (e.g. frequency per day) -- if it was noted more than once on the same day. Example, 5 days for (1) baby_id and 3 days had 3 visits each, those days would have a numerical value of 3 and the others 1 o 2 or 0 depending on the count.

    Thank you so much for any assistance!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8 BABY_ID float(caresdate time2 cares qnum) strL response_text
    "Z6373907" 22286 62340000 6 10 "supine"                                                                                                                                                            
    "Z6373907" 22286 62340000 6  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22286 62340000 6  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22286 62340000 6  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22286 62340000 6 13 "10"                                                                                                                                                                
    "Z6373907" 22286 64380000 6  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22286 7.56e+07 8 10 "supine"                                                                                                                                                            
    "Z6373907" 22286 7.56e+07 8  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22286 7.56e+07 8  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22286 7.56e+07 8  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22286 7.56e+07 8 13 "10"                                                                                                                                                                
    "Z6373907" 22286 7.56e+07 8  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22286 7.92e+07 8  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287  2820000 1  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287  2880000 1  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22287  2880000 1  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287  2880000 1  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22287  2880000 1 13 "10"                                                                                                                                                                
    "Z6373907" 22287  5640000 1 10 "supine"                                                                                                                                                            
    "Z6373907" 22287 1.08e+07 2  1 "Visitation"                                                                                                                                                        
    "Z6373907" 22287 1.08e+07 2 10 "supine"                                                                                                                                                            
    "Z6373907" 22287 1.08e+07 2  2 "Mom"                                                                                                                                                               
    "Z6373907" 22287 1.08e+07 2  3 "Breastfeeding"                                                                                                                                                     
    "Z6373907" 22287 1.08e+07 2  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287 1.44e+07 2  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22287 1.44e+07 2  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287 1.44e+07 2  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22287 1.44e+07 2 13 "10"                                                                                                                                                                
    "Z6373907" 22287 2.16e+07 3  1 "Visitation"                                                                                                                                                        
    "Z6373907" 22287 2.16e+07 3 10 "supine"                                                                                                                                                            
    "Z6373907" 22287 2.16e+07 3  2 "Mom"                                                                                                                                                               
    "Z6373907" 22287 2.16e+07 3  3 "Breast and bottle feeding"                                                                                                                                         
    "Z6373907" 22287 2.16e+07 3  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287 2.16e+07 3  5 "Mother"                                                                                                                                                            
    "Z6373907" 22287 2.16e+07 3 13 "10"                                                                                                                                                                
    "Z6373907" 22287 2.16e+07 3  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287 3.24e+07 4 10 "supine"                                                                                                                                                            
    "Z6373907" 22287 3.24e+07 4  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22287 3.24e+07 4  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287 3.24e+07 4  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22287 3.24e+07 4 13 "10"                                                                                                                                                                
    "Z6373907" 22287 3.24e+07 4  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287 3.63e+07 4 15 "completion of antibiotics following 48 hour of negative cultures, mom to breastfeed on demand, infant to H3 with mom after 24 hours of age"                        
    "Z6373907" 22287 3.63e+07 4  7 "ad lib feedings, antibiotic therapy continues as ordered"                                                                                                          
    "Z6373907" 22287 3.63e+07 4  8 "no contact yet this shift"                                                                                                                                         
    "Z6373907" 22287 3.96e+07 4 10 "supine"                                                                                                                                                            
    "Z6373907" 22287 3.96e+07 4  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22287 3.96e+07 4  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287 3.96e+07 4  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22287 3.96e+07 4 13 "10"                                                                                                                                                                
    "Z6373907" 22287 3.96e+07 4  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287 4.32e+07 5  1 "Visitation"                                                                                                                                                        
    "Z6373907" 22287 4.32e+07 5  2 "Mom;Dad"                                                                                                                                                           
    "Z6373907" 22287 45240000 5 15 "negative cultures, feeding well, projected discharge date of 1/9-1/10/21, mom to attempt breastfeeding with infant cues, set mom up on breastpump with 1200 visit."
    "Z6373907" 22287 45240000 5  8 "how is he doing?"                                                                                                                                                  
    "Z6373907" 22287 5.04e+07 5  1 "Visitation"                                                                                                                                                        
    "Z6373907" 22287 5.04e+07 5 10 "held"                                                                                                                                                              
    "Z6373907" 22287 5.04e+07 5 18 "feeding;holding"                                                                                                                                                   
    "Z6373907" 22287 5.04e+07 5  2 "Mom"                                                                                                                                                               
    "Z6373907" 22287 5.04e+07 5  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22287 5.04e+07 5  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287 5.04e+07 5  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22287 5.04e+07 5 13 "15"                                                                                                                                                                
    "Z6373907" 22287 5.04e+07 5  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287 6.12e+07 6  1 "Visitation"                                                                                                                                                        
    "Z6373907" 22287 6.12e+07 6 10 "held"                                                                                                                                                              
    "Z6373907" 22287 6.12e+07 6 11 "mother"                                                                                                                                                            
    "Z6373907" 22287 6.12e+07 6 18 "feeding;holding"                                                                                                                                                   
    "Z6373907" 22287 6.12e+07 6  2 "Mom"                                                                                                                                                               
    "Z6373907" 22287 6.12e+07 6  3 "Breastfeeding;Bottle Feeding"                                                                                                                                      
    "Z6373907" 22287 6.12e+07 6  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287 6.12e+07 6  5 "Mother"                                                                                                                                                            
    "Z6373907" 22287 6.12e+07 6 13 "20"                                                                                                                                                                
    "Z6373907" 22287 6.12e+07 6  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287 7.20e+07 7 10 "supine"                                                                                                                                                            
    "Z6373907" 22287 7.20e+07 7  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22287 7.20e+07 7  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287 7.20e+07 7  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22287 7.20e+07 7 13 "15"                                                                                                                                                                
    "Z6373907" 22287 7.20e+07 7  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22287 8.28e+07 8 10 "supine"                                                                                                                                                            
    "Z6373907" 22287 8.28e+07 8  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22287 8.28e+07 8  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22287 8.28e+07 8  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22287 8.28e+07 8 13 "15"                                                                                                                                                                
    "Z6373907" 22287 8.28e+07 8  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22288  7200000 1 10 "supine"                                                                                                                                                            
    "Z6373907" 22288  7200000 1  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22288  7200000 1  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22288  7200000 1  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22288  7200000 1 13 "10"                                                                                                                                                                
    "Z6373907" 22288  7200000 1  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22288 1.80e+07 2 10 "supine"                                                                                                                                                            
    "Z6373907" 22288 1.80e+07 2  3 "Bottle Feeding"                                                                                                                                                    
    "Z6373907" 22288 1.80e+07 2  4 "22 Cal premature formula"                                                                                                                                          
    "Z6373907" 22288 1.80e+07 2  5 "Nurse"                                                                                                                                                             
    "Z6373907" 22288 1.80e+07 2 13 "10"                                                                                                                                                                
    "Z6373907" 22288 1.80e+07 2  6 "None (Room Air)"                                                                                                                                                   
    "Z6373907" 22288 2.88e+07 3 10 "supine"                                                                                                                                                            
    "Z6373907" 22288 2.88e+07 3  3 "Bottle Feeding"                                                                                                                                                    
    end
    format %td caresdate
    format %tcHH:MM time2
    label values qnum qnum
    label def qnum 1 "Contact Info", modify
    label def qnum 2 "Contact Person", modify
    label def qnum 3 "Feed Method", modify
    label def qnum 4 "Feed Type", modify
    label def qnum 5 "Fed By", modify
    label def qnum 6 "O2 Device", modify
    label def qnum 7 "Ind Care", modify
    label def qnum 8 "Concerns", modify
    label def qnum 10 "Position", modify
    label def qnum 11 "Care by Who", modify
    label def qnum 13 "Feed Min", modify
    label def qnum 15 "Goals", modify
    label def qnum 18 "Mother Care", modify
    label var BABY_ID "BABY_ID" 
    label var qnum "group(question_id)"

  • #2
    Code:
    by BABY_ID caresdate, sort: egen wanted1 = max(response_text == "Visitation")
    by BABY_ID caresdate: egen wanted2 = total(response_text == "Visitation")
    This proposed code is a little bit brittle. It relies on Visitation always being spelled with an initial upper case letter and the rest in lower case, and entered with no leading or trailing blanks. If the source of your data is such that you can rely on that level of consistency, then this code will work properly. But if you cannot, it is best that you first clean up the response_text variable. In that case I would do it as something like:
    Code:
    replace response_text = proper(trim(response_text))
    before running the code.

    (It also relies on Visitation never being misspelled. But solving that problem is much harder, so I'm not going to go there.)

    Comment


    • #3
      Brilliant. Thank you so much. It worked well b/c "Visitation" is a single choice response field so it is always entered the same way, if entered. I appreciate your time!

      Comment

      Working...
      X