clear set more off #delimit; /************************************************************************************************************* This do-file reads in flow of funds data. The historical prn files are available at: http://www.federalreserve.gov/releases/z1/Current/data.htm Layout of file: 1. Files to be read 2. Read files and merge 3. Label variables 4. Clean and save *************************************************************************************************************/ do paths; tempfile data; local vintage; *If left blank, will read in current vintage. Otherwise, must be CCYYMMDD format for a day of the FoF release, i.e. 20110916.; /************************************************************************************************************* 1. Files to be read *************************************************************************************************************/ foreach x in a b g l s u {; if `"`vintage'"'==`""' {; copy "http://www.federalreserve.gov/releases/z1/Current/Disk/`x'tabs.zip" "`x'tabs.zip", replace; }; else {; copy "http://www.federalreserve.gov/releases/z1/`vintage'/Disk/`x'tabs.zip" "`x'tabs.zip", replace; }; qui unzipfile "`x'tabs.zip", replace; qui erase `x'tabs.zip; }; local tabs: dir `""' files `"*.prn"'; /************************************************************************************************************* 2. Read files and merge *************************************************************************************************************/ qui gen DATES=.; qui save `data', replace; foreach tab of local tabs {; if inlist("`tab'","ltab231d") | regexm(`"`tab'"',`"mtrx"') {; *ltab231d.prn appears to have a coding mistake such that it has too many variables and wraps. I have written to the FoF; qui erase "`tab'"; continue; }; local date_reformatted; di `"`tab'"'; qui insheet using "`tab'", delimiter(" ") clear double names case; qui erase "`tab'"; capture qui drop v*; *Drops variables that appear in multiple tables; foreach var of varlist * {; if `"`var'"'==`"DATES"' {; continue; }; capture qui destring `var', replace ignore("NA"); label variable `var' `=regexr(`"`tab'"',`"\.prn$"',`""')'; local tabtype: variable label `var'; local tabtype: piece 1 2 of "`tab'"; qui replace `var' = `var' * 10^(-3); *Converts variables into billions. Note that all tables are now in millions of dollars; if regexm(`"`var'"',`"A$"') & `"`date_reformatted'"'==`""' {; *Annual variables; qui tostring DATES, replace; qui replace DATES = DATES + "04"; qui destring DATES, replace; local date_reformatted yes; }; }; qui merge 1:1 DATES using `data', nogenerate noreport; qui save `data', replace; }; /************************************************************************************************************* 3. Clean and save *************************************************************************************************************/ qui tostring DATES, replace; qui gen year = substr(DATES,1,4); qui gen quarter = substr(DATES,5,2); qui destring year quarter, force replace; qui gen quarterly = yq(year,quarter); qui tsset quarterly, quarterly; qui format quarterly %tq; qui format F* %16.0fc; capture aorder; move quarterly DATES; qui save "$SavePath\FoF`vintage'", replace;