Hello there,
I'm having trouble to convert the following character field to date field with the SAS code below:
data temp1;
length id $1. date $10.;
infile DATALINES dsd missover;
input id date $;
CARDS;
1, 7/ 3/2011
2, 3/17/2011
;
run;
data temp2;
set temp1;
new_date=input(date, mmddyy10.); format new_date date9.;
run;
The code works for the second record (3/17/2011) but not for the first one (7/ 3/2011). I think the reason for that is the way the day part of the field was recorded in the second record. As you can notice there is a blank space between "/" and "3".
I was wondering if there is another SAS date format to be able to tackle this issue.
Best,
Recep
PS: I use SAS 9.4 TS Level 1M3 on W32 7Pro platform.
Hi @Recep I have had that issue before. if you know your char date values are inconsistent of that type, use compress
data temp1;
length id $1. date $10.;
infile DATALINES dsd missover;
input id date $;
CARDS;
1, 7/ 3/2011
2, 3/17/2011
;
run;
data temp2;
set temp1;
new_date=input(compress(date), mmddyy10.); format new_date date9.;
run;
Try:
informat date mmddyy10.;
data temp1;
length id $1. date 8.;
informat date mmddyy10.;
infile DATALINES dsd truncover;
input id date ;
CARDS;
1, 7/ 3/2011
2, 3/17/2011
;
run;
Hi Reza,
Thanks a lot for your response! Unfortunately the code did not work. Plus I was hoping to deal with the problem with a SAS date format as opposed to reading raw data into SAS. The data I have already exist as a SAS file and I would like to manipulate the character date field.
Cheers,
Recep
new_var = input(variable, mmddyy10.);
@Recep wrote:
Hi Reza,
Thanks a lot for your response! Unfortunately the code did not work. Plus I was hoping to deal with the problem with a SAS date format as opposed to reading raw data into SAS. The data I have already exist as a SAS file and I would like to manipulate the character date field.
Cheers,
Recep
Hi @Recep I have had that issue before. if you know your char date values are inconsistent of that type, use compress
data temp1;
length id $1. date $10.;
infile DATALINES dsd missover;
input id date $;
CARDS;
1, 7/ 3/2011
2, 3/17/2011
;
run;
data temp2;
set temp1;
new_date=input(compress(date), mmddyy10.); format new_date date9.;
run;
Thanks a lot! This is exactly what I needed to...
Recep
I think you meant to mark @novinosrin post as correct because mine clearly isn't....:(
Thanks a lot for letting me know Reeza! Clearly it's getting late here:)
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.