Converting text into date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Converting text into date

Hi,

I am relatively new at this and always find this tricky i have fields that are like 1/7/2011 12/11/2013 etc, and they are text, how do i turn these into proper sas dates.

Thanking in advance,

Stan


Accepted Solutions
Solution
‎06-17-2014 11:16 PM
Occasional Contributor
Posts: 7

Re: Converting text into date

I found the solution here it is:

data want;

set have;

new_date = input (date,ddmmyy10.) ;

  format new_date ddmmyy10. ;

put _all_ ;

run;

View solution in original post


All Replies
Super Contributor
Posts: 297

Re: Converting text into date

Are these dates in text files or a sas dataset?

Occasional Contributor
Posts: 7

Re: Converting text into date

Posted in reply to Scott_Mitchell

These are variables within a dataset, I've tried  this:

data _null_;

a='1/7/2011';

b=input(put(a,8.),mmddyy10.);

put b/b=mmddyy10.;

run;

but that dont seem to work, any other suggestions will be greatly apreciated.

Many thanks,

Stan

Super Contributor
Posts: 275

Re: Converting text into date

data have;

input date :$10.;

_date=input(strip(date),mmddyy10.);

format _date mmddyy10.;

cards;

1/7/2011

2/19/2011

10/13/2012

12/11/2013

;

run;

Occasional Contributor
Posts: 7

Re: Converting text into date

sorry mate how would you do the same on the existing data set rather than the one you have created?

Many thanks,

stan

Super Contributor
Posts: 275

Re: Converting text into date

data want;

  set have;

  _date=input(strip(date),mmddyy10.);

  format _date mmddyy10.;

run;

Occasional Contributor
Posts: 7

Re: Converting text into date

the strip function didnt seem to work, aim on sas 9.2

Super Contributor
Posts: 644

Re: Converting text into date

If strip() is not available use trim(left(...))

Richard

Super Contributor
Posts: 275

Re: Converting text into date

data _null_;

a='1/7/2011';

b=input(put(a,8.),mmddyy10.);

put b/b=mmddyy10.;

run;

Super User
Super User
Posts: 7,997

Re: Converting text into date

Per slchen you would use the input function to convert text strings into numeric variables (which are what date/time variables are in SAS).  The input requires you to specify the format of the data, length.  So in your example your date value is 10 characters, mm/dd/yyyy, spaces are interpreted as 0.  So the format to read this in would be mmddyy10.  If you have only a two digit year then: mm/dd/yy, so the format is mmddyy8.  The input reads in the text string and converts it to numeric SAS date which can have a format applied to it to show the date representation - otherwise you will just see a number.  The reverse of course is put which takes a numeric and puts it into a character string.

Super Contributor
Posts: 307

Re: Converting text into date

Kinda looks like the same question as the just-posted https://communities.sas.com/message/215994#215994

Solution
‎06-17-2014 11:16 PM
Occasional Contributor
Posts: 7

Re: Converting text into date

I found the solution here it is:

data want;

set have;

new_date = input (date,ddmmyy10.) ;

  format new_date ddmmyy10. ;

put _all_ ;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 3200 views
  • 0 likes
  • 6 in conversation