Home About Research Teaching Media CV Blog

Suppose you have data on year of birth, but you want to group several years together, e.g. group 1950, 1951 and 1952 births together; 1953, 1954, and 1955 together, etc.

Below is some JavaScript code I wrote to generate the relevant Stata commands without much fuss. You only have to make minor adjustments: Enter the start year (e.g. 1950), the end year (e.g. 1955), and the interval length (e.g. 3 years).

[sourcecode language=”javascript”]
<html><body><script>
//*** Generate Stata Code to replace cohort groups *****
//*** Enda Hargaden, Summer 2010
//*** Just replace the following three variables and refresh the page

var start_year = 1881;
var end_year = 1990;
var interval = 5;

//*** You’re done. Or at least you should be.

var c;
var a;
var backup1 = start_year;
var backup2 = end_year;
a = end_year – start_year;
a = a/interval;
a = Math.ceil(a);
a=a+1;

document.write("gen cohort_group = 0 <br />");
for(i=1;i<a;i++)
{
c=start_year+interval;
document.write("replace cohort_group = " + i + " if cohort > " + (start_year-1) + " & cohort < " + c + "<br />");

start_year = start_year+interval;

}

start_year = backup1;
end_year = backup2

document.write("<br />recode cohort_group ");
document.write("( 0 = 0 \"Other\" ) ///<br />");
for(j=1;j<a;j++)
{

if(j<(a-1))
{
c=start_year+interval-1;
document.write("( " + j + " = " + j + " \"" + (start_year) + " – " + c + "\" ) ///<br />");
start_year=start_year+interval;
}
if(j==a-1)
{
c=start_year+interval-1;
document.write("( " + j + " = " + j + " \"" + (start_year) + " – " + c + "\" ), gen(cohort_clean)<br />la var cohort_clean \"Birth Cohort\"<br />");
start_year=start_year+interval;
}
}
</script></body></html>
[/sourcecode]

I wrote this with a five-year interval in mind so I cannot guarantee you won’t run into an integer problem with the last entry, etc. However, it should get you most of the way there. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *