hi all,

I am trying to figure out how to use -rangestat- for the following dataset example using -dataex-

For each observation, I would like to count the number of different atc_codes that are associated with drugclass=0 in a rolling 30 day window using dayzero as the key variable. I am not interested in including observations where drugclass==1 in this count (drugclass== 1 should not be contributing to the count), so if there are only drugclass==1 in the 30 day window, then the count would be 0. However, I do want to have the count of different atc_codes for every observation (both drugclass==1 and drugclass==0)

the command would look similar to this: rangestat (count) xyz,interval(dayzero -30 0) by (patid)-



Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double patid str8 atc_code float(dpd_match dpd_bzra_match) int dayzero float drugclass
400002181 "R05DA20" 1 0 1901 1
400002804 "N05CF01" 0 1 2556 0
400002804 "N05CF01" 0 1 2208 0
400002804 "N05CF01" 0 1 2067 0
400002804 "N05CF01" 0 1 2458 0
400002804 "N05CF01" 0 1 1843 0
400002804 "N05CF01" 0 1 1977 0
400002804 "N05CF01" 0 1 2303 0
400010442 "N02AJ13" 1 0 2175 1
400010442 "N02AJ13" 1 0 1908 1
400010442 "N02AJ13" 1 0 2183 1
400010442 "N02AJ17" 1 0 1908 1
400010442 "N05BA06" 0 1 2496 0
400040251 "N02AJ06" 1 0 2326 1
400040251 "N03AE01" 0 1 1857 0
400040251 "N03AE01" 0 1 2222 0
400040251 "N03AE01" 0 1 1994 0
400040251 "N03AE01" 0 1 1881 0
400040251 "N03AE01" 0 1 2029 0
400040251 "N03AE01" 0 1 1907 0
400040251 "N03AE01" 0 1 2446 0
400040251 "N03AE01" 0 1 2080 0
400040251 "N03AE01" 0 1 2412 0
400040251 "N03AE01" 0 1 2586 0
400040251 "N03AE01" 0 1 2493 0
400040251 "N03AE01" 0 1 1965 0
400040251 "N03AE01" 0 1 2164 0
400040251 "N03AE01" 0 1 2188 0
400040251 "N03AE01" 0 1 2327 0
400040251 "N03AE01" 0 1 2302 0
400040251 "N03AE01" 0 1 2041 0
400040251 "N03AE01" 0 1 2530 0
400040251 "N03AE01" 0 1 2266 0
400040251 "N03AE01" 0 1 2133 0
400040251 "N03AE01" 0 1 2109 0
400040251 "N03AE01" 0 1 2547 0
400040251 "N03AE01" 0 1 1928 0
400040251 "N03AE01" 0 1 2373 0
400040251 "N03AE01" 0 1 2352 0
400040251 "N03AE01" 0 1 2466 0
400040251 "N03AE01" 0 1 1836 0
400040251 "N03AE01" 0 1 2245 0
400040251 "N05CF01" 0 1 1944 0
400040251 "N05CF01" 0 1 2148 0
400040251 "N05CF01" 0 1 2448 0
400040251 "N05CF01" 0 1 2511 0
400040251 "N05CF01" 0 1 2091 0
400040251 "N05CF01" 0 1 2579 0
400040251 "N05CF01" 0 1 2312 0
400040251 "N05CF01" 0 1 1928 0
400040251 "N05CF01" 0 1 1857 0
400040251 "N05CF01" 0 1 2204 0
400040251 "N05CF01" 0 1 2008 0
400040251 "N05CF01" 0 1 2229 0
400040251 "N05CF01" 0 1 1836 0
400040251 "N05CF01" 0 1 1977 0
400040251 "N05CF01" 0 1 2398 0
400040251 "N05CF01" 0 1 2283 0
400040251 "N05CF01" 0 1 2118 0
400040251 "N05CF01" 0 1 2341 0
400040251 "N05CF01" 0 1 1907 0
400040251 "N05CF01" 0 1 2547 0
400040251 "N05CF01" 0 1 2062 0
400040251 "N05CF01" 0 1 2257 0
400040251 "N05CF01" 0 1 2039 0
400040251 "N05CF01" 0 1 2607 0
400040251 "N05CF01" 0 1 2478 0
400040251 "N05CF01" 0 1 2428 0
400040251 "N05CF01" 0 1 1892 0
400040251 "N05CF01" 0 1 2368 0
400040251 "N05CF01" 0 1 1881 0
400040251 "N05CF01" 0 1 2175 0
400041118 "N02AA03" 1 0 2252 1
400041118 "N02AJ06" 1 0 2294 1
400041118 "N05BA06" 0 1 2373 0
400041118 "N05BA06" 0 1 1921 0
400041118 "N05BA06" 0 1 2481 0
400041118 "N05BA06" 0 1 2350 0
400041118 "N05CF01" 0 1 2481 0
400041118 "N05CF01" 0 1 2593 0
400041118 "N05CF01" 0 1 2522 0
400041118 "N05CF01" 0 1 2308 0
400041118 "N05CF01" 0 1 2395 0
400041118 "N05CF01" 0 1 2472 0
400041118 "N05CF01" 0 1 2417 0
400041118 "N05CF01" 0 1 2571 0
400041118 "N05CF02" 0 1 2244 0
400041118 "N05CF02" 0 1 1970 0
400041118 "N05CF02" 0 1 2449 0
400041118 "N05CF02" 0 1 2003 0
400041118 "N05CF02" 0 1 2522 0
400041118 "N05CF02" 0 1 2287 0
400041118 "N05CF02" 0 1 2323 0
400041118 "N05CF02" 0 1 1945 0
end
label values drugclass drugclass1
label def drugclass1 0 "bzd", modify
label def drugclass1 1 "opioid", modify

any feedback would be greatly appreciated!

Thanks!