## Convert Numeric to Date

Solved
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?

Accepted Solutions
Solution
‎07-26-2017 01:11 PM
Not applicable
Posts: 0

## Re: Convert Numeric to Date

[ Edited ]

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.

``````Data TEST;
DATENUM = 20070202;
DATE = Input( Put( DATENUM, 8.), Yymmdd10.);
Put DATE = Date9.;
Run;``````
```DATE=02FEB2007
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

David

If your data value is CHARACTER, then use this technique from @Patrick:

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_;
DateAsCharacter='20070202';
DateAsNumber=input(DateAsCharacter,yymmdd8.);

/* format DateAsNumber yymmdd10.;*/
put DateAsNumber= DateAsNumber= yymmdd10.
DateAsNumber= eurdfdd10.;
run;``````

All Replies
Solution
‎07-26-2017 01:11 PM
Not applicable
Posts: 0

## Re: Convert Numeric to Date

[ Edited ]

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.

``````Data TEST;
DATENUM = 20070202;
DATE = Input( Put( DATENUM, 8.), Yymmdd10.);
Put DATE = Date9.;
Run;``````
```DATE=02FEB2007
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

David

If your data value is CHARACTER, then use this technique from @Patrick:

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_;
DateAsCharacter='20070202';
DateAsNumber=input(DateAsCharacter,yymmdd8.);

/* format DateAsNumber yymmdd10.;*/
put DateAsNumber= DateAsNumber= yymmdd10.
DateAsNumber= eurdfdd10.;
run;``````

Not applicable
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
Posts: 4,797

## Re: Convert Numeric to Date

DrewSAS

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.

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_;
DateAsCharacter='20070202';
DateAsNumber=input(DateAsCharacter,yymmdd8.);
/* format DateAsNumber yymmdd10.;*/
put DateAsNumber= DateAsNumber= yymmdd10. DateAsNumber= eurdfdd10.;
run;

HTH
Patrick

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

input(LN_DC,mmddyy10.)+90

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,

put(input(LN_DC,mmddyy10.)+90),mmddyy10.

should do it. Obviously I am wrong.

Can someone help me? Thanks,

Amin

☑ This topic is solved.