Announcement

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

  • Converting complex string variable to %tc variables.

    Hey guys,

    Let me preface by saying while I am new to Stata I have tried extensively to figure this out on my own, so my apologies if this is a beginner question. Alright so here it is:

    I have a dataset that includes a string variable that describes an employees shift in the following format: " 03jul2014 2 p.m. to 10 p.m." or even "16may2015 10 p.m. to noon" I've been tasked with determining whether two separate variables (these are in the correct %tc format already) fall within the shift length. However, as has been shown in the above example are not in the correct format. So I have two problems.

    1. How can I convert the shift variables into a %tc variable? I think the best way to accomplish this would be to split the variables into two different variables. This would require destringing the variable and maybe creating some sort of an array split by spaces that I can manipulate into two different variable sets. However, I'm ill-equipped on how to accomplish this.

    2. Because the shifts sometimes go from one day to another I need to adjust the variable for that. I think this would be an IF THEN esque argument that would add +1 to the date dependant on whether the shift goes from pm to am. (or pm to noon). However, this could create a problem if the date is the last day of the month.

    I'm still in the process of learning the software, so any sort of advice or direction for either of these problems would be greatly appreciated.

    Thanks!

  • #2
    There is probably software more suited to this kind of task, but you could eventually accomplish it in Stata. You'd use a number of string-manipulation functions, so start with looking into those.
    Code:
    help string functions
    and maybe
    Code:
    help f_regexm
    For example, because it's fixed-length, you might begin by splitting the date from the time using -substr()- (if you're going to stick with Stata's conventional string functions), and then begin parsing the two time values.

    If there is a finite number of shifts (a handful of start-time and end-time combinations), then it might be more straightforward to use a lookup table; otherwise, you'll need to keep track of a.m. and p.m. and add twelve or not accordingly, as well as to develop logic for adding a day to the date for overnight shifts that involve p.m. to a.m. or p.m. to noon.

    Once you have the components, you can either concatenate them and use -clock()- or split the date component further and use something like -mdyhms() in combination with -real()- if you haven't used -destring- on the variables holding the components.
    Code:
    help datetime
    Stata keeps track of the date well enough, and there won't be a problem with last day of month or year.

    Comment

    Working...
    X