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

Hi,

I have a problem with output CSV data.

I have a file consist of Month/Year in Text format, for example 11/2007, 01/2003 ....ect.

when I use ODS csv to output the file, and then open in excel file,  the Month/Year values change from 11/2007 to 7-Nov, and 01/2003 changes to 3-Jan.

I wonder, is there a way to hold the original date value as is when I do ODS csv and looks the same when open in excel file.

Any idea would be appreciate.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
DanielMK
Calcite | Level 5

Hi Ursula,

When a CSV file is opened in Excel, Excel tries to guess the data type of each cell.  If the cell only contains numbers and some symbols (e.g., /) it will interpret it as a date.  This is regardless of the data type of your original SAS dataset.  Excel, however, does not support incomplete dates so it also tries to complete your dates, and sometimes it does it right and sometimes not.  On top of that, once the date is complete, Excel applies a format to the cell to display the date in a certain way.  You can't change how Excel treats dates, so depending on what your end goal is, I would suggest one of the following two options:

1-In your SAS dataset, i.e., before exporting as CSV, add an apostrophe/single quote (') before the incomplete date, and then make sure this symbol appears in the CSV file.  When Excel opens the CSV file, it will interpret the cell as text and will show exactly what you want.  However, you won't be able to do calculations or sorting in Excel with this field.

2-In your SAS dataset, complete the date (e.g., '03/2015' should be '03/01/2015' or '01-Mar-2015') and then convert it to a date field with the input function.  Excel will consistently import a complete date correctly.

I hope this helps,

Daniel

View solution in original post

8 REPLIES 8
Peter_C
Rhodochrosite | Level 12

Problem is the import in excel

ursula
Pyrite | Level 9

Thank you, Peter.

I noticed, this is happened only if the date value is Month/year, but it works fine if the date value is Month/Day/Year.

Eric_stats
Calcite | Level 5

Hi, Ursula.    A couple of suggestions for possibilities.   

1.  In Excel,  you could format the column   via Format Cells, Custom,  mm/yyyy

2. When outputting to .csv from SAS, can you add a   '     in front of each date record,  as in '01/2001.   Should enable it to be viewed "as-is" in excel.

ursula
Pyrite | Level 9

Thanks Eric for the suggestion.

The date record in my file actually is a text format, I have no idea why SAS read as numeric and interpret differently (wrong) when output as CSV file.

Erico
Calcite | Level 5

Hi Ursula,

As others have pointed out, this is for more likely an Excel problem than a SAS problem.

I suggest you look at the csv file in a plain text editor such as Notepad, not Excel, to see exactly what SAS is putting out.

In Excel (at least on my computer), if I enter 11/2007 in a cell, Excel automatically converts it to the date value for November 1, 2007, and displays Nov-07. As Eric@Stats suggests, you can format this with the custom format mm/yyyy so that it displays as 11/2007, but the internal value is still the date value for 11/1/2007, so if you later transfer the data to another program or another format, you may not get what you want. Essentially, Excel corrupts the data when it imports it.

Eric

ursula
Pyrite | Level 9

Thank you Erico and Eric.

I have 2 versions output the file into csv, the first output shows the date as 7-Nov, and the second output is Nov-07, I don't know why it came out different output with the same procedure.

I have tried to do custom format the date, it looks good for Nov-07 (format mm/yyyy -- 11/2007), but not the 7-Nov. I tried different format for 7-Nov, but it came out year as 2015.

DanielMK
Calcite | Level 5

Hi Ursula,

When a CSV file is opened in Excel, Excel tries to guess the data type of each cell.  If the cell only contains numbers and some symbols (e.g., /) it will interpret it as a date.  This is regardless of the data type of your original SAS dataset.  Excel, however, does not support incomplete dates so it also tries to complete your dates, and sometimes it does it right and sometimes not.  On top of that, once the date is complete, Excel applies a format to the cell to display the date in a certain way.  You can't change how Excel treats dates, so depending on what your end goal is, I would suggest one of the following two options:

1-In your SAS dataset, i.e., before exporting as CSV, add an apostrophe/single quote (') before the incomplete date, and then make sure this symbol appears in the CSV file.  When Excel opens the CSV file, it will interpret the cell as text and will show exactly what you want.  However, you won't be able to do calculations or sorting in Excel with this field.

2-In your SAS dataset, complete the date (e.g., '03/2015' should be '03/01/2015' or '01-Mar-2015') and then convert it to a date field with the input function.  Excel will consistently import a complete date correctly.

I hope this helps,

Daniel

ursula
Pyrite | Level 9

Thanks to all of you who are responding to my question.

SAS communities would not be success without your support!

Have a great day!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1215 views
  • 6 likes
  • 5 in conversation