BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
buckeyefisher
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
djbateman
Lapis Lazuli | Level 10

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

6 REPLIES 6
djbateman
Lapis Lazuli | Level 10

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

buckeyefisher
Obsidian | Level 7

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

djbateman
Lapis Lazuli | Level 10

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;

buckeyefisher
Obsidian | Level 7

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)

djbateman
Lapis Lazuli | Level 10

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;

buckeyefisher
Obsidian | Level 7

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

format dateinsas year.;

dateinsas=mdy(1,1,dateinchar);

Thanks!!!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1117 views
  • 0 likes
  • 2 in conversation