如何使字符变量等于任意 SAS 格式的数字变量的格式化值? [英] How can I make a character variable equal to the formatted value of a numeric variable for arbitrary SAS formats?
问题描述
如果我有一个带有格式的数字变量,有没有办法将格式化的值作为字符变量获取?
If I have a numeric variable with a format, is there a way to get the formatted value as a character variable?
例如我想编写类似下面的内容来将 10/06/2009
打印到屏幕上,但没有 putformatted()
函数.
e.g. I would like to write something like the following to print 10/06/2009
to the screen but there is no putformatted()
function.
data test;
format i ddmmyy10.;
i = "10JUN2009"d;
run;
data _null_;
set test;
i_formatted = putformatted(i); /* How should I write this? */
put i_formatted;
run;
(显然我可以编写 put(i, ddmmyy10.)
,但我的代码需要适用于 i
碰巧具有的任何格式.)
(Obviously I can write put(i, ddmmyy10.)
, but my code needs to work for whatever format i
happens to have.)
推荐答案
VVALUE
函数使用与变量关联的格式来格式化传递给它的变量.这是使用 VVALUE
的代码:
data test;
format i ddmmyy10.;
i = "10JUN2009"d;
run;
data _null_;
set test;
i_formatted = vvalue(i);
put i_formatted;
run;
虽然 cmjohns 解决方案比此代码稍快,但此代码更简单,因为不涉及宏.
While cmjohns solution is slightly faster than this code, this code is simpler because there are no macros involved.
这篇关于如何使字符变量等于任意 SAS 格式的数字变量的格式化值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!