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;
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.
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.