DATA Step, Macro, Functions and more

character to date

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

character to date

My year field has been stored in character field of 25 length.

I want to convert this to date field with years.

I tried this code but the output is empty

data want;

set have;

date_sas=input(put(dateinchar, 4.), yymmdd8.);

format date_sas date 9.;

run;


Accepted Solutions
Solution
‎05-28-2015 01:51 PM
Regular Contributor
Posts: 237

Re: character to date

Posted in reply to buckeyefisher

You will need to change the date format to what you want it to look like.  Instead of YYMMDD8. try YEAR. in its place:

data dates;

  length dateinchar $25;

  input dateinchar $;

  format dateinsas year.;

  dateinnum=input(dateinchar,4.);

  dateinsas=mdy(1,1,dateinnum);

  cards;

2004

2005

1999

;

run;

View solution in original post


All Replies
Regular Contributor
Posts: 237

Re: character to date

Posted in reply to buckeyefisher

Can you give an example (or two) of what DATEINCHAR looks like and what you want DATE_SAS to look like?

Contributor
Posts: 63

Re: character to date

Posted in reply to djbateman

dateinchar (this field is defined as character in the input file length - 25)

2004

2005

1999

dateinsas ( I want this to be defined as date field, length could be 4 digits)

2004

2005

1999

Regular Contributor
Posts: 237

Re: character to date

Posted in reply to buckeyefisher

Does this help?  I converted the 4-digit character year into a 4-digit numeric year (DATEINNUM), but I also converted it into a SAS date (DATEINSAS) using the MDY() function where I assumed the month was January and the day was the 1st.  If you want a different month/day value, you can change those as you see fit.

data dates;

  length dateinchar $25;

  input dateinchar $;

  format dateinsas yymmdd8.;

  dateinnum=input(dateinchar,4.);

  dateinsas=mdy(1,1,input(dateinchar,4.));

  cards;

2004

2005

1999

;

run;

Contributor
Posts: 63

Re: character to date

Posted in reply to djbateman

Thanks, It doesn't work.

For example when input  = 2007 , it gives 07-01-01 whereas I want output = 2007 (but the field being defined as a date field)

Solution
‎05-28-2015 01:51 PM
Regular Contributor
Posts: 237

Re: character to date

Posted in reply to buckeyefisher

You will need to change the date format to what you want it to look like.  Instead of YYMMDD8. try YEAR. in its place:

data dates;

  length dateinchar $25;

  input dateinchar $;

  format dateinsas year.;

  dateinnum=input(dateinchar,4.);

  dateinsas=mdy(1,1,dateinnum);

  cards;

2004

2005

1999

;

run;

Contributor
Posts: 63

Re: character to date

Posted in reply to djbateman

this worked. We dont need to convert this to numeric.

format dateinsas year.;

dateinsas=mdy(1,1,dateinchar);

Thanks!!!

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 334 views
  • 0 likes
  • 2 in conversation