Announcement

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

  • How modify date variable based on another date variable

    Hello everybody,

    I have five date format variables; the first (start) contains day-month-year-time, the remaining four contain only time. Based on the variable start, I would like to include day-month-year in the variable time0_string-time3_string to have something like 12apr2016 15:25:41 instead of 15:25:41. Note also that although they are not posted here, time0_string-time3_string are available also in date format instead of string (in case you think it is easier to work with them).
    Here are is my data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double start str8(time0_string time1_string time2_string time3_string)
    1776078633000 ""         ""         ""         ""        
    1776078178000 "11:03:09" "11:06:19" "11:09:38" "11:11:31"
    1776079237000 "11:21:03" "11:30:54" "11:38:28" "11:42:46"
    1776083212000 ""         ""         ""         ""        
    1776084714000 ""         ""         ""         ""        
    1776085152000 ""         ""         ""         ""        
    1776085132000 "12:59:06" "13:03:02" "13:05:07" "13:08:19"
    1776087094000 "13:31:43" "13:33:56" "13:37:24" "13:38:57"
    1776087196000 "13:33:38" "13:39:31" "13:42:00" "13:53:58"
    1776094048000 ""         ""         ""         ""        
    1776090487000 "14:28:44" "14:40:57" "14:50:10" "14:55:57"
    1776093941000 "15:25:50" "15:30:27" "15:32:34" "15:34:58"
    1776078952000 "11:16:21" "11:22:27" "11:25:10" "11:27:26"
    1776096157000 "16:02:57" "16:09:19" "16:10:11" "16:14:00"
    1776099274000 ""         ""         ""         ""        
    1776100269000 ""         ""         ""         ""        
    1776099143000 "16:52:47" "16:56:20" "16:58:51" "17:01:07"
    1776099712000 "17:02:15" "17:04:31" "17:07:01" "17:08:55"
    1776099221000 "16:53:59" "16:56:59" "16:59:06" "17:00:49"
    1776099570000 "16:59:45" "17:02:27" "17:04:39" "17:05:56"
    1776099828000 "17:04:05" "17:06:59" "17:09:05" "17:10:37"
    1776099952000 "17:06:03" "17:08:59" "17:11:38" "17:13:02"
    1776098397000 "16:40:16" "16:47:01" "16:49:55" "16:54:09"
    1776099405000 "16:56:57" "17:00:15" "17:04:02" "17:06:08"
    1776099302000 "16:55:16" "16:58:42" "17:00:12" "17:02:53"
    1776099660000 "17:01:13" "17:04:55" "17:05:59" "17:07:56"
    1776100176000 "17:09:48" "17:12:48" "17:14:59" "17:16:52"
    1776099312000 "16:55:35" "17:05:49" "17:09:01" "17:11:17"
    1776098285000 "16:38:26" "17:00:02" "17:02:08" "17:04:32"
    1776099535000 "16:59:19" "17:03:46" "17:08:14" "17:10:50"
    end
    format %tc start
    The variable start is currently in date format, which is the following:

    27apr2016 17:02:57
    27apr2016 17:49:04
    26apr2016 18:02:25
    27apr2016 14:06:02
    27apr2016 18:09:34
    27apr2016 18:16:07

    I thank you for the guidance in advance!

    Best,
    Alessio

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double start str8(time0_string time1_string time2_string time3_string)
    1776078633000 ""         ""         ""         ""        
    1776078178000 "11:03:09" "11:06:19" "11:09:38" "11:11:31"
    1776079237000 "11:21:03" "11:30:54" "11:38:28" "11:42:46"
    1776083212000 ""         ""         ""         ""        
    1776084714000 ""         ""         ""         ""        
    1776085152000 ""         ""         ""         ""        
    1776085132000 "12:59:06" "13:03:02" "13:05:07" "13:08:19"
    1776087094000 "13:31:43" "13:33:56" "13:37:24" "13:38:57"
    1776087196000 "13:33:38" "13:39:31" "13:42:00" "13:53:58"
    1776094048000 ""         ""         ""         ""        
    1776090487000 "14:28:44" "14:40:57" "14:50:10" "14:55:57"
    1776093941000 "15:25:50" "15:30:27" "15:32:34" "15:34:58"
    1776078952000 "11:16:21" "11:22:27" "11:25:10" "11:27:26"
    1776096157000 "16:02:57" "16:09:19" "16:10:11" "16:14:00"
    1776099274000 ""         ""         ""         ""        
    1776100269000 ""         ""         ""         ""        
    1776099143000 "16:52:47" "16:56:20" "16:58:51" "17:01:07"
    1776099712000 "17:02:15" "17:04:31" "17:07:01" "17:08:55"
    1776099221000 "16:53:59" "16:56:59" "16:59:06" "17:00:49"
    1776099570000 "16:59:45" "17:02:27" "17:04:39" "17:05:56"
    1776099828000 "17:04:05" "17:06:59" "17:09:05" "17:10:37"
    1776099952000 "17:06:03" "17:08:59" "17:11:38" "17:13:02"
    1776098397000 "16:40:16" "16:47:01" "16:49:55" "16:54:09"
    1776099405000 "16:56:57" "17:00:15" "17:04:02" "17:06:08"
    1776099302000 "16:55:16" "16:58:42" "17:00:12" "17:02:53"
    1776099660000 "17:01:13" "17:04:55" "17:05:59" "17:07:56"
    1776100176000 "17:09:48" "17:12:48" "17:14:59" "17:16:52"
    1776099312000 "16:55:35" "17:05:49" "17:09:01" "17:11:17"
    1876098285000 "16:38:26" "17:00:02" "17:02:08" "17:04:32"
    1876099535000 "16:59:19" "17:03:46" "17:08:14" "17:10:50"
    end
    format %tc start
    
    foreach var of varlist time0_string-time3_string{
        forval i=1/`=_N'{
            qui replace `var'= "`:di %td dofc(start[`i'])'"+" " +`var' in `i' if !missing(`var')
        }
    }
    Res.:

    Code:
    . l, sep(0)
    
         +--------------------------------------------------------------------------------------------------------+
         |              start         time0_string         time1_string         time2_string         time3_string |
         |--------------------------------------------------------------------------------------------------------|
      1. | 12apr2016 11:10:33                                                                                     |
      2. | 12apr2016 11:02:58   12apr2016 11:03:09   12apr2016 11:06:19   12apr2016 11:09:38   12apr2016 11:11:31 |
      3. | 12apr2016 11:20:37   12apr2016 11:21:03   12apr2016 11:30:54   12apr2016 11:38:28   12apr2016 11:42:46 |
      4. | 12apr2016 12:26:52                                                                                     |
      5. | 12apr2016 12:51:54                                                                                     |
      6. | 12apr2016 12:59:12                                                                                     |
      7. | 12apr2016 12:58:52   12apr2016 12:59:06   12apr2016 13:03:02   12apr2016 13:05:07   12apr2016 13:08:19 |
      8. | 12apr2016 13:31:34   12apr2016 13:31:43   12apr2016 13:33:56   12apr2016 13:37:24   12apr2016 13:38:57 |
      9. | 12apr2016 13:33:16   12apr2016 13:33:38   12apr2016 13:39:31   12apr2016 13:42:00   12apr2016 13:53:58 |
     10. | 12apr2016 15:27:28                                                                                     |
     11. | 12apr2016 14:28:07   12apr2016 14:28:44   12apr2016 14:40:57   12apr2016 14:50:10   12apr2016 14:55:57 |
     12. | 12apr2016 15:25:41   12apr2016 15:25:50   12apr2016 15:30:27   12apr2016 15:32:34   12apr2016 15:34:58 |
     13. | 12apr2016 11:15:52   12apr2016 11:16:21   12apr2016 11:22:27   12apr2016 11:25:10   12apr2016 11:27:26 |
     14. | 12apr2016 16:02:37   12apr2016 16:02:57   12apr2016 16:09:19   12apr2016 16:10:11   12apr2016 16:14:00 |
     15. | 12apr2016 16:54:34                                                                                     |
     16. | 12apr2016 17:11:09                                                                                     |
     17. | 12apr2016 16:52:23   12apr2016 16:52:47   12apr2016 16:56:20   12apr2016 16:58:51   12apr2016 17:01:07 |
     18. | 12apr2016 17:01:52   12apr2016 17:02:15   12apr2016 17:04:31   12apr2016 17:07:01   12apr2016 17:08:55 |
     19. | 12apr2016 16:53:41   12apr2016 16:53:59   12apr2016 16:56:59   12apr2016 16:59:06   12apr2016 17:00:49 |
     20. | 12apr2016 16:59:30   12apr2016 16:59:45   12apr2016 17:02:27   12apr2016 17:04:39   12apr2016 17:05:56 |
     21. | 12apr2016 17:03:48   12apr2016 17:04:05   12apr2016 17:06:59   12apr2016 17:09:05   12apr2016 17:10:37 |
     22. | 12apr2016 17:05:52   12apr2016 17:06:03   12apr2016 17:08:59   12apr2016 17:11:38   12apr2016 17:13:02 |
     23. | 12apr2016 16:39:57   12apr2016 16:40:16   12apr2016 16:47:01   12apr2016 16:49:55   12apr2016 16:54:09 |
     24. | 12apr2016 16:56:45   12apr2016 16:56:57   12apr2016 17:00:15   12apr2016 17:04:02   12apr2016 17:06:08 |
     25. | 12apr2016 16:55:02   12apr2016 16:55:16   12apr2016 16:58:42   12apr2016 17:00:12   12apr2016 17:02:53 |
     26. | 12apr2016 17:01:00   12apr2016 17:01:13   12apr2016 17:04:55   12apr2016 17:05:59   12apr2016 17:07:56 |
     27. | 12apr2016 17:09:36   12apr2016 17:09:48   12apr2016 17:12:48   12apr2016 17:14:59   12apr2016 17:16:52 |
     28. | 12apr2016 16:55:12   12apr2016 16:55:35   12apr2016 17:05:49   12apr2016 17:09:01   12apr2016 17:11:17 |
     29. | 14jun2019 02:24:45   14jun2019 16:38:26   14jun2019 17:00:02   14jun2019 17:02:08   14jun2019 17:04:32 |
     30. | 14jun2019 02:45:35   14jun2019 16:59:19   14jun2019 17:03:46   14jun2019 17:08:14   14jun2019 17:10:50 |
         +--------------------------------------------------------------------------------------------------------+
    
    .

    Comment


    • #3
      The code worked perfectly! Thanks a lot Andrew!

      Comment

      Working...
      X