DATA Step, Macro, Functions and more

SAS date formatted variable to non-date numeric variable

Reply
N/A
Posts: 0

SAS date formatted variable to non-date numeric variable

Hi,

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?

Thanks
Super Contributor
Super Contributor
Posts: 3,174

Re: SAS date formatted variable to non-date numeric variable

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.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: SAS date formatted variable to non-date numeric variable

Scott,

Thanks for your advices. Those were very helpful. You did help me to better understand the PUT and INPUT functions that I later used. Message was edited by: Caramel
SAS Super FREQ
Posts: 8,743

Re: SAS date formatted variable to non-date numeric variable

Hi:
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.
$200,512.10 or
200-51-210
20,051,210
20.051.210
200.512,10

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:
[pre]
elapsed = today() - datevar;
elapsed = 17729 - 16780; <----what SAS does internally
elapsed = 949 = almost 3 years which is the right number of days
VERSUS
20080716 - 20051210 = 29506???? this is a meaningless number
[/pre]


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:
[pre]
format to -- Display As --
use -3334 16780
mmddyy10. 11/15/1950 12/10/2005
year4. 1950 2005
monyy7. NOV1950 DEC2005
date9. 15NOV1950 10DEC2005
[/pre]

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.

cynthia
N/A
Posts: 0

Re: SAS date formatted variable to non-date numeric variable

Hello,

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.

Thanks again.
SAS Super FREQ
Posts: 8,743

Re: SAS date formatted variable to non-date numeric variable

Hi:
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:
[pre]
data testit;
justanum = 20051210;
newdate = input(put(justanum,8.),yymmdd8.);
output;
justanum = 19501115;
newdate = input(put(justanum,8.),yymmdd8.);
output;
run;

proc print data=testit;
title 'convert "raw" date to SAS date value';
run;

[/pre]

and the result would be the correct internal "timeline" date values:
[pre]
convert "raw" date to SAS date value

Obs justanum newdate

1 20051210 16780
2 19501115 -3334

[/pre]

cynthia
N/A
Posts: 0

Re: SAS date formatted variable to non-date numeric variable

Thank you very much Cynthia.

That really worked!!
Ask a Question
Discussion stats
  • 6 replies
  • 254 views
  • 0 likes
  • 3 in conversation