BookmarkSubscribeRSS Feed
deleted_user
Not applicable
In SAS 9, I have a table that has a text field that holds information that is a date in YYYYMMDD format. I need to convert this data to a date format as I need to do comparisons on this field.

Some samples from the field would be:

20081002
20070417
20080101

The field these are stored in is a text field. How can I make this data workable if I need to compare it to another date, say finding all data that is within the last year.

Thanx.
Andy
1 REPLY 1
Cynthia_sas
SAS Super FREQ
Hi:

You might want to investigate the INPUT function. For example, if your text field was called CHARSTR, then you could create a numeric variable from the character field by using the INPUT statement:
[pre]
newdate = input(charstr,yymmdd8.);
[/pre]

In this instance, (inside a DATA step program), the new variable NEWDATE would be numeric, would be internally stored as the number of days since Jan 1, 1960, and could be formatted with any of the SAS date formats.

You could then do comparisons or test conditions using NEWDATE like this:
[pre]
if year(newdate) = 2007 . . .
or
if newdate lt '01Jan2008'd . . .
or
if year(newdate - 180) = 2007 . . .
[/pre]

cynthia

ps...there have been a lot of previous forum postings on the conversion of character dates to SAS date values. A search of the forum, might get you some more code examples.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 715 views
  • 0 likes
  • 2 in conversation