How can I convert a date formatted variable to a regular formatted (e.g. 8.) numeric variable?
If I have a variable with an YYMMDDn8. format, how I can transfer that value (e.g. 20051210) to a new numeric variable? That new variable should have just an 8. format but I am interested to keep all the 8 digits. I have tried different ways already but I am getting always the YYMMDDn8. format.
In other words, how do I change the YYMMDDn8. date format without losing the 8 digits?
A SAS DATE variable is a numeric value internally, representing a count of days since Jan 1 1960. You can assign a new SAS variable in a DATA step to your SAS DATE variable, and you will need to use both the PUT function and the INPUT function in order to create a SAS numeric variable containng the 8 digits as you have explained. What you are accomplishing is that you are first "formatting" the SAS DATE variable. Then you are "inputting" the formatted string using a specific INFORMAT.
Another option is again to use the PUT function in an assignment statement, however you can declare the SAS NUMERIC variable with a LENGTH statement, but this statement must occur before the assignment statement.
Personally, my experience is that it's better to know how you are expressing variable values through PUT and INPUT functions, rather than letting SAS automatically convert variable values on your behalf in a DATA step.
To me, the question is what are you really trying to do, or what is your purpose for treating the date as a "raw" number????
For example, the date, 12/10/2005 or 10Dec2005 or 20051210 -- can be represented many different ways, but no matter which representation we choose, it ALWAYS means the 10th of December 2005. 20051210 depends on context for meaning.
So, if "20051210" looked like any of these numbers, with punctuation added, then none of us would immediately think of Dec 10th 2005 as the meaning for the number.
SAS stores a date as a number on a timeline -- if Jan 1, 1960 is "day 0" on the timeline, then Dec 10th 2005 represents 16780 days FROM Jan 1, 1960. And, since there were days BEFORE Jan 1, 1960, the SAS internal timeline allows for dates to go back to the 1500's. But let's just take one date, for example, Nov 15th 1950 was 3334 days BEFORE Jan 1, 1960 so the internal number for that date is -3334. The internal number for Dec 10th 2005 is 16780; the internal number for
Dec 10th 2999 is 379831; the internal number for Dec 10th 1600 is -131143. I think that's pretty cool how SAS stores dates in this fashion.
Once your date is stored as that "timeline version" of the number, there is no ambiguity about what the number can mean inside SAS when the variable is used. For example, let's say I want to find out how many days it's been between Dec 10th 2005 and today (July 16th 2008), I can do something like this in SAS:
elapsed = today() - datevar;
elapsed = 17729 - 16780; <----what SAS does internally
elapsed = 949 = almost 3 years which is the right number of days
20080716 - 20051210 = 29506???? this is a meaningless number
But, that subtraction only works IF the dates are stored as their timeline versions of the number. As you can see, subtracting the 8 digit version of the number does not accurately reflect the correct number of elapsed days.
If it's annoying to see 16780 or -3334, when you want to see dates, the solution is very simple -- just assign a permanent format (such as mmddyy10. or yymmddn8.) to the date variable. Most SAS procedures will use a permanently assigned format to display a date value. If you want to change the displayed value for a date, you can change the permanent format by using a different format. So the same timeline version of the dates can be displayed different ways without affecting how SAS will be able to use them internally:
format to -- Display As --
use -3334 16780
mmddyy10. 11/15/1950 12/10/2005
year4. 1950 2005
monyy7. NOV1950 DEC2005
date9. 15NOV1950 10DEC2005
Do you really want Dec 10th 2005 to be to be internally stored as the number, 20051210 (20 milltion, 51 thousand, Two-hundred and 10)?? It will make your unformatted date easy to read, but will pretty much render the value useless for calculating how long or how old or elapsed time.
Thanks for your message. The reason for treating the date as a "raw" number is because I have to merge that data file with another data file using the date as an identifier. Unfortunately the date on the other data file was defined as a "raw" number with 8 digits in the form yyyymmdd instead of using the date format.
Well, rather than turn your SAS date into a "raw" number, I'd be very tempted to turn the "raw" number from the other file into a SAS date value. Let's say that your "raw" number was called JUSTANUM:
justanum = 20051210;
newdate = input(put(justanum,8.),yymmdd8.);
justanum = 19501115;
newdate = input(put(justanum,8.),yymmdd8.);
proc print data=testit;
title 'convert "raw" date to SAS date value';
and the result would be the correct internal "timeline" date values:
convert "raw" date to SAS date value