Question

I have a data set that looks something like this:

    Name  birth  birth_day  death  death_day   
    Don   1913   02-Mar     1998   01-Nov
    Jack  1924   04-Dec     1970   13-Sep

I want to calculate the age of the people but I am confused as to how to format the entries in birth_day and death_day numerically so that I can subtract their birthdays from their death days to calculate their age. My professor suggested using the dmy function, so I did

    x=dmy(day(birth_day), month(birth_day), birth);

However that did not work as planned. How can I go about resolving this problem?

In response to rambles:

     data _null_;
     set mydata;

     death_date = input(cats(death_day,'-', death), date11.);
     birth_date = input(cats(birth_day,'-', birth), date11.);
     format death_date birth_date date11. age 4.1;

     age = yrdif(birth_date, death_date,'AGE');  ** The 'AGE' part only works in SAS 9.3+!!;
     put death_date= birth_date= age=;
     run;

I tried something like this, however I ge an error at the line format death_date birth_date date11. age 4.1; saying ERROR 48-59: The format $DATE was not found or could not be loaded.

Was it helpful?

Solution

The following example mimics your first row:-

   data example;
     Name = "Don";
     birth_day = "02-Mar";
     birth = "1913";
     death_day = "01-Nov";
     death = "1998";
   run;

   data example_with_age;
     set example;
     length death_date birth_date 5 age 3;
     format death_date birth_date date11. age 4.1;

     death_date = input(cats(death_day,'-', death), date11.);
     birth_date = input(cats(birth_day,'-', birth), date11.);

     age = yrdif(birth_date, death_date,'AGE');  ** The 'AGE' part only works in SAS 9.3+!!;
   run;

I've assumed your birth and death variables are character. If they're numeric, you should surround them with a put statement in the input rows.

I've been lazy with calcalating age by using a feature in newer versions of SAS - it's a surprising hassle calculating age when using full dates. If you're using an older SAS version, let me know in a comment and I'll add the older method of calculating the age.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top