[EpiData-list] Howto: Group a date variable by calendar year

epidata-list at lists.umanitoba.ca epidata-list at lists.umanitoba.ca
Wed Dec 14 14:44:06 CST 2005


* Example pgm file to group a date variable V1
* created by: Jens Lauritsen, EpiData Association

set show info=off
generate 50               // generate 50 empty observations for testing
define v1 <dd/mm/yyyy>
define y  ####
y = 1900 + recnumber      // could also be 1900 + _n
v1 = dmy(1,7,y)

* so now we have a date variable with dates from 1900 to 1950
* three different strategies to group.
* Strategy 1: extract year from the date and recode to a string:
define y1 ####
define ys ________________________
y1 = year(v1)
recode y1 to ys by 10
tab ys

* Strategy 2: round the number into 10 year groups
define y2 ####
y2 = year(v1)
y2 = trunc(y2/10)*10      // creates groups by 10 years
tab y2

* If you use 5 instead of 10 you get 5 year groups
* y2 = trunc(y2/5)*5      // creates groups by 5 years

* strategy 3: split on specific dates:
define y3 #
let y3 = 0
if (v1 < dmy(1,10,1930) ) then y3 = 1
if (v1 >=  dmy(1,10,1930)) and (v1 < dmy(1,10,1939) ) then y3 = 2
if (v1 >=  dmy(1,10,1939)) and (v1 < dmy(1,10,1960) ) then y3 = 3
tab y3

* run the above to get the tabulations.
----------------------------------------------------------------------------------
Output:
E.g. for strategy 1:
No.    %    Cum %
1900 - 1909    9    18.00    18.00
1910 - 1919    10    20.00    38.00
1920 - 1929    10    20.00    58.00
1930 - 1939    10    20.00    78.00
1940 - 1949    10    20.00    98.00
1950 - 1959    1    2.00    100.00
          Total    50    100%     

* strategy 2:
           No.    %    Cum %
1900    9    18.00    18.00
1910    10    20.00    38.00
1920    10    20.00    58.00
1930    10    20.00    78.00
1940    10    20.00    98.00
1950    1    2.00    100.00
       Total    50    100%     

* strategy 3
     No.    %    Cum %
1    30    60.00    60.00
2    9    18.00    78.00
3    11    22.00    100.00
    Total    50    100%
----------------------------------------------------------------------------------
Regards Jens Lauritsen
EpiData Association


More information about the EpiData-list mailing list