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

Dear SAS community, really appreciate your help

I have this field study_date that has mixed formats and I am struggling with.  I imported this excel (first screenshot) then used this code but it generated many uninterpretable data.  Please see the second screenshot.  How can I read this column in to capture all the values as dates. Thanks again

 

datestudy=input(date_study, ANYDTDTE11.);

rykwong_0-1697142579816.png

rykwong_1-1697142860907.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So is the first one what EXCEL shows and the second is what you get if you run PROC IMPORT on the EXCEL file?

Tom_0-1697144503718.pngTom_1-1697144555991.png

 

Then this is what happens when you MIX character and numeric values in the same COLUMN in the EXCEL worksheet.

 

Best solution is to change those string values like '02JUN2020'  (or is that 'O2JUN2O2O' instead) into actual DATE values.  Then the PROC IMPORT will make a NUMERIC variable.

 

If you have to fix the gibberish that PROC IMPORT generated then do something like:

data want;
  set have;
  date = input(date_study,??date9.);
  if missing(date) and not missing(date_study) then 
    date='30DEC1899'd + input(date_study,32.)
  ;
  format date date9.;
run;

So the first input will convert strings like '02JUN2020' into a number of days.  The second input will convert strings like '43843' into a number of days and then adjust for the difference in where SAS and EXCEL start numbering.

 

 

View solution in original post

10 REPLIES 10
Reeza
Super User
I'm confused. Are there two different screenshots of the date_study column or one?
rykwong
Quartz | Level 8

Hi Reeza

this is first screenshot is how the data is in excel

rykwong_0-1697144230836.png

 

this is the second screenshot is after the excel is read by sas

rykwong_1-1697144294739.png

 

Kurt_Bremser
Super User
  • Make sure you have consistent formatting in the spreadsheet
  • Save to a text (csv) file
  • Read it with a DATA step, and use the appropriate informat for the date column
ballardw
Super User

Your first picture tells me that your "dates" are a mix of character and numeric values in Excel. Why do I think that you may ask. Value alignment: some are justified to the left of the cell, these are very likely character, the ones justified to the right are Excel date values with an Excel date display set (will typically have numeric values in the 44,000 range if roughly current which aren't treated as a date by any SAS informat because they do not have year, month and day components but instead are a number of days since 1 Jan 1900 or).

Easiest solution in the long wrong: If all the dates when you look at them in Excel have the same appearance then do a File Save As file to CSV. Then read that CSV file with a data step. If your column formatted properly you may be able to read with a known type informat like DATE9. and not deal with a guessing informat like the Anydtdte.

 

 

rykwong
Quartz | Level 8
thanks, will fix that
Tom
Super User Tom
Super User

So is the first one what EXCEL shows and the second is what you get if you run PROC IMPORT on the EXCEL file?

Tom_0-1697144503718.pngTom_1-1697144555991.png

 

Then this is what happens when you MIX character and numeric values in the same COLUMN in the EXCEL worksheet.

 

Best solution is to change those string values like '02JUN2020'  (or is that 'O2JUN2O2O' instead) into actual DATE values.  Then the PROC IMPORT will make a NUMERIC variable.

 

If you have to fix the gibberish that PROC IMPORT generated then do something like:

data want;
  set have;
  date = input(date_study,??date9.);
  if missing(date) and not missing(date_study) then 
    date='30DEC1899'd + input(date_study,32.)
  ;
  format date date9.;
run;

So the first input will convert strings like '02JUN2020' into a number of days.  The second input will convert strings like '43843' into a number of days and then adjust for the difference in where SAS and EXCEL start numbering.

 

 

rykwong
Quartz | Level 8

this solve my problem!  thanks so much!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 1985 views
  • 4 likes
  • 5 in conversation