BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ted
Calcite | Level 5 Ted
Calcite | Level 5
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?
1 ACCEPTED SOLUTION

Accepted Solutions
deleted_user
Not applicable

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.

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;





 

View solution in original post

4 REPLIES 4
deleted_user
Not applicable

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.

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;





 

deleted_user
Not applicable
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
Patrick
Opal | Level 21
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.

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

HTH
Patrick

Message was edited by: Patrick
AminB
Fluorite | Level 6

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 203895 views
  • 1 like
  • 4 in conversation