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   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: count differents values of a variable
Date   Wed, 13 Jul 2011 22:58:40 +0100

I think

... : gen new = _N

would also work.

Nick

On 13 Jul 2011, at 21:41, "Pablo.CARVALLO" <pablocepal@gmail.com> wrote:

Thanks a lot, Scott, your code works great. I just changed it for a panel data:

. tsset country date

and

.  bys country _spell: gen new = _seq[_N]

Best regards,

Pablo




2011/7/13 Scott Merryman <scott.merryman@gmail.com>:
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.



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