converting birthdate to a number

Reply
Occasional Contributor
Posts: 5

converting birthdate to a number

Hi all,

I am VERY new to SAS.  As in got thrown into it this week for a work project.  In excel I could format a birth date into a number.  Can I do that in SAS?  If so, how?  Thanks in advance for any help!

 

-Stef

SAS Employee
Posts: 14

Re: converting birthdate to a number

Can you post an example of a birth date you have and what you'd like it to look like? 

Occasional Contributor
Posts: 5

Re: converting birthdate to a number

Posted in reply to antonbcristina

The birth date is posted as 5/19/1978

Thank you!

SAS Employee
Posts: 14

Re: converting birthdate to a number

And how would you like it to look?

Occasional Contributor
Posts: 5

Re: converting birthdate to a number

Posted in reply to antonbcristina

So in excel if I were to format 5/19/1981 into a number it would be 29725.  I do know that Excel formats the number based off Jan 1, 1900. 

SAS Employee
Posts: 14

Re: converting birthdate to a number

Ok, I'm guessing the birth date you have already is stored as a character string. What you could do is create a new variable that stores the numeric version of the character string. SAS stores dates numerically as the number of days since January 1st, 1960. To convert your character date (say bday_c) into a numeric one (say bday_n), you can use the following:

 

bday_n = input(bday_c,mmddyy.);

 

The INPUT function is used here, which has two arguments: a character string and an informat. The informat MMDDYY specifies that SAS should read the character string as having the month first (mm), then the day (dd), and finally the year (yy).

Highlighted
Occasional Contributor
Posts: 5

Re: converting birthdate to a number

Posted in reply to antonbcristina

I will try this! Thank you!

Super User
Posts: 12,148

Re: converting birthdate to a number


StefB wrote:

So in excel if I were to format 5/19/1981 into a number it would be 29725.  I do know that Excel formats the number based off Jan 1, 1900. 


For an entertaining result in Excel type 0 (zero) into a cell. Then format the cell as a date. Excel will treat the value as "1/0/1900" or "

Saturday, January 00, 1900

 

 

SAS dates are usable from 1581 through the year 20,000 though the currently supplied date formats don't like years past 9999.

Super User
Posts: 21,478

Re: converting birthdate to a number

SAS will do that by default, but you can have it look like a date even though its a number underneath. Then the dates can be subtracted/added where necessary.

 

The first thing to do is check what format your date is already in, use PROC CONTENTS to do that:

 

proc contents data=sashelp.stocks;
run;

If it's not a number with a date format then you need to change it. Otherwise it's already a number and if you want to see it that way you can remove the format. 

 

SAS dates are indexed from Jan 1, 1960. 


Here's a demo of how dates work:

 

*create demo data;
data have;
input date1 $  date2 $ ;
cards;
01/12/1997 02/13/1997
04/12/2004 04/25/2008
;
run;

data want;
set have;

*convert to numeric dates;
date1_num = input(date1, mmddyy10.);
date2_num = input(date2, mmddyy10.);

*calculate the difference;
dif = date2_num - date1_num;

*format second date to look like a date;
format date2_num date9.;
run;

proc print data=want;run;

 

 

 

 

 

Ask a Question
Discussion stats
  • 8 replies
  • 122 views
  • 0 likes
  • 4 in conversation