# Re: st: Re: Count occurrence and duration of states in panel data

 From "Martin Weiss" To Subject Re: st: Re: Count occurrence and duration of states in panel data Date Mon, 1 Jun 2009 00:57:05 +0200

```<>

```
Sorry, I overlooked the -gen- command in your initial post, but the lowercase issue stands as it is...
```
```
Anyway, now that I have seen Nick`s tip on this http://www.stata-journal.com/article.html?article=pr0033
```I know where your code comes from.

```
Why is it that in the fifth line you want the result to be 1? The window should stretch from 70-30=40, so line 2 and line 4 contain a relevant event of being in state 2, don`t they? Anyway, here is your code:
```
**********
clear
input id begin end state
1 12 14 1
1 15 46 2
1 47 52 3
1 59 66 2
1 70 80 1
1 81 88 2
1 89 105 1
2 14 21 2
2 55 59 3
2 61 109 1
2 110 115 2
2 116 200 1
end

list, noobs sepby(id)

qui{
gen count=.
forv i=1/`=_N'{
cou if inrange(begin[`i']-end, 1,30) & ///
state==2 & ///
id == id[`i']
replace count=r(N) in `i'
}
}
list, noobs sepby(id)
**********

HTH
Martin
_______________________
```
----- Original Message ----- From: <go.diva@web.de>
```To: <statalist@hsphsun2.harvard.edu>
Sent: Monday, June 01, 2009 12:04 AM
Subject: Re: st: Re: Count occurrence and duration of states in panel data

```
Thanks for the up front comment but I defined count before the forval command, otherwise the -forv- will not work.
```
```
Do you have any suggestions regarding my question (which I summarize below for convenience)? Can I use inrange for my problem, i.e with begin and end date? How to write it such that it works? Sorry, if this questions sound too easy for you but I try hard since days.
```

clear
input Id begin end state s2 res, auto
1 12 14 1 . 0
1 15 46 2 1 0
1 47 52 3 . 1
1 59 66 2 1 1
1 70 80 1 . 1
1 81 88 2 1 1
1 89 105 1 . 2
2 14 21 2 1 0
2 55 59 3 . 1
2 61 109 1 . 0
2 110 115 2 1 0
2 116 200 1 . 1
end

gen count = .

local N = _N
qui forval i = 1/`N' {
egen tag = tag(s2) ///
if Id == Id[`i'] & ///
inrange(begin, begin[`i'] - 30, end)
count if tag == 1
replace count = r(N) in `i'
drop tag
}
l, sepby(Id)

I want to know how often a person has been in a certain state (e.g. state
2) during the past 30 days before the current state. Important is that the
current state should not count.

I have data in which the time intervals are not of equal length. And to
keep things for the moment simple I defined a variable s2 which takes on
the value 1 if the state of interest occurs. The variable res shows my
desired results.

The next step would be to calculate the duration the person has been in s2
during the past 30 days. If there is a related solution I would be
extremely happy if you let me know this as well.

-----Ursprüngliche Nachricht-----
Von: "Martin Weiss" <martin.weiss1@gmx.de>
Gesendet: 31.05.09 15:50:53
An: <statalist@hsphsun2.harvard.edu>
Betreff: st: Re: Count occurrence and duration of states in panel data

<>

Two observations up front:

You do not have to use -:mylabel, auto- here. I used it a while back
```
to -input- a string that should have labels attached automatically, but your
```vars are numeric.

In your -forv- you replace  the variable "count" which does not exist. You
also want to make "Id" lowercase for your code to stand a chance...

HTH
Martin
_______________________
```
----- Original Message ----- From: <go.diva@web.de>
```To: <statalist@hsphsun2.harvard.edu>
Sent: Sunday, May 31, 2009 3:07 PM
Subject: st: Count occurrence and duration of states in panel data

```
```I want to know how often a person has been in a certain state (e.g. state
2) during the past 30 days before the current state. Important is that the
current state should not count.

I have data in which the time intervals are not of equal length. And to
keep things for the moment simple I defined a variable s2 which takes on
the value 1 if the state of interest occurs. The variable res shows my
desired results.

clear
input id begin end state s2 res :mylabel , auto
1 12 14 1 . 0
1 15 46 2 1 0
1 47 52 3 . 1
1 59 66 2 1 1
1 70 80 1 . 1
1 81 88 2 1 1
1 89 105 1 . 2
2 14 21 2 1 0
2 55 59 3 . 1
2 61 109 1 . 0
2 110 115 2 1 0
2 116 200 1 . 1
end

I followed some earlier questions at statalist and stata tips 39, 51 on
inrange and events in intervals:

gen count = .

local N = _N
qui forval i = 1/`N' {
egen tag = tag(s2) ///
if Id == Id[`i'] & ///
inrange(begin, begin[`i'] - 30, end)
count if tag == 1
replace count = r(N) in `i'
drop tag
}
l, sepby(Id)

I surmise that the inrange command is not appropriate. As s2 should also
count if even the begin date has been more than 30 days away but the
```
person is in the state within the 30 day interval (with respect to the end
```date),

```
Can inrange be adjusted such that it takes the begin date and the end date
```into account? Do I have to think about an entirely different approach?

```
The next step would be to calculate the duration the person has been in s2
```during the past 30 days. If there is a related solution I would be
extremely happy if you let me know this as well.

Any ideas will be appreciated!
Elke
____________________________________________________________
Text: GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

____________________________________________________________
Text: GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```