SAS: Converting character to numeric variable - comma as a decimal separator

烂漫一生 提交于 2019-12-10 16:24:49

问题


I'm trying to use INPUT function, as it is always suggested, but it seems that SAS has some problems with proper interpretation of amounts like: 2,30 1,61 0,00 ...and I end up with missing values. Perhaps it's caused by comma being thousands separator where SAS come from ;)

data temp;
    old = '1,61';
    new = input(old, 5.2);
run;

Why the result of above is new = .?

It seems that I've found some work-around - by replacing comma with a period using TRANWRD before INPUT function is called (vide code below), but it's quite ugly solution and I suppose there must be a proper one.

data temp;
    old = '1,61';
    new = input(tranwrd(old,',','.'), 5.2);
run;

回答1:


The reason new = . in your example is because SAS does not recognize the comma as a decimal separator. See the note in the log.

NOTE: Invalid argument to function INPUT at line 4 column 11. old=1,61 new=. ERROR=1 N=1 NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.

The documentation contains a list of various SAS informats. Based on the documentation it looks like you can use the COMMAX informat.

COMMAXw.d - Writes numeric values with a period that separates every three digits and a comma that separates the decimal fraction.

The modified code looks like this:

data temp;
    old = '1,61';
    new = input(old,commax5.);
run;

proc print;

The resulting output is:

Obs    old      new

 1     1,61    1.61

If you want to keep the new variable in the same format you can just add the statement format new commax5.; to the data step.

Thanks to Tom for pointing out that SAS uses informats in the INPUT() function.



来源:https://stackoverflow.com/questions/48114558/sas-converting-character-to-numeric-variable-comma-as-a-decimal-separator

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