*! version 3.0.0 21 Apr 2026

Title

timeallocx -- Create diary-level duration, counts, and timing variables for one target activity or category

Syntax

timeallocx varname, did(string) dst(string)

where varname is intended to be a binary indicator equal to 1 for episodes of interest and 0 otherwise.

did(string) variable or variables that uniquely identify each diary; required

dst(string) diary start time on a 24-hour clock; required

Description

timeallocx converts an episode-level diary dataset into a diary-level dataset with one row per diary.

It is designed for situations where the researcher wants detailed information about one specific activity or category, such as eating, paid work, travel, childcare, or time spent at home.

Unlike timealloc, which summarises all categories of a diary field, timeallocx focuses on a single category of the diary field and also creates timing variables.

For each diary, the command creates total time, number of episodes, start and end times for each episode, durations for each episode, and a separate set of variables referring to the last episode in the diary.

This is especially useful when the timing of an activity matters, for example when studying meal times, work start times, commuting, or the final occurrence of an activity during the day.

After running timeallocx, the dataset contains one row per diary. Existing variables are preserved, and identifiers plus the newly created variables are placed first.

Required variables

start must be present in the data and must contain the start minute of each episode.

end must be present in the data and must contain the end minute of each episode.

Episode duration is calculated as end - start.

Arguments

varname should normally be a numeric binary variable coded 1 for the category of interest and 0 otherwise.

For example:

. gen eating = (activity==150)

where code 150 corresponds to eating.

did(string) specifies the variable or variables that jointly identify each diary uniquely.

For example:

. timeallocx eating, did(PUMFID) dst(4)

. timeallocx eating, did(hldid persid id) dst(4)

dst(string) specifies the diary start time using a 24-hour clock.

Examples:

dst(0) = diary begins at midnight

dst(4) = diary begins at 04:00

dst(18) = diary begins at 18:00

This option is used to attach readable clock-time labels to the generated timing variables.

What the command creates

For each diary, timeallocx creates the following variables:

Output

total total minutes where varname==1

episodes number of episodes where varname==1

start1 start2 ... startN start minute of each target episode

end1 end2 ... endN end minute of each target episode

duration1 duration2 ... durationN duration of each target episode

start_last start minute of final target episode in diary

end_last end minute of final target episode in diary

duration_last duration of final target episode in diary

The number of numbered episode variables depends on the maximum number of target episodes observed in the data.

Why the last-episode variables are useful

Many activities occur a different number of times across diaries. For example, some people may eat twice, others three or four times.

When the researcher wants the timing of the final occurrence, using start_last is often more useful than using start3 or start4, because not all diaries contain the same number of episodes.

Examples include the timing of dinner, the last childcare spell of the day, the last paid work episode, or the final screen-use episode before sleep.

How the command works

Internally, timeallocx first keeps the identifiers,
start, end, and the target variable.
It then uses epigenx to redefine episodes based on the target variable, so that adjacent episodes with the same value of varname are merged.

After that, the command keeps only episodes where varname==1 and calculates totals, counts, and timing variables from those episodes.

Finally, it merges those results back with one-row-per-diary background information from the original file.

Dataset after running the command

After running timeallocx, the dataset is reduced to one row per diary.

The variables listed in did() and the newly created summary variables
appear first. Other variables originally in the file are retained, although
episode-level variables are no longer meaningful once the file has been
collapsed to diary level.

Examples

Example 1: Meal timing

This example identifies eating episodes and creates diary-level timing measures.

. use Canada2022, clear

. gen eating = (ACTIVITY==150)

. timeallocx eating, did(PUMFID) dst(4)

The resulting variables can then be inspected:

. list PUMFID total episodes start1 end1 start_last end_last

Example 2: Paid work start times

. gen work = inlist(activity,210,211,212)

. timeallocx work, did(id) dst(4)

. summarize start1

This gives the average first work-start time across diaries.

Example 3: Last screen use before sleep

. gen screen = inlist(activity,610,611)

. timeallocx screen, did(pid day) dst(4)

Use start_last to study the final screen-use episode of the day.

Interpreting timing variables

The variables start1, end1, start_last, and similar variables are stored as minutes since the diary start time specified in dst().

If dst(4) is used, then 0 corresponds to 04:00, 60 to 05:00, and 120 to 06:00.

Because the command attaches clock-time labels, these timing variables can also be read directly in labeled form inside Stata.

Remarks

1. Intended for one binary target at a time

timeallocx analyses one target variable at a time. If you want outputs for another category, reopen the original episode file and run the command again using a different variable.

2. Input should normally be coded 0/1

The command is designed for indicator variables coded 1 for the category of interest and 0 otherwise. It is good practice to create that variable explicitly before running the command.

3. Diaries with no target activity

If a diary contains no episodes where varname==1, total and episodes are set to zero and the timing variables remain missing.

4. If nobody in the dataset has the target activity

If there are no episodes with varname==1 anywhere in the data, the command displays an error message indicating that there is no time in the activity.

5. Input data should be structurally valid

The command assumes start and end correctly define episode timing. If the episode file may contain gaps or overlaps, check or repair it before using timeallocx.

Stored results

timeallocx does not store results in r() or e(). Results are returned through the transformed dataset.

Author

Juana Lamote de Grignon-Pérez

Centre for Time Use Research (CTUR)

Also see

timealloc for diary-level summaries across all categories of a diary field.

epigenx for redefining episode boundaries in an existing episode file.