Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: How to generate dummy for the following years


From   Ronnie Babigumira <rb.glists@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: How to generate dummy for the following years
Date   Fri, 27 Jul 2012 13:25:39 +0300

Does this do what you want? 

bysort firm_id (year) : gen starter = export[_n] == 1 & export[_n-1] == 0
list, sepby(firm_id)




-- 
010100100110111101101110011011100110100101100101




On Friday, July 27, 2012 at 1:14 PM, umut senalp wrote:

> Dear Ronnie,
> 
> Thank you for your reply, as you said, the code you provided has generated "always_exporter" successfully. 
> 
> Regarding to your latest reply: 
> 
> generate starter == .
> replace starter == 1 if export2003 == 0 & export2004 == 1 
> 
> I do not think that I will be able to capture all "starters" with this way practically, since some firms start to export 2005 some of them 2007 etc. So I need to write a code to find the first export dummy taking the value of "1" for each firm, it will show me that the firm switch from 0 to 1 first time (means it start to export). I tried to show what I am trying to do with an example below. Thanks again,
> 
> Best
> 
> Umut
> firm id year export starter
> > 1 2003 0 0
> > 1 2004 0 0 
> > 1 2005 0 0
> > 1 2006 1 1
> > 1 2007 0 0
> > 1 2008 1 0
> > 1 2009 1 0
> > 1 2010 1 0
> > 1 2011 1 0
> > 2 2003 0 0
> > 2 2004 0 0
> > 2 2005 0 0
> > 2 2006 0 0
> > 2 2007 1 1
> > 2 2008 0 0
> > 2 2009 0 0
> > 2 2010 0 0
> > 3 2003 0 0 
> > 3 2004 1 1
> > 3 2005 1 0 
> > 3 2006 0 0
> > 3 2007 0 0
> > 3 2008 0 0
> > 3 2009 0 0 
> > 3 2010 0 0
> > 3 2011 1 0 
> > 4 2010 0 0
> > 4 2011 1 1 
> 
> 
> 
> 
> ----------------------------------------
> > Date: Fri, 27 Jul 2012 08:42:03 +0300
> > From: rb.glists@gmail.com (mailto:rb.glists@gmail.com)
> > To: statalist@hsphsun2.harvard.edu (mailto:statalist@hsphsun2.harvard.edu)
> > Subject: Re: st: How to generate dummy for the following years
> > 
> > Umut
> > Reading through your email again, I don't think I answered your question so the only useful part of my earlier response may be how to generate always_exporter.
> > 
> > Ps: For what you were trying to do with
> > 
> > **
> > gen starter=0
> > replace starter=1 if export==0 in 2003 & export==1 in 2004
> > 
> > ***
> > 
> > I am wondering if what you are looking for is a -reshape-, so
> > 
> > reshape wide export, i(firm_id) j(year)
> > 
> > This would give you
> > 
> > firm_id export2003 export2004 export2005 export2006 export2007 export2008 export2009 export2010 export2011
> > 00028073 1 1 0 1 0 1 1 1 1
> > 00036721 0 0 0 0 0 0 0 0 .
> > 00047152 1 1 1 0 0 0 0 0 1
> > 00063351 . . . . . . . 1 1
> > 00144277 . . . . 1 1 0 1 .
> > 00144614 . . . . . . 0 0 .
> > 00145362 1 1 1 . . . . . .
> > 00147034 1 1 1 1 1 1 1 1 .
> > 
> > You can then
> > generate starter == .
> > replace starter == 1 if export2003 == 0 & export2004 == 1
> > 
> > Or, to create a dummy, you can
> > gen starter = export2003 == 0 & export2004 == 1
> > 
> > Ronnie
> > --
> > 
> > 
> > 010100100110111101101110011011100110100101100101
> > 
> > 
> > 
> > 
> > On Friday, July 27, 2012 at 12:28 AM, Ronnie Babigumira wrote:
> > 
> > > Umut,
> > > Regarding your original question on generating dummies. There will be more direct ways but here is one way to do it.
> > > 
> > > clear
> > > input str8 firm_id year export
> > > 00028073 2003 1
> > > 00028073 2004 1
> > > 00028073 2005 0
> > > 00028073 2006 1
> > > 00028073 2007 0
> > > 00028073 2008 1
> > > 00028073 2009 1
> > > 00028073 2010 1
> > > 00028073 2011 1
> > > 00036721 2003 0
> > > 00036721 2004 0
> > > 00036721 2005 0
> > > 00036721 2006 0
> > > 00036721 2007 0
> > > 00036721 2008 0
> > > 00036721 2009 0
> > > 00036721 2010 0
> > > 00047152 2003 1
> > > 00047152 2004 1
> > > 00047152 2005 1
> > > 00047152 2006 0
> > > 00047152 2007 0
> > > 00047152 2008 0
> > > 00047152 2009 0
> > > 00047152 2010 0
> > > 00047152 2011 1
> > > 00063351 2010 1
> > > 00063351 2011 1
> > > 00144277 2007 1
> > > 00144277 2008 1
> > > 00144277 2009 0
> > > 00144277 2010 1
> > > 00144614 2009 0
> > > 00144614 2010 0
> > > 00145362 2003 1
> > > 00145362 2004 1
> > > 00145362 2005 1
> > > 00147034 2003 1
> > > 00147034 2004 1
> > > 00147034 2005 1
> > > 00147034 2006 1
> > > 00147034 2007 1
> > > 00147034 2008 1
> > > 00147034 2009 1
> > > 00147034 2010 1
> > > end
> > > 
> > > * Always exporter
> > > tempvar foo
> > > bys firm_id : egen `foo' = mean(export)
> > > gen always_exporter = `foo' == 1
> > > 
> > > * Start and end data for exporters
> > > * Split data
> > > tempfile fdata nedata
> > > save `fdata'
> > > drop if export == 1
> > > save `nedata'
> > > use `fdata'
> > > keep if export == 1
> > > 
> > > bysort firm_id (year): gen start= year[1]
> > > bysort firm_id (year): gen end= year[_N]
> > > 
> > > * Append non-export data and fillin missing data
> > > append using `nedata'
> > > bys firm_id (start): replace start = start[_n-1] if start >= .
> > > bys firm_id (end): replace end = end[_n-1] if end >= .
> > > 
> > > sort firm_id year
> > > list, sepby(firm_id)
> > > 
> > > 
> > > 
> > > Hope this is helpful
> > > 
> > > Ronnie
> > > 
> > > --
> > > 010100100110111101101110011011100110100101100101
> > > 
> > > 
> > > 
> > > 
> > > On Thursday, July 26, 2012 at 6:07 PM, umut senalp wrote:
> > > 
> > > > Dear Stata Listers,
> > > > 
> > > > I am using StataSE 12. I have an unbalanced data for over 2000 firms (covering the period 2003-2011) and I have a dummy variable for the firms' export status (Export=1 if firm have positive export value that year). While, a few firms observed for all 9 years (2003-2011), majority of them observed for minimum 2 years consecutively.
> > > > I want to create a new set of dummy variables indicating the year that a firm started to export (Starter),
> > > > one year before starting to export (Year_before),and for the following two years that the firm started to export which are Year_After and Two_Years_After.
> > > > So, I want to generate 4 new dummies based on the firm's Export status. In addition to that, I have some firms which are exporter for the all period that they are observable (means I do not know when they started to export), and I want to name them as Always_Exporter.
> > > > 
> > > > Since I am not so familiar with programming in Stata I tried following command but it failed, it was just for the firms that I have observation for 2003 onwards, anyway.
> > > > gen starter=0
> > > > replace starter=1 if export==0 in 2003 & export==1 in 2004
> > > > 
> > > > And I realised that this approach will not work since I need to do it for many times for following years to catch all starters.
> > > > 
> > > > Well, I think I need to use a program that includes loop but I don't know where to start to write the code. I would be glad if you could help me on this issue. I hope I've the issue clear enough.
> > > > 
> > > > Kind regards
> > > > 
> > > > Umut SENALP
> > > > *
> > > > * 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index