BookmarkSubscribeRSS Feed
StefB
Calcite | Level 5

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

8 REPLIES 8
antonbcristina
SAS Employee

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

StefB
Calcite | Level 5

The birth date is posted as 5/19/1978

Thank you!

antonbcristina
SAS Employee

And how would you like it to look?

StefB
Calcite | Level 5

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. 

antonbcristina
SAS Employee

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).

StefB
Calcite | Level 5

I will try this! Thank you!

ballardw
Super User

@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.

Reeza
Super User

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;

 

 

 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 749 views
  • 0 likes
  • 4 in conversation