Desktop productivity for business analysts and programmers

Convert Numeric to Date

Occasional Contributor Ted
Occasional Contributor
Posts: 16

Convert Numeric to Date

I have a variable 'date' which stores dates as numbers, eg: 20070202. Although I know this is a date SAS just sees a number. How can I convert this to a date format so I can then use it to calculate someones age?
Posts: 0

Re: Convert Numeric to Date

Your problem is two-fold.

You say that you have a number holding a date, but the format of that number is in fact a string of the apparent form Ccyymmdd. What you want to do is take that string and read it as a date.

That is quite easy, requiring only that you use an Input() function with an appropriate informat. The structure of the informat is Yymmdd followed by a length. Personally, I use a length of 10 bytes, as if the fractional bars were still in the date, so select "Yymmdd10.".

However, if you have a number of slightly more than 20 million that you are trying to convert, then you are trying to use an informat which expects a string value to read a number. SAS will then warn you of the conversion of a number to a character before it converts the value to a date. You get the result but at the request of an unnecessary message in the log.

Dinosaur curmudgeons like me believe in handling data correctly so that when messages like these occur, we know something unexpected has happened. The conversion of 20 million to a string and then conversion to a date is not unexpected, so we code around these issues by handling the conversion with a format.

Here is the code I used, in the log entry, and as you see, it is a clean interpretation of the data.

17 Data TEST;
18 DATENUM = 20070202;
19 DATE = Input( Put( DATENUM, 8.), Yymmdd10.);
20 Put DATE = Date9.;
21 Run;

NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.46 seconds
user cpu time 0.01 seconds
system cpu time 0.01 seconds

Kind regards

Posts: 0

Re: Convert Numeric to Date

I have a similar problem except the variable "date" is stored as 20070202, but it is a character variable, how can i change that to a date format? so the output would look like 2007-02-02
Respected Advisor
Posts: 3,828

Re: Convert Numeric to Date


SAS knows exactly 2 variable types: numeric and character.
To read data you can use an INFORMAT to tell SAS how to interprete the data for reading, to write data you can use a FORMAT.

SAS dates and datetimes are stored as numbers using NUMERIC variables.
To write these numbers which represent a date you have to apply a (date-) format on these numeric variables.

In your case:
1. convert the character string representing a date (and stored in a character variable) to a numeric value representing a SAS date -> read the character string using a (date) INFORMAT.
2. Write the SAS date (a number) as date using a (date-) FORMAT.

data _null_;
/* format DateAsNumber yymmdd10.;*/
put DateAsNumber= DateAsNumber= yymmdd10. DateAsNumber= eurdfdd10.;


Message was edited by: Patrick
Occasional Contributor
Posts: 6

Re: Convert Numeric to Date

I want to add 3 months to a character date variable. Here is what I did:


1. Convert the character date to a number and added 3 months (used 90 days) to that number



2. Now I want to display results in (1) as a date in format ddmmmyyyy. I am having a lot of difficulty in doing this. As I read the help,


   should do it. Obviously I am wrong.


Can someone help me? Thanks,



Ask a Question
Discussion stats
  • 4 replies
  • 4 in conversation