02-09-2018 01:14 PM
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!
02-09-2018 02:14 PM
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).
02-09-2018 05:04 PM
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.
02-09-2018 02:15 PM
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;