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
multinomial logistic regression with greatest correspondence between predicted and observed values: why no lasso, stepwise, or elastic net?Hi all, I normally include covariates in my regression models based on theoretical considerations, …
how to select any elements of a matrix randomly ?hi ı couldnt find any way to get rid of this problem. in R ı solved it with Y[j,1]<-sample(get(pa…
randomly assign and simulationHi statalist, Please, could anyone help me? My data contains id, city, month , year and x1. The var…
Calculating months between two dates issues wrong valuesHi all, I am using stata 15.1 a general thing: I have realized that when I use the following comma…
Type I error calculationHello, I am running a simulation and I wanted to know if there is a command that generates the type…
Subscribe to:
Post Comments (Atom)
0 Response to Variable Generation
Post a Comment