Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: st: How to count switches ?


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: How to count switches ?
Date   Sun, 27 Jun 2010 13:56:02 +0200

<> 


" When I do "expand 2, gen(id)"
I get the error "option gen() not allowed" (I'm have stata 10)."


Sorry about that! Indeed -expand- lacked the -gen()- option back then, but you can replicate its effect:



*************
vers 10

clear*
set obs 25
gen byte time=_n
expand 2
gen byte id=(_n>`=_N/2')+1
gen byte myvar=runiform()<.7
tsset id time
tsspell myvar
l, noo sepby(id) h(25)

bys id: gen byte tag=_n==_N
egen mytotal=total(tag*_spell)

di in r "Overall # of spells: " mytotal[_N]
*************





" Another tiny question. I have half a millon observation over 10 years. I
would like to know how many individuals i have in my data, that is  how
many unique id . How do i do that?"



Add this to the end of the code:

*************
egen mygroups=group(id)
su mygroups, mean
di in r "I have `r(max)' groups in my data"
*************

Also see NJC and coauthor`s http://www.stata.com/support/faqs/data/distinct.html


HTH
Martin

-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]] Im Auftrag von Ignacio Martinez
Gesendet: Sonntag, 27. Juni 2010 13:39
An: [email protected]
Betreff: RE: st: How to count switches ?

I'm almost there :-).

In your example I'm having a tiny problem. When I do "expand 2, gen(id)"
I get the error "option gen() not allowed" (I'm have stata 10).

Another tiny question. I have half a millon observation over 10 years. I
would like to know how many individuals i have in my data, that is  how
many unique id . How do i do that?

Thanks.

-Ignacio

On Sun, 2010-06-27 at 11:33 +0200, Martin Weiss wrote:
> <>
> 
> With a panelvar present, -ssc d tsspell- reports the _spell variable as within the panelvar. I have added two lines at the end to let you report the overall number:
> 
> ***********
> clear*
> set obs 25
> gen byte time=_n
> expand 2, gen(id)
> gen byte myvar=runiform()<.7
> tsset id time
> tsspell myvar
> l, noo sepby(id)
> 
> bys id: gen byte tag=_n==_N
> egen mytotal=total(tag*_spell)
> 
> di in r "Overall # of spells: " mytotal[_N]
> ***********
> 
> 
> HTH
> Martin
> 
> 
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Ignacio Martinez
> Sent: Sonntag, 27. Juni 2010 01:06
> To: [email protected]
> Subject: RE: st: How to count switches ?
> 
> Thanks!!
> Now I understand much better what is going on. Only one more question.
> 
> Your example is for  only one individual. I have a panel and what I want
> is the sum of the switches across individuals. So if individual A switch
> between employed and unemployed 2 times and individual B switch 3 times
> I want to get 5 as my result. How do you do that? If i do 
> 
> > su _spell, mean
> > di in r "# of spells: " r(max)
> 
> I get 3 instead of 5 :-( .
> 
> 
> Thanks for helping me.
> 
> -Ignacio
> 
> 
> On Sun, 2010-06-27 at 00:38 +0200, Martin Weiss wrote:
> > <>
> > 
> > So -ssc d tsspell- is user-written by Nick Cox.
> > 
> > 
> > Try a simple example, and the meaning becomes clear. Everytime your variable changes, _spell is stepped up. So its maximum should be what you want:
> > 
> > ***********
> > clear*
> > set obs 25
> > set seed 12543
> > gen byte time=_n
> > gen byte myvar=runiform()<.7
> > tsset time
> > tsspell myvar
> > l, noo sepby(_spell)
> > su _spell, mean
> > di in r "# of spells: " r(max)
> > ***********
> > 
> > 
> > BTW, no attachments on Statalist...
> > 
> > 
> > HTH
> > Martin
> > 
> > -----Original Message-----
> > From: [email protected] [mailto:[email protected]] On Behalf Of Ignacio Martinez
> > Sent: Sonntag, 27. Juni 2010 00:26
> > To: [email protected]
> > Subject: Re: st: How to count switches ?
> > 
> > Thanks a lot for the help. I need a little more help.
> > I installed tsspell. When I do "tsspell status" it generates 3 new
> > variables _spell, _seq, _end .
> > I'm confuse about how can i get the total number of switches in my data
> > set by using those variables.
> > 
> > Best,
> > 
> > -Ignacio
> > 
> > On Sat, 2010-06-26 at 15:25 -0400, Dmitriy Krichevskiy wrote:
> > > There is -tsspell- that does this. (-findit tsspell-).
> > > 
> > > On 6/26/10, Ignacio Martinez <[email protected]> wrote:
> > > > Hi I'm new in the list and I hope someone can help me.
> > > >
> > > > I would like to know how can I count switches in stata. I have the
> > > > following variables: Year, ID, Employment status
> > > > I want to generate a variable that ==1 if employment status in year t !=
> > > > to employment status in year t-1 for each individual.
> > > >
> > > > Thanks a lot for the help.
> > > >
> > > > -Ignacio
> > > >
> > > 
> > > 
> > 
> > 
> > 
> > *
> > *   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/
> 
> 
> *
> *   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/


*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index