Showing posts with label append. Show all posts
Showing posts with label append. Show all posts

Sep 27, 2015

Appending all files in a directory

cd "E:\eu-lfs\converted files"

local filelist : dir . files "*_y.dta"  // Create local wit all filenames ending with "_y.dta
di `filelist'

local first : word 1 of `filelist'      // Identify first file
di "`first'"

local total_ : word count `filelist' // Identify total number of files
di `total_'

use "`first'", clear                    

forvalues x = 2/`total_' {
    di `x'
    local y : word `x' of `filelist'
    append using "`y'", force
}

save eulfs, replace


Jul 2, 2014

Random graphs (24): Small-multiples and overlayed line plots


clear

// SG.GEN.PARL.ZS - Proportion of seats held by women in national parliaments (%)
wbopendata, indicator(SG.GEN.PARL.ZS) clear long

// Keep European countries
keep if regioncode == "ECS" // "Europe & Central Asia (all income levels)"

// Keep EU-28
keep if inlist(iso2code, "AT", "BE", "BG", "CY", "CZ", "DE", "DK", "EE", "ES") | ///
        inlist(iso2code, "FI", "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LI") | ///
  inlist(iso2code, "LT", "LU", "LV", "NL", "PL", "PT", "RO", "SE", "SI") | ///
  inlist(iso2code, "SK") // EU-28

// Identify and rename salient variables
ren sg_gen_parl_zs women
label var women "% of national parliament seats held by women"
ren iso2code cntry
drop countrycode region regioncode

// Reshape to drop empty observations
reshape wide women, i(cntry) j(year)
dropmiss women1960-women2014, force
reshape long

// Calculate EU-28 mean
preserve
collapse (mean) women, by(year)
gen cntry = "{bf:EU-28}"
tempfile eu28
save `eu28', replace
restore
append using `eu28'

// Plot small-multiples figure twoway scatter women year /// , by(cntry /// , note(" ") /// caption("{it:Source:} World Development Indicators, date of extraction: 2015-05-25", span size(small))) /// cmissing(no) /// connect(l) /// msymbol(o) /// xtitle("") xlabel(1990 1997 2000 2005 2010 2014, ang(45)) /// ylabel(0 (10) 50) /// name(small_multiples, replace)
// Plot overlayed figure encode cntry, gen(country) // No strings for -xtline- xtset country year, yearly // Declare panel xtline women if cntry != "{bf:EU-28}" /// , overlay /// xlabel(1990 1997 2000 2005 2010 2014) xtitle("Year") /// plot1opts(lpattern(dash)) plot2opts(lpattern(dash)) /// plot3opts(lpattern(dash)) plot4opts(lpattern(dash)) /// plot5opts(lpattern(dash)) plot6opts(lpattern(dash)) /// plot7opts(lpattern(dash)) plot8opts(lpattern(dash)) /// plot9opts(lpattern(dash)) plot10opts(lpattern(dash)) /// plot11opts(lpattern(dash)) plot12opts(lpattern(dash)) /// plot13opts(lpattern(dash)) plot14opts(lpattern(dash)) /// plot15opts(lpattern(dash)) plot16opts(lpattern(dash)) /// plot17opts(lpattern(dash)) plot18opts(lpattern(dash)) /// plot19opts(lpattern(dash)) plot20opts(lpattern(dash)) /// plot21opts(lpattern(dash)) plot22opts(lpattern(dash)) /// plot23opts(lpattern(dash)) plot24opts(lpattern(dash)) /// plot25opts(lpattern(dash)) plot26opts(lpattern(dash)) /// plot27opts(lpattern(dash)) plot28opts(lpattern(dash)) /// name(overlay, replace) /// addplot(line women year if cntry == "{bf:EU-28}", /// lwidth(thick) lpattern(solid) sort(cntry year)) /// legend(order(29 "EU-28 (unweighted average)") pos(6) ring(0)) /// caption("{it:Source:} World Development Indicators, date of extraction: 2015-05-25", span size(small)) /// ytitle("% of national parliament seats" "held by women, EU-28 countries")

Aug 30, 2012

-merge- crib sheet

Since Stata 11, -merge- comes with a more precise syntax, distinguishing between different types of matching. The general command structure is as follows:
use master
merge type idvars using using, options 

The so-called "master" file is being matched with the "using" file based on the list of ID variable(s) "idvars". "Type" distinguishes between four types of merging:
  • One-to-one:
    When using -merge 1:1-, Stata merges one observation in the "master" file to the corresponding observation in the "using" file.
    (However, cases that could be matched will nonetheless be included in the merged file.In order to prevent that, the option -assert(match)- has to be added to the command. However, if ID's are not unique, -merge 1:1- will produce an error message.)
  • Many-to-one:
    When using -merge m:1-, Stata merges many observations from the "master" data set to one corresponding obervation in the "using" file. An example for this would be to have individual-level data in the "master" file and country- or household-level information in the "using" file.
  • One-to-many:
    -merge 1:m- is just the reverse of many-to-one; e.g. country-level information in master file is matched to respondent information comprising the "using" file.
  • Many-to-many:
    According to the Stata Data-Management Reference Manual [D], -merge m:m- "is allowed for completeness, but it is difficult to imagine an example of when it would be useful. Use of -merge m:m- is not encouraged."

 

Troubleshooting merges

Still have syntax with the old -merge- command? See the old help file here.

William Gould has the following suggestions for merges gone bad:
  • Check whether the ID variable is stored properly, e.g. if it's a long number it might not be sufficient to store it as a float. Stata might start rounding the long numbers if they are too long for the storage type.
  • Check the uniqueness of ID's in both files to be merged:
  • by id, sort: assert _N == 1
  • Merge on all common variables: If you have doubts about your ID variable, add another variable that should be constant within units, for instance gender.