{"id":794,"date":"2011-08-22T00:45:43","date_gmt":"2011-08-22T04:45:43","guid":{"rendered":"http:\/\/hargaden.com\/enda\/?p=54"},"modified":"2011-08-22T00:45:43","modified_gmt":"2011-08-22T04:45:43","slug":"cohort-grouping-script","status":"publish","type":"post","link":"https:\/\/hargaden.com\/enda\/cohort-grouping-script\/","title":{"rendered":"Cohort grouping script"},"content":{"rendered":"<p>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.<\/p>\n<p>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).<\/p>\n<p>[sourcecode language=&#8221;javascript&#8221;]<br \/>\n&lt;html&gt;&lt;body&gt;&lt;script&gt;<br \/>\n\/\/*** Generate Stata Code to replace cohort groups *****<br \/>\n\/\/*** Enda Hargaden, Summer 2010<br \/>\n\/\/*** Just replace the following three variables and refresh the page<\/p>\n<p>var start_year = 1881;<br \/>\nvar end_year = 1990;<br \/>\nvar interval = 5;<\/p>\n<p>\/\/*** You&#8217;re done. Or at least you should be.<\/p>\n<p>var c;<br \/>\nvar a;<br \/>\nvar backup1 = start_year;<br \/>\nvar backup2 = end_year;<br \/>\na = end_year &#8211; start_year;<br \/>\na = a\/interval;<br \/>\na = Math.ceil(a);<br \/>\na=a+1;<\/p>\n<p>document.write(&quot;gen cohort_group = 0 &lt;br \/&gt;&quot;);<br \/>\nfor(i=1;i&lt;a;i++)<br \/>\n{<br \/>\nc=start_year+interval;<br \/>\n   document.write(&quot;replace cohort_group = &quot; + i + &quot; if cohort &gt; &quot; + (start_year-1) + &quot; &amp; cohort &lt; &quot; + c + &quot;&lt;br \/&gt;&quot;);<\/p>\n<p>start_year = start_year+interval;<\/p>\n<p>}<\/p>\n<p>start_year = backup1;<br \/>\nend_year = backup2<\/p>\n<p>document.write(&quot;&lt;br \/&gt;recode cohort_group &quot;);<br \/>\ndocument.write(&quot;( 0 = 0 \\&quot;Other\\&quot; ) \/\/\/&lt;br \/&gt;&quot;);<br \/>\nfor(j=1;j&lt;a;j++)<br \/>\n{<\/p>\n<p>\tif(j&lt;(a-1))<br \/>\n\t{<br \/>\n\tc=start_year+interval-1;<br \/>\n\tdocument.write(&quot;( &quot; + j + &quot; = &quot; + j + &quot; \\&quot;&quot; + (start_year) + &quot; &#8211; &quot; + c + &quot;\\&quot; ) \/\/\/&lt;br \/&gt;&quot;);<br \/>\n\tstart_year=start_year+interval;<br \/>\n\t}<br \/>\n\tif(j==a-1)<br \/>\n\t{<br \/>\n\tc=start_year+interval-1;<br \/>\n\tdocument.write(&quot;( &quot; + j + &quot; = &quot; + j + &quot; \\&quot;&quot; + (start_year) + &quot; &#8211; &quot; + c + &quot;\\&quot; ), gen(cohort_clean)&lt;br \/&gt;la var cohort_clean \\&quot;Birth Cohort\\&quot;&lt;br \/&gt;&quot;);<br \/>\n\tstart_year=start_year+interval;<br \/>\n\t}<br \/>\n}<br \/>\n&lt;\/script&gt;&lt;\/body&gt;&lt;\/html&gt;<br \/>\n[\/sourcecode]<\/p>\n<p>I wrote this with a five-year interval in mind so I cannot guarantee you won&#8217;t run into an integer problem with the last entry, etc. However, it should get you most of the way there.\u00a0Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-794","post","type-post","status-publish","format-standard","hentry","category-stata"],"_links":{"self":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts\/794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/comments?post=794"}],"version-history":[{"count":0,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts\/794\/revisions"}],"wp:attachment":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/media?parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/categories?post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/tags?post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}