This post is an extension of a previous post of mine that I've yet to fully solve: https://www.statalist.org/forums/for...-based-on-week. One addition I've made since that post is that I created a variable called -week- that essentially counts up, by 1s, for each Thursday-to-Wednesday "week". I'm hoping that may make looping potentially easier...
Essentially, what I'm hoping for is generating a new variable: once -attend- is a 1 within a Thursday-to-Wednesday week, then all days in that week following (i.e., up until the next Thursday) are also a 1.
As a concrete example, in the data I provided: for id 16482, on Thursday, January 9, 2020, that is a 1 for -attend-. Therefore, the Friday, Saturday, Sunday, Monday, Tuesday, and Wednesday following it should also be a 1--but we can see that the Friday and Saturday immediately following are both 0s. There are a few variations of this throughout the data example, and most simply put, I'm hoping to get some code that make those 0s into 1s.
I've been trying to work with some code that uses a foreach loop and local macro based on Nick Cox's suggestion (but my modification fails due to syntax error):
Code:
sort id date gen work = . gen wanted = attend local D = 6 foreach d in 4 5 6 0 1 2 { replace work = cond(attend == 1 & week_day == `d', date, .) by id : replace work = work[_n-1] if work == 0 by id: replace wanted = wanted[_n-1] if wanted == 0 & inrange(date - work, 1, `D') local D = `D' - 1 }
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(id date attend day week) 16482 21923 1 4 1 16482 21924 0 5 1 16482 21925 0 6 1 16482 21926 1 0 1 16482 21927 1 1 1 16482 21928 1 2 1 16482 21929 1 3 1 16482 21930 1 4 2 16482 21931 1 5 2 16482 21932 1 6 2 16482 21933 1 0 2 16482 21934 1 1 2 16482 21935 1 2 2 16482 21936 1 3 2 16482 21937 1 4 3 16482 21938 1 5 3 16482 21939 1 6 3 16482 21940 1 0 3 16482 21941 1 1 3 16482 21942 1 2 3 16482 21943 1 3 3 16482 21944 1 4 4 16482 21945 1 5 4 16482 21946 1 6 4 16482 21947 1 0 4 16482 21948 1 1 4 16482 21949 1 2 4 16482 21950 1 3 4 16482 21951 1 4 5 16482 21952 1 5 5 16482 21953 1 6 5 16482 21954 1 0 5 16482 21955 1 1 5 16482 21956 1 2 5 16482 21957 1 3 5 16482 21958 0 4 6 16482 21959 0 5 6 16482 21960 0 6 6 16482 21961 0 0 6 16482 21962 0 1 6 16482 21963 0 2 6 16482 21964 0 3 6 16482 21965 1 4 7 16482 21966 1 5 7 16482 21967 1 6 7 16482 21968 1 0 7 16482 21969 1 1 7 16482 21970 1 2 7 16482 21971 1 3 7 16482 21972 1 4 8 16482 21973 1 5 8 16482 21974 1 6 8 16482 21975 1 0 8 16482 21976 1 1 8 16482 21977 1 2 8 16482 21978 1 3 8 16482 21979 1 4 9 16482 21980 1 5 9 16482 21981 1 6 9 16482 21982 1 0 9 16482 21983 1 1 9 16482 21984 1 2 9 16482 21985 1 3 9 16482 21986 0 4 10 16482 21987 0 5 10 16482 21988 1 6 10 16482 21989 0 0 10 16482 21990 . 1 10 16482 21991 . 2 10 16482 21992 . 3 10 16482 21993 . 4 11 16482 21994 . 5 11 16482 21995 . 6 11 16482 21996 . 0 11 16482 21997 . 1 11 16482 21998 . 2 11 16482 21999 . 3 11 16483 21937 0 4 3 16483 21938 1 5 3 16483 21939 1 6 3 16483 21940 1 0 3 16483 21941 1 1 3 16483 21942 1 2 3 16483 21943 1 3 3 16483 21944 1 4 4 16483 21945 1 5 4 16483 21946 1 6 4 16483 21947 1 0 4 16483 21948 1 1 4 16483 21949 1 2 4 16483 21950 1 3 4 16483 21951 1 4 5 16483 21952 1 5 5 16483 21953 0 6 5 16483 21954 0 0 5 16483 21955 0 1 5 16483 21956 0 2 5 16483 21957 0 3 5 end format %tdnn/dd/CCYY date
0 Response to Loops within weeklong period
Post a Comment