Dear Statalist,
I have the following dataset showing each person each day working multiple shifts. For instance, the first row means the id1 day1 shift1 lasts for 20 minutes.
I want to generate a duration lag variable showing the working duration of the previous shift of the same day (as shown by the last variable durationlag)
clear
input byte(id date shift duration durationlag)
1 1 1 20 .
1 1 1 20 .
1 2 1 30 .
1 2 2 50 30
2 1 1 60 .
2 1 1 60 .
2 1 2 70 60
2 1 2 70 60
end
[/CODE]
I treid the following code but it does not work well.
gen durationlag=.
bys id date : replace durationlag=duration[_n-1]
bys id date shift: replace durationlag=durationlag[_n-1]
Can someone give me some advice?
Thank you very much.
Best,
Changjun
Related Posts with Variable Generation
Regression Command for pooled DataHi.. I have four countries cross section data in my sample. i regressed separate regression for each…
Help Reshaping/Merging dataDear all, I am using Stata 16, on mac. Here is snapshot of a piece of my dataset. I have collected d…
Generating residuals with miHi all, On p. 296 of the Stata 16 MI manual, the following is stated: The MI predictions shoul…
Table CreationHey Statalists, I have data with variable named district, hospital name and total number of cases ha…
GPH files compatibility: Stata 15 and 16Hi everyone, I am trying to open .gph files created in Stata 16 using Stata 15. However, I get this…
Subscribe to:
Post Comments (Atom)
0 Response to Variable Generation
Post a Comment