When using ods excel statement stop Excel converting numbers saved as character

▼魔方 西西 提交于 2021-02-11 13:31:55

问题


I need to export some results from SAS to Excel. So far, I was using a simple proc export for that but since the export got more complex (multiple proc results and data sets need to be mapped to one sheet) I need to find an alternative. It seems,ods excel is the way to go - and I agree, it is highly customizable - but I am facing a problem where I am not sure whether it is SAS or Excel related.

Problem:

When I have a SAS data set with character columns, where the data are strings of digits separated by points the ods export to Excel will always convert this data to a number format and which basically makes the data unreadable. For example:

character in SAS "04.2000" will become "4,2" in Excel (Standard format)

MWE:

data test;
input date $;
datalines;
04.2000
10.2001
; 

ods excel file="date.xlsx";
proc print data=test;
run;
ods excel close;

The proc export prints this data correctly, without automatic conversion. Is this something on the SAS or rather Excel side? What can I do to get the same result as in proc export when using ods excel, any ideas?


回答1:


To absolutely force the <month>.<year> date representation value to be a string in Excel you render it as a formula. The value will not change, even after 'null' editing the cell (F2,Enter).

Example:

data test;
input datestring $;
forcedate = '="' || datestring || '"';
datalines;
04.2000
10.2001
; 

ods excel file="date.xlsx";
proc print data=test;
run;
ods excel close;

You can apply a $CHAR<n> format to your date string. The field will still be interpreted as a number in Excel if you manually edit the cell.

Example:

data test;
input datestring $;
format datestring $char7.;
datalines;
04.2000
10.2001
; 

ods excel file="date.xlsx";
proc print data=test;
run;
ods excel close;

Excel (image)

You can also use the ODS EXCEL style option tagattr='type:String' to force the initial cell rendering, however upon editing (F2, Enter) Excel will again numerize the cell value.

Example:

proc print data=test;
  var datestring / style=[tagattr='type:text'];

* String also works;
* var datestring / style=[tagattr='type:String'];
run;



回答2:


For example, you can use format. it's work fine:

data test;
input date $;
datalines;
04.2000
10.2001
; 
run;
ods excel file="c:\temp\date.xlsx";
proc print data=test;
format date $20.;
run;
ods excel close;



回答3:


If you use PROC REPORT you can likely do this through your DEFINE statement.

define date/ 'Date' format = $20.  style(column)=[tagattr="type:text"];


来源:https://stackoverflow.com/questions/62342380/when-using-ods-excel-statement-stop-excel-converting-numbers-saved-as-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!