Use PROC FCMP to create a user defined function to calculate a person's age. User defined functions created by PROC FCMP can be used in the DATA Step beginning in SAS 9.2.
For more information on PROC FCMP click on the link below to go to a 2007 SAS Global Forum paper about the procedure.
User-Written DATA Step FunctionsClick on the link below to go to the SAS 9.2 documentation about PROC FCMP
SAS 9.2 PROC FCMP DocumentationLink to another note about calculating age.
Sample 24567: Calculate a person's age
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* Use the INTCK function to count the number of months between */
/* the date of birth and the current date. Divide the number of */
/* months by 12 to produce the number of years. Use the MONTH */
/* function to determine if the month of the birthday and the current */
/* date are the same. If they are, determine if the birthday has */
/* occurred this year. If it hasn't, adjust the age by subtracting */
/* one year. */
proc fcmp outlib=work.functions.samples;
function sas_age(Birthday,Current);
age=int(intck('month',birthday,current)/12);
if month(birthday)=month(current) then
age = age-(day(birthday)>day(current));
return(age);
endsub;
run;
options cmplib=work.functions;
data birth;
input name $ bday :mmddyy10.;
datalines;
Miguel 12/31/1973
Joe 02/28/1976
Rutger 03/29/1976
Broguen 03/01/1976
Susan 12/12/1976
Michael 02/14/1971
LeCe 11/09/1967
Hans 07/02/1955
Lou 07/30/1960
;
run;
data ages;
set birth;
retain current;
if _n_=1 then current=today();
format bday current worddate20.;
actualage=sas_age(bday,current);
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Obs name bday current actualage 1 Miguel December 31, 1973 August 7, 2009 35 2 Joe February 28, 1976 August 7, 2009 33 3 Rutger March 29, 1976 August 7, 2009 33 4 Broguen March 1, 1976 August 7, 2009 33 5 Susan December 12, 1976 August 7, 2009 32 6 Michael February 14, 1971 August 7, 2009 38 7 LeCe November 9, 1967 August 7, 2009 41 8 Hans July 2, 1955 August 7, 2009 54 9 Lou July 30, 1960 August 7, 2009 49
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> FCMP |
Date Modified: | 2009-08-25 13:36:54 |
Date Created: | 2009-08-07 15:36:39 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | z/OS | 9.2 TS1M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.2 TS1M0 | |||
Microsoft Windows XP 64-bit Edition | 9.2 TS1M0 | |||
Microsoft® Windows® for x64 | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.2 TS1M0 | |||
Microsoft Windows XP Professional | 9.2 TS1M0 | |||
Windows Vista | 9.2 TS1M0 | |||
64-bit Enabled AIX | 9.2 TS1M0 | |||
64-bit Enabled HP-UX | 9.2 TS1M0 | |||
64-bit Enabled Solaris | 9.2 TS1M0 | |||
HP-UX IPF | 9.2 TS1M0 | |||
Linux | 9.2 TS1M0 | |||
Linux for x64 | 9.2 TS1M0 | |||
OpenVMS on HP Integrity | 9.2 TS1M0 | |||
Solaris for x64 | 9.2 TS1M0 |