Announcement

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

  • Help with creating 3 groups based on date and time

    In my dataset, I want to create 3 groups based on date and time: 1) Pitocin ended before LEA placement 2) LEA placement before pitocin start 3) LEA placement and Pitocin occurred simultaneously. There is a fourth group that will be N/A because the date & time are not available.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str16(datetime_pitocin_start datetime_pitocin_stop datetime_lea_placement)
    "NA"               "NA"               "NA"              
    "  1/26/2008 1:54" "  1/26/2008 4:24" "1/26/2008 10:50"
    "   8/8/2009 9:35" "  8/10/2009 5:18" "8/8/2009 8:51"   
    "  6/22/2008 3:26" " 6/23/2008 12:42" "6/22/2008 7:11"  
    "  7/25/2007 4:15" "  7/26/2007 5:23" "7/25/2007 8:42"  
    "NA"               "NA"               "NA"              
    "  4/12/2010 7:23" "  4/12/2010 2:47" "4/12/2010 5:32"  
    "  1/29/2007 5:00" "  1/30/2007 7:09" "NA"              
    "   9/2/2008 2:51" "   9/3/2008 6:36" "9/2/2008 10:01"  
    "NA"               "NA"               "10/24/2009 5:29"
    " 12/11/2008 6:49" " 12/11/2008 6:43" "12/11/2008 8:50"
    "   8/2/2009 4:31" "   8/2/2009 6:26" "8/2/2009 3:36"   
    "NA"               "NA"               "NA"              
    " 7/21/2008 11:58" "  7/28/2008 3:11" "7/21/2008 11:39"
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "11/13/2001 9:00"
    "  9/25/2012 7:04" " 9/26/2012 12:08" "9/25/2012 10:20"
    "  9/16/2008 9:48" "  9/16/2008 8:53" "9/16/2008 12:56"
    "NA"               "NA"               "5/18/2010 6:36"  
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    " 10/28/2009 6:59" " 10/29/2009 6:46" "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "11/27/2007 11:58" " 11/28/2007 6:00" "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "  7/24/2006 7:50" "  7/24/2006 5:29" "NA"              
    "NA"               "NA"               "NA"              
    "  4/22/2006 5:32" " 4/22/2006 10:21" "4/22/2006 11:57"
    " 1/23/2008 10:34" "  1/24/2008 5:00" "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "NA"              
    "  7/12/2006 5:00" "  7/13/2006 1:40" "7/12/2006 11:16"
    "NA"               "NA"               "8/9/2002 12:00"  
    "   8/8/2008 2:32" "   8/8/2008 5:10" "8/7/2008 5:58"   
    "NA"               "NA"               "NA"              
    "   9/2/1998 8:22" "   9/2/1998 7:52" "9/2/1998 3:39"   
    " 12/7/2004 10:09" "  12/7/2004 1:21" "12/7/2004 11:05"
    " 10/21/2000 6:56" " 10/21/2000 9:14" "10/21/2000 10:12"
    "  5/10/1999 6:48" " 5/10/1999 12:22" "NA"              
    "NA"               "NA"               "NA"              
    "NA"               "NA"               "4/8/2002 3:30"   
    " 11/27/2001 7:53" " 11/27/2001 9:48" "11/27/2001 6:00"
    "  3/24/2006 2:10" " 3/24/2006 10:15" "3/24/2006 5:10"  
    "   8/3/2005 1:27" "  8/4/2005 12:11" "8/3/2005 2:50"   
    "  6/18/2001 5:57" " 6/18/2001 10:28" "NA"              
    "  5/16/2001 9:16" "  5/16/2001 6:56" "5/16/2001 1:40"  
    "  1/2/2002 11:03" "   1/2/2002 3:35" "1/2/2002 9:30"   
    " 3/19/2003 12:30" "  3/19/2003 2:41" "3/19/2003 5:39"  
    " 1/17/2005 11:19" "  1/17/2005 7:31" "1/17/2005 4:50"  
    "  8/21/2004 6:55" "  8/21/2004 8:25" "8/21/2004 2:39"  
    "  8/11/1998 6:40" "  8/11/1998 3:26" "NA"              
    "  9/30/2004 8:29" "  9/30/2004 9:30" "9/30/2004 4:54"  
    "NA"               "NA"               "NA"              
    "  7/29/2002 9:30" "  7/30/2002 3:42" "7/29/2002 3:59"  
    "  7/25/2000 7:30" "  7/25/2000 8:34" "7/25/2000 12:00"
    "  3/24/2003 6:42" "  3/25/2003 2:55" "3/24/2003 6:16"  
    "   4/4/2003 9:16" "   4/4/2003 6:10" "NA"              
    " 11/28/2003 6:00" " 11/28/2003 4:25" "11/28/2003 2:29"
    "  10/5/2005 9:45" "  10/5/2005 8:26" "10/5/2005 12:30"
    "  12/6/2001 9:00" "  12/7/2001 8:05" "12/7/2001 3:30"  
    "  4/17/2004 7:40" " 4/17/2004 10:31" "4/17/2004 11:10"
    "NA"               "NA"               "NA"              
    "  5/7/2004 12:33" "   5/7/2004 4:38" "5/7/2004 3:00"   
    " 1/22/1999 10:40" " 1/22/1999 10:44" "NA"              
    "  8/22/1999 9:10" "  8/22/1999 6:20" "NA"              
    "  7/27/2003 6:30" "  7/27/2003 8:45" "7/27/2003 9:14"  
    "  7/7/2003 11:28" "   7/7/2003 5:45" "7/7/2003 11:05"  
    " 11/7/1998 10:18" "  11/7/1998 1:47" "NA"              
    "  6/8/2001 12:14" "   6/9/2001 3:56" "NA"              
    "  12/8/2002 8:37" "  12/8/2002 9:14" "12/8/2002 3:59"  
    "  7/18/2004 3:59" " 7/18/2004 10:18" "7/18/2004 5:55"  
    "NA"               "NA"               "11/16/2004 11:29"
    "  8/21/2004 7:34" "  8/21/2004 1:54" "8/21/2004 9:27"  
    "  6/17/2002 3:49" " 6/17/2002 10:57" "6/17/2002 2:00"  
    " 12/11/2000 7:30" " 12/11/2000 4:12" "12/11/2000 1:30"
    "NA"               "NA"               "NA"              
    "  3/29/2001 8:29" "  3/29/2001 8:15" "3/29/2001 8:39"  
    " 10/29/2011 2:15" "10/29/2011 10:44" "10/29/2011 4:44"
    "  2/13/2003 7:34" " 2/13/2003 10:30" "2/13/2003 7:20"  
    "NA"               "NA"               "2/8/1999 9:40"   
    "NA"               "NA"               "10/25/1997 7:15"
    "   3/7/2006 2:00" "   3/8/2006 3:36" "3/7/2006 10:44"  
    "NA"               "NA"               "NA"              
    "  2/11/2004 6:59" "  2/11/2004 6:24" "2/11/2004 11:32"
    " 10/30/2001 1:34" "10/31/2001 12:30" "10/31/2001 1:30"
    "  6/13/2002 4:30" " 6/13/2002 10:50" "6/13/2002 8:25"  
    end


  • #2
    So, first, two generic aspects of Stata coding:
    1. Date and datetime variables as strings are pretty close to useless and should almost always be converted to Stata internal format numeric variables.
    2. Missing values coded as anything other than Stata numeric missing values (. .a-.z) or Stata string missing value ("") are dangerous and should be recoded.
    Once you do that, the code you ask for is simplicity itself:
    Code:
    foreach v of varlist datetime_* {
        gen double _`v' = clock(`v', "MDYhm")
        assert missing(_`v') == inlist(`v', "", "NA")
        format _`v' %tcnn/dd/CCYY_hh:MM
        drop `v'
        rename _`v' `v'
    }
    
    label define group .n "NA"
    gen byte group:group = 1 if datetime_pitocin_stop < datetime_lea_placement ///
        & !missing(datetime_lea_placement)
    replace group = 2 if datetime_lea_placement < datetime_pitocin_start ///
        & !missing(datetime_pitocin_start)
    replace group = 3 if inrange(datetime_lea_placement, datetime_pitocin_start, ///
        datetime_pitocin_stop) & !missing(datetime_pitocin_start, datetime_pitocin_stop, ///
        datetime_lea_placement)
    replace group = .n if missing(group)
    Note: Since you seem to be fond of "NA" for missing, I have given group a value label that labels missing value .n with "NA", but the actual underlying value that Stata sees is still .n and won't cause you any trouble.

    Comment


    • #3
      Thank you Dr. Schechter for the solution and helpful advice.

      Comment

      Working...
      X