Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Check existence within a group


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Check existence within a group
Date   Fri, 7 May 2004 15:53:46 +0100

Your guess is right. You don't need a loop at all. 
Stata is very good at doing this kind of thing 
without loops. 

Your variable -newmkttype- is coded 0 for new, 1 for old. 
Not the way that I would do it, but that's not important. 

You can get it from 

bysort borrower mkttype (issuedate) : gen newmkttype = _n > 1 

That is we sort first on -borrower-, within -borrower- on 
-mkttype-, and within -mkttype- on -issuedate-. Then you want 
the first occurrence within blocks of -borrower mkttype- 
to be flagged as new, by coding as 0, and later occurrences as
not new, and so coded 1. This is achieved by 

_n > 1 

which evaluates as 0 if _n == 1 and 1 otherwise. 

de la Garza, Adrian
 
> I have a database that contains info on bonds issued as follows:
> 
> borrower	issuedate	mtydate	amount	mkttype
> ABN Amro	04Jun93	03Jun98	50		E
> ABN Amro	19Sep94	17Jul96	100		E
> ABN Amro	22Nov94	21Nov98	50		S
> ...
> ACINDAR	02Feb92	30Jan99	200		L
> ACINDAR	20Mar92	19Mar97	50		E
> ACINDAR	29Jul94	28Mar00	175		L
> ...
> 
> and I want to create a variable -newmkttype- that tells me if the
> borrower is issuing a bond in a completely new and unknown 
> market type.
> If it is, then I want this variable to take the value of 0. If, on the
> contrary, the borrower has issued bonds in this market type 
> in the past,
> then I want this variable to take the value of 1 (notice that the
> database is sorted by borrower issuedate mtydate amount).
> 
> borrower	issuedate	mtydate	amount	mkttype	newmkttype
> ABN Amro	04Jun93	03Jun98	50		E		0
> ABN Amro	19Sep94	17Jul96	100		E		1
> ABN Amro	22Nov94	21Nov98	50		S		0
> ...
> ACINDAR	02Feb92	30Jan99	200		L		0
> ACINDAR	20Mar92	19Mar97	50		E		0
> ACINDAR	29Jul94	28Mar00	175		L		1
> ...
> 
> How can I construct this variable? Is there an easy way of 
> checking the
> borrower's history to determine whether -newmkttype- should 
> get a 0 or a
> 1, without writing a complicated loop?

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index