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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

7 REPLIES 7
Reeza
Super User

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;
Recep
Quartz | Level 8

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

Reeza
Super User

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


 

novinosrin
Tourmaline | Level 20

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;
Recep
Quartz | Level 8

Thanks a lot! This is exactly what I needed to...

 

Recep

Reeza
Super User

I think you meant to mark @novinosrin post as correct because mine clearly isn't....:(

Recep
Quartz | Level 8

Thanks a lot for letting me know Reeza! Clearly it's getting late here:)

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1408 views
  • 4 likes
  • 3 in conversation