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;
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;
Can you give an example (or two) of what DATEINCHAR looks like and what you want DATE_SAS to look like?
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
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;
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)
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;
this worked. We dont need to convert this to numeric.
format dateinsas year.;
dateinsas=mdy(1,1,dateinchar);
Thanks!!!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.