BookmarkSubscribeRSS Feed
CathyVI
Pyrite | Level 9

Hi,

I am trying to export to csv file. I have a date in Date9. format e.g., 

dob

01APR2004

I want to keep as it is but SAS is given me:  

dob
1-Apr-04

I have tried multiple codes but my excel output is still the same. I check the output in notepad and my results is still in DATE9. but different in excel.

Here are my code that I tried but the csv export did not provide date9. that I wanted.

Code 1:
data final_1;  
set final;
keep DOB ; 
format dob date9.;
run;

Code 2: 
proc sql;
	create view final_1 as 
select put(DOB,DATE9.) as DOB
from final;
quit;

PROC EXPORT DATA=final_1 OUTFILE= "test/final_1.csv" 
DBMS=csv REPLACE;
RUN;

 

 

2 REPLIES 2
ballardw
Super User

Welcome to the wonderful world of "Excel knows more that you do about how your data should appear."

 

The conversion is happening by Excel to the text file.

 

To force an appearance you will have to write to a different file structure instead of CSV to bypass the text to Excel conversion instructions in Excel

Instead of Proc Export try using ODS EXCEL output.

ods excel file="<your path>/test_1.xlsx";

proc print data=final_1;
format dob date9.;
run;

ods excel close.

Or get used to changing the appearance in Excel.

 

 

Tom
Super User Tom
Super User

Your asking on the wrong forum.

 

But seriously the issue is how EXCEL is interpreting the file.  If you really need control how EXCEL is going to read your data then you should make an XLSX file instead of a CSV file.  CSV files do not have any place to store information about variable types or display formats. A CSV file is just a text file.  

 

Both of your methods should produce the same file.  Try a simple example and actual look at the CSV file (don't let EXCEL open it and change it first just look at at with any text editor).

 

Try this test:

filename csv 'myfile.csv';
data test;
  do date=today() to today()+3;
    datestring=put(date,date9.);
    output;
  end;
  format date date9.;
run;

proc export data=test dbms=csv file=csv ;
run;

data _null_;
  infile csv ;
  input;
  list;
run;

So running that code today I created a CSV file that looks like this:

date,datestring
22FEB2024,22FEB2024
23FEB2024,23FEB2024
24FEB2024,24FEB2024
25FEB2024,25FEB2024

So there is no difference between the actual date value formatted to print with DATE9. format and the character variable with dates already formatted in the style that the DATE9. format produces.  So it makes sense the Excel will treat both columns the same since in the CSV file they ARE the same.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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