# Thread: Creating a dummy with certain conditions

1. ## Creating a dummy with certain conditions

Hello, I am a beginner user of Stata.

I am trying to create a dummy in a panel data when certain company did have bond rated over the sample period.

ex)

A000020 2003 1
A000020 2004 1
A000020 2005 1
A000020 2006 1
A000020 2007 1
A000020 2008 1
A000020 2009 1
A000020 2010 1
A000020 2011 0
A000040 2003 0
A000040 2004 0
A000040 2005 0
A000040 2006 0
A000040 2007 0
A000040 2008 0
A000040 2009 0
A000040 2010 0
A000040 2011 0

In this example, company A000040 never had its bond rated over 2003 to 2011, but company A000020 had its bond rated most of years.

Because company A000040 does not have a single bond rating, I want to assign value 0 to new variable "bond rating" for all years. Regarding company A000020, becuase it had its bond rated at least one time(in this case, most of years) over the sample period, I want to assign value 1 to new variable "bond rating" for all sample years(including years it did not have its bond rated).

I solved this problem in excel, but it required me several steps and I had to go through these steps over and over again as the sample changes.

As I am beginning to learn Stata, any input will be of great help to me.

Thank you for your consideration and time.

2. ## Re: Creating a dummy with certain conditions

See
help egen

The command could be like:

bysort Symbol: egen dummy = max(Had_bond_rated)

3. ## Re: Creating a dummy with certain conditions

Originally Posted by wangwang
See
help egen

The command could be like:

bysort Symbol: egen dummy = max(Had_bond_rated)

I would like to ask one more question regarding genearting dummy.

I wanted to extract firms with top 30% of asset size and bottom 30% annualy over the sample period. Below is the code I made

_pctile asset if year==2003, nq(10)
gen d_uncons_asset2=0 if asset<=r(r3) & year==2003 & asset!=.
_pctile asset if year==2004, nq(10)
replace d_uncons_asset2=0 if asset<=r(r3) & year==2004 & asset!=.
_pctile asset if year==2005, nq(10)
replace d_uncons_asset2=0 if asset<=r(r3) & year==2005 & asset!=.
_pctile asset if year==2006, nq(10)
replace d_uncons_asset2=0 if asset<=r(r3) & year==2006 & asset!=.
_pctile asset if year==2007, nq(10)
replace d_uncons_asset2=0 if asset<=r(r3) & year==2007 & asset!=.
_pctile asset if year==2008, nq(10)
replace d_uncons_asset2=0 if asset<=r(r3) & year==2008 & asset!=.
_pctile asset if year==2009, nq(10)
replace d_uncons_asset2=0 if asset<=r(r3) & year==2009 & asset!=.
_pctile asset if year==2010, nq(10)
replace d_uncons_asset2=0 if asset<=r(r3) & year==2010 & asset!=.
_pctile asset if year==2011, nq(10)
replace d_uncons_asset2=0 if asset<=r(r3) & year==2011 & asset!=.

_pctile asset if year==2003, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2003 & asset!=.
_pctile asset if year==2004, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2004 & asset!=.
_pctile asset if year==2005, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2005 & asset!=.
_pctile asset if year==2006, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2006 & asset!=.
_pctile asset if year==2007, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2007 & asset!=.
_pctile asset if year==2008, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2008 & asset!=.
_pctile asset if year==2009, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2009 & asset!=.
_pctile asset if year==2010, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2010 & asset!=.
_pctile asset if year==2011, nq(10)
replace d_uncons_asset2=1 if asset>=r(r7) & year==2011 & asset!=.

As you can see, even though this code works, I had to repete the same code and change the year from 2003 to 2011. Is'nt there any better way?

Thank you very much for your consideration and help.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts