Announcement

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

  • Generating categorical variable based on time values

    Hello,
    I am working on a dataset which includes measurement data from study participants. Each case has variables for the different measurement times at which measurements were taken. I would like to create categorical variables that groups the measurement times into the three categories of morning, evening or night. Unfortunately, I am not getting anywhere with the datetime help, as my problem seems to be in the syntax of the variable generation.

    Here is an example of a measurement time variable that I want to categorize:
    messung_tim |
    e_20 | Freq. Percent Cum.
    ------------+-----------------------------------
    06:19:00 | 1 8.33 8.33
    06:45:00 | 1 8.33 16.67
    07:18:00 | 1 8.33 25.00
    08:27:00 | 1 8.33 33.33
    13:50:00 | 1 8.33 41.67
    16:01:00 | 1 8.33 50.00
    16:29:00 | 1 8.33 58.33
    17:53:00 | 1 8.33 66.67
    18:27:00 | 1 8.33 75.00
    18:31:00 | 1 8.33 83.33
    20:40:00 | 1 8.33 91.67
    20:57:00 | 1 8.33 100.00
    ------------+-----------------------------------
    Total | 12 100.00


    The format of those variables is %tcHH:MM:SS.

    Here is what I have tried:
    gen MZ20 = .
    replace MZ20 = 1 if messung_time_20 == tc(06:00)/tc(11:59)
    replace MZ20 = 2 if messung_time_20 == tc(12:00)/tc(23:59)
    replace MZ20 = 3 if messung_time_20 == tc(00:00)/tc(05:59)


    Sadly there are always 0 real changes made.
    However, since the value of the categorical variable can be changed when I enter the exact time (for example):

    . replace MZ20 = 1 if messung_time_20 == tc(06:19)
    (1 real change made)


    ... I suspect an error in my approach or syntax.

    Alternatively I tried:

    . recode messung_time_39 (tc(06:00)/tc(12:00) = 1), gen(MZ39)
    unknown el tc in rule


    I am grateful for any help. Many thanks in advance.
    Tim




  • #2
    Tim:
    welcome to this forum.
    Have you already considered the -group- function available from -egen-?
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment

    Working...
    X