如何使字符变量等于任意 SAS 格式的数值变量的格式化值?
如果我有一个具有格式的数字变量,有没有办法将格式化值作为字符变量获取?
例如,我想编写如下内容以将 10/06/2009
打印到屏幕上,但没有 putformatted()
函数。
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
恰好具有的任何格式。)
If I have a numeric variable with a format, is there a way to get the formatted value as a character variable?
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;
(Obviously I can write put(i, ddmmyy10.)
, but my code needs to work for whatever format i
happens to have.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
VVALUE
函数使用与变量关联的格式来格式化传递给它的变量。以下是使用VVALUE
的代码:虽然 cmjohns 解决方案比此代码稍快,但此代码更简单,因为不涉及宏。
The
VVALUE
function formats the variable passed to it using the format associated with the variable. Here's the code usingVVALUE
:While cmjohns solution is slightly faster than this code, this code is simpler because there are no macros involved.
使用
vformat()
函数。Use
vformat()
function.这似乎对我尝试过的一对夫妇有效。我使用 VARFMT 和宏函数来检索给定变量的格式。
这给了我:
This seemed to work for a couple that I tried. I used VARFMT and a macro function to retrieve the format of the given variable.
This gave me:
我可以使用宏代码和
sashelp.vcolumn
来完成此操作,但有点繁琐。I can do this with macro code and
sashelp.vcolumn
but it's a bit fiddly.是的,有一个 putformatted() 函数。实际上有两个:putc() 和 putn()。 putc 处理字符格式,putn() 处理数字。您的代码需要查看格式名称(所有且仅以“$”开头的字符格式)来确定使用哪个格式。以下是 putc 的语法(来自交互式帮助):
参数
Yes, there is a putformatted() function. In fact, there are two: putc() and putn(). Putc handles character formats, putn() numeric. Your code will need to look at the format name (all and only character formats start with "$") do determine which to use. Here is the syntax of putc (from the interactive help):
Arguments