SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
annie0
Calcite | Level 5

Hi SAS community,

 

I encountered a data change issue when I use proc import to call in XLSX into sas. There is 1 column, let us call it date1. And the column is text in excel file, contains date information in different formats, please see screenshot1. When it is imported into SAS, the value of date1 changed as screenshot2. You can see highlighted part in screenshot2 becomes 4xxxx. If I tried to use put function to convert it to charater, the value changed to other dates as shown in screenshot3.

 

Is ithere any solution to avoid this data change issue?

 

Thanks!

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What you are describing is what happens when you mix date values and strings in the same column in your Excel file.  When SAS sees mixed types in the same column it sets the field as character in SAS.  But then the date values come across as digit strings that represent the actual number that Excel uses to store a date, instead of the formatted value that Excel shows you when looking at the spreadsheet.

 

Easiest fix is to find those cells in the Excel sheet that have date values instead of strings and convert them from date to text.  Then they will import the same as the other text fields.

 

If you cannot do that then you can try to write code to check if the value looks like a raw Excel date and then convert it.

 

So assume your variable with the mixed up column is named RAW this code will make a new column name DATE with a SAS date value.

data want;
  set have;
  date=input(raw,??date11.);
  format date date9.;
  if missing(date) and 1 <= input(date,??32.) <= 50000 then do;
    date = int(input(date,??32.))+'30DEC1899'd ;
  end;
run;

SAS uses 1960 as the base year and Excel uses 1900.  But they use different starting numbers and Excel mistakenly thinks that 1900 was a leap year.  Hence the offset is 2 days different then just remove the SAS date for 1/1/1900 (remember that dates before 1960 are negative numbers in SAS). 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

What you are describing is what happens when you mix date values and strings in the same column in your Excel file.  When SAS sees mixed types in the same column it sets the field as character in SAS.  But then the date values come across as digit strings that represent the actual number that Excel uses to store a date, instead of the formatted value that Excel shows you when looking at the spreadsheet.

 

Easiest fix is to find those cells in the Excel sheet that have date values instead of strings and convert them from date to text.  Then they will import the same as the other text fields.

 

If you cannot do that then you can try to write code to check if the value looks like a raw Excel date and then convert it.

 

So assume your variable with the mixed up column is named RAW this code will make a new column name DATE with a SAS date value.

data want;
  set have;
  date=input(raw,??date11.);
  format date date9.;
  if missing(date) and 1 <= input(date,??32.) <= 50000 then do;
    date = int(input(date,??32.))+'30DEC1899'd ;
  end;
run;

SAS uses 1960 as the base year and Excel uses 1900.  But they use different starting numbers and Excel mistakenly thinks that 1900 was a leap year.  Hence the offset is 2 days different then just remove the SAS date for 1/1/1900 (remember that dates before 1960 are negative numbers in SAS). 

annie0
Calcite | Level 5
Thank you so much for your reply. Very helpful.
ballardw
Super User

Sometimes, depending on how badly the data has been mixed up, then saving a spreadsheet as a CSV file and importing that may fix this sort of issue. If using a wizard to import CSV set the guessingrows option available with delimited files to a large value or in Proc Import code.

 

 

annie0
Calcite | Level 5
Thank you so much for your reply!

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 4 replies
  • 10774 views
  • 0 likes
  • 3 in conversation