How about this:
sort ID Period
by ID: gen switch_period = 1 if _n==1
by ID: gen switch_period = switch_period[_n-1] + switch
by ID switch_period: gen Duration = _N
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Albert Newton
Sent: Thursday, April 06, 2006 1:42 PM
To: [email protected]
Subject: Re: st: RE: How to construct a duration variable?
Thank you, Nick.
For example,  there are three brands in the market: A, B and C.  One
consumer buy A at the first period.  At the second period, she
switches to B.  Since then, she keeps buying B for the next four
periods.  After that, she switches to C etc.
Then the duration variable I want should be:
hhid    Period   Brand    Switch  Duration
1         1         A           0           1
1         2         B           1           4
1         3         B           0           4
1         4         B           0           4
1         5         B           0           4
1         6         C           1           2
1         7         C           0           2
1         8         A           1           1
1         9         B           1           4
1        10         B           0           4
1        11         B           0           4
1        12         B           0           4
1        13         C           1           2
1        14         C           0           2
I wish that it's clear this time.  I just not sure how to construct
the duration variable in Stata.
Thanks,
Albet
On 4/6/06, Nick Cox <[email protected]> wrote:
> I think you are going to need to explain how "Duration"
> is calculated. The numerical example is not
> clear to me.
>
> Nick
> [email protected]
>
> Albert Newton
>
> >        I get stuck in coding a during variable.  The data is like:
> >
> > hhid    weekshoppingday triptype        switch  switchtype
> >   Duration
> > 1871877 954     7       1       0       0       0
> > 1871877 955     7       2       1       2       1
> > 1871877 956     3       1       1       4       1
> > 1871877 957     6       0       1       1       1
> > 1871877 958     7       1       1       6       3
> > 1871877 959     7       1       0       0       0
> > 1871877 960     7       1       0       0       0
> > 1871877 961     7       0       1       1       1
> > 1871877 962     7       2       1       5       1
> > 1871877 963     4       1       1       4       3
> > 1871877 964     2       1       0       0       0
> > 1871877 964     7       1       0       0       0
> >
> > I just wanto count how long the customer stay in the switched
shopping
> > type.  Could any one give some suggestion of how to constuct the
> > variable "Duration"?
> > The one above is make manually.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
This message is intended solely for the designated recipient(s). It may contain confidential or proprietary information and may be subject to confidentiality protections. If you are not a designated recipient, you may not review, copy, or distribute this message. If you receive this in error, please notify the sender by reply e-mail and delete this message. 
Arrowstreet Capital, L.P. is an Equal Opportunity Employer. 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/