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: Find the fiscal year for each obs
From
"Yu Chen, PhD" <[email protected]>
To
[email protected]
Subject
Re: st: Find the fiscal year for each obs
Date
Sun, 7 Apr 2013 11:05:27 -0500
Paul,
Thank you so very much for your codes. I truly appreciate it! I also
learned the programming skills.
Best,
Yu Chen
On Sun, Apr 7, 2013 at 5:44 AM, Seed, Paul <[email protected]> wrote:
>
> One option is to start by restructuring file B wide so that here is only
> one record per firm
> Then match on firm, and use a loop to set the fiscal year.
> The resulting file will have the same number of records as FileA, and
> relatively few extra variables.
> So the extra memory requirements (beyond those for reading fileA) are
> relatively low
>
>
> Some thing like this (code not checked):
> **************Start Stata code ***************
> use fileB, clear
> summ Fiscal, mean
> local b = r(min)
> local e = r(max)
> keep Begin End Firm Fiscal
> reshape wide Begin End , i(Firm) j(Fiscal)
> compress
> save fileB_wide, replace
>
> use fileA, clear
> compress
> merge m:1 Firm using fileB_wide
>
> gen Fiscal = .
> foreach fy of numlist `b'/`e' {
> replace Fiscal = `fy' if Date >= Begin`fy' & Date <= End`fy'
> drop Begin`fy' End`fy'
> }
>
>
> **************End Stata code ***************
>
> An alternative requiring even less core memory would be to split fileB
> into separate files for each fiscal year.
> Then merge each one in turn with file A, update Fiscal appropriately, drop
> the extra variables and
> repeat.
>
> Some thing like this (code again not checked):
> **************Start Stata code ***************
> use fileB, clear
> summ Fiscal, mean
> local b = r(min)
> local e = r(max)
> foreach fy of numlist `b'/`e' {
> preserve
> keep if Fiscal == `fy'
> keep Firm Begin End
> compress
> save fileB`fy'
> restore
> }
>
> use fileA, clear
> compress
> gen Fiscal = .
> foreach fy of numlist `b'/`e' {
> merge m:1 Firm using fileB`fy'
> replace Fiscal = `fy' if Date >= Begin & Date <= End
> drop Begin End _merge
> }
>
> **************End Stata code ***************
>
>
>
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/