* Preparation
clear all
cd "[enter working directory]"
import excel "WDI_selected.xlsx", firstrow clear
drop TimeCode
rename (Time CountryName CountryCode) (year country code)
destring GDPpercapitaPPP-Lifeexpectancyatbirthmale, replace force
* Create a new frame
frame create cntry_codes
* Load data into a frame
frame cntry_codes: use "Country_classification", clear
** Combining datasets: merge vs frlink **
* Version 1: Merge
// Like before, you can simply merge the two datasets
*merge m:1 code using "Country_classification"
* Version 2: Link
// Especiall useful if you made changes to the second dataset which you don't want to store
// or if you are new to the data, the data is large and you are not sure which vars you need
frlink m:1 code, frame(cntry_codes) gen(link_1)
/*
First part as with the merge command, options describe which frame is linked
and how the link variable is named (non-mandatory, but probably useful).
If you are planning to link frames, read the manual on frlink to avoid some
mistakes. Here is an extraction of the most important points from the Stata
manual (Chapter D, p. 311 ff)
We start with the don’ts. There are only three:
Do not modify the contents of the link variable,
... but if you do, use frlink rebuild to fix it.
Do not rename the match variables in either frame,
... but if you do, drop the link variable, and use frlink m:1 or 1:1 to link
the frames again.
Do not drop the match variables from either frame,
... and if you do, we cannot help you.
And here are the do’s with qualification, which is always the same:
Type frlink rebuild afterward.
Do rebuild after adding observations in either or both frames.
Do rebuild after dropping observations in the linked frame.
Do rebuild after modifying the contents of the match variables in either
or both frames.
And remember a rule that always applies:
It is always safe to type frlink rebuild.
If there is no problem, it will do nothing.
If there is a problem, it will fix it unless it cannot,
... then it explains why and do nothing to your data.
*/
frget class, from(link_1) // Gets variable class from the linked dataset
assert country==cntry_name // Country names are not the same in both sets
br country cntry_name if country!=cntry_name // Different spelling
gen name_year = frval(link_1,name) + "_" + string(year) // Use frval to use variables from the linked frame in functions
drop link_1 // Deletes the link, transferred variables stay