DATA Step, Macro, Functions and more

creating age (years and month separately) from subtraction between 4 variables

Accepted Solution Solved
Reply
Occasional Contributor MCB
Occasional Contributor
Posts: 15
Accepted Solution

creating age (years and month separately) from subtraction between 4 variables

My data is the following:

 

ID BirthYear BirthMonth PensionYear PensionMonth
1 1944 1 2009 8
2 1947 2 2012 7
3 1941 10 2007 5

 

The table above shows when the person is born and when the person retired. I need to create two additional variables which show retirement age as 1. number of years and 2. number of month. For example I need to calculate that ID 1 has retired at the age of 65 years and 8 month.

 

ID BirthYear BirthMonth PensionYear PensionMonth PensionAgeYears PensionAgeMonth
1 1944 1 2009 8 65 8
2 1947 2 2012 7 65 5
3 1941 10 2007 5 65 7

  

Thanks!

 


Accepted Solutions
Solution
‎11-01-2016 04:59 AM
PROC Star
Posts: 754

Re: creating age (years and month separately) from subtraction between 4 variables

[ Edited ]

Something like this?

 

data mydata;
   input ID $ BirthYear BirthMonth PensionYear PensionMonth;
   datalines;
1 1944 1  2009 8
2 1947 2  2012 7
3 1941 10 2007 5
;

data PensionData;
   set mydata;
   Birthdate = mdy(BirthMonth,1,BirthYear);
   Pensiondate = mdy(PensionMonth,1,PensionYear);

   PensionAgeYears = intck('year', Birthdate, Pensiondate, 'c');
   PensionAgeMonth = intck('month', intnx('year', Birthdate, PensionAgeYears, 's'), Pensiondate, 'c');

   format Birthdate Pensiondate ddmmyy10.;
   drop Birthdate Pensiondate;
run;

http://sasnrd.com/date-basics/ 

View solution in original post


All Replies
Solution
‎11-01-2016 04:59 AM
PROC Star
Posts: 754

Re: creating age (years and month separately) from subtraction between 4 variables

[ Edited ]

Something like this?

 

data mydata;
   input ID $ BirthYear BirthMonth PensionYear PensionMonth;
   datalines;
1 1944 1  2009 8
2 1947 2  2012 7
3 1941 10 2007 5
;

data PensionData;
   set mydata;
   Birthdate = mdy(BirthMonth,1,BirthYear);
   Pensiondate = mdy(PensionMonth,1,PensionYear);

   PensionAgeYears = intck('year', Birthdate, Pensiondate, 'c');
   PensionAgeMonth = intck('month', intnx('year', Birthdate, PensionAgeYears, 's'), Pensiondate, 'c');

   format Birthdate Pensiondate ddmmyy10.;
   drop Birthdate Pensiondate;
run;

http://sasnrd.com/date-basics/ 

Occasional Contributor MCB
Occasional Contributor
Posts: 15

Re: creating age (years and month separately) from subtraction between 4 variables

Thanks!! this is perfect!

PROC Star
Posts: 754

Re: creating age (years and month separately) from subtraction between 4 variables

Anytime, Glad I could help Smiley Happy Please mark it as a solution

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 256 views
  • 1 like
  • 2 in conversation