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]

Re: st: count differents values of a variable


From   Scott Merryman <scott.merryman@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: count differents values of a variable
Date   Wed, 13 Jul 2011 14:58:29 -0500

You can use Nick's -tsspell- (use -findit tsspell- to locate and download):

clear
input str6 quarter    str3 country    str1 var1
1996q2    arg        D
1996q3    arg        D
1996q4    arg        D
1997q1    arg        A
1997q2    arg        A
1997q3    arg        A
1997q4    arg        A
1998q1    arg        D
1998q2    arg        A
2002q4    arg        A
2003q1    arg        D
2003q2    arg        D
2003q3    arg        D
2003q4    arg        D
2004q1    arg        A
2004q2    arg        D
end

gen date = quarterly(quarter , "YQ")
format date %tq
tsset date
encode var1, gen(v1)
tsspell v1
bys _spell: gen new = _seq[_N]
replace new = . if _seq>1
drop _*
l

Scott



On Wed, Jul 13, 2011 at 1:42 PM, Pablo.CARVALLO <pablocepal@gmail.com> wrote:
> Dear List,
>
> I have the following dataset:
>
> quarter    country    var1
> 1996q2    arg        D
> 1996q3    arg        D
> 1996q4    arg        D
> 1997q1    arg        A
> 1997q2    arg        A
> 1997q3    arg        A
> 1997q4    arg        A
> 1998q1    arg        D
> 1998q2    arg        A
> 2002q4    arg        A
> 2003q1    arg        D
> 2003q2    arg        D
> 2003q3    arg        D
> 2003q4    arg        D
> 2004q1    arg        A
> 2004q2    arg        D
>
> I need a new variable to know when a new value of var1 starts and how
> many quarters this value repeats.
>
> For example, there are 3 "D" from 1996q2-1996q4, so that newvar=3.
> From 1997q1 to 1997q4 there are 4 "A", so that newvar=4.
>
> My dataset should be like this:
>
> quarter   country   var1    newvar
> 1996q2    arg        D       3
> 1996q3    arg        D       .
> 1996q4    arg        D       .
> 1997q1    arg        A       4
> 1997q2    arg        A       .
> 1997q3    arg        A       .
> 1997q4    arg        A       .
> 1998q1    arg        D       1
> 1998q2    arg        A       2
> 2002q4    arg        A       .
> 2003q1    arg        D       4
> 2003q2    arg        D       .
> 2003q3    arg        D       .
> 2003q4    arg        D       .
> 2004q1    arg        A       1
> 2004q2    arg        D       1
>
> Thanks a lot.
>
>
> Carvallo
> *
> *   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