如何使字符变量等于任意 SAS 格式的数值变量的格式化值?

发布于 2024-08-04 16:52:08 字数 416 浏览 6 评论 0原文

如果我有一个具有格式的数字变量,有没有办法将格式化值作为字符变量获取?

例如,我想编写如下内容以将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

林空鹿饮溪 2024-08-11 16:52:08

VVALUE 函数使用与变量关联的格式来格式化传递给它的变量。以下是使用 VVALUE 的代码:

data test;
  format i ddmmyy10.;
  i = "10JUN2009"d;
run;

data _null_;
  set test;
  i_formatted = vvalue(i);
  put i_formatted;
run;

虽然 cmjohns 解决方案比此代码稍快,但此代码更简单,因为不涉及宏。

The VVALUE function formats the variable passed to it using the format associated with the variable. Here's the code using VVALUE:

data test;
  format i ddmmyy10.;
  i = "10JUN2009"d;
run;

data _null_;
  set test;
  i_formatted = vvalue(i);
  put i_formatted;
run;

While cmjohns solution is slightly faster than this code, this code is simpler because there are no macros involved.

迷路的信 2024-08-11 16:52:08

使用vformat()函数。

/* test data */
data test;
  i = "10jun2009"d;
  format i ddmmyy10.;
run;

/* print out the value using the associated format */
data _null_;
  set test;
  i_formatted = putn(i, vformat(i));
  put i_formatted=;
run;
/* on log
i_formatted=10/06/2099
*/

Use vformat() function.

/* test data */
data test;
  i = "10jun2009"d;
  format i ddmmyy10.;
run;

/* print out the value using the associated format */
data _null_;
  set test;
  i_formatted = putn(i, vformat(i));
  put i_formatted=;
run;
/* on log
i_formatted=10/06/2099
*/
热鲨 2024-08-11 16:52:08

这似乎对我尝试过的一对夫妇有效。我使用 VARFMT 和宏函数来检索给定变量的格式。

 data test;
  format i ddmmyy10. b comma12.;
  i = "10JUN2009"d;
  b = 123405321;
run;


%macro  varlabel(variable) ;
  %let dsid=%sysfunc(open(&SYSLAST.)) ;
  %let varnum=%sysfunc(varnum(&dsid,&variable)) ;
  %let fmt=%sysfunc(varfmt(&dsid,&varnum));
  %let dsid=%sysfunc(close(&dsid)) ;
  &fmt
%mend varlabel;

data test2;
  set test;
  i_formatted = put(i, %varlabel(i) );
  b_formatted = put(b, %varlabel(b) );
  put i_formatted=;
  put b_formatted=;
run;

这给了我:

i_formatted=10/06/2009
b_formatted=123,405,321

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.

 data test;
  format i ddmmyy10. b comma12.;
  i = "10JUN2009"d;
  b = 123405321;
run;


%macro  varlabel(variable) ;
  %let dsid=%sysfunc(open(&SYSLAST.)) ;
  %let varnum=%sysfunc(varnum(&dsid,&variable)) ;
  %let fmt=%sysfunc(varfmt(&dsid,&varnum));
  %let dsid=%sysfunc(close(&dsid)) ;
  &fmt
%mend varlabel;

data test2;
  set test;
  i_formatted = put(i, %varlabel(i) );
  b_formatted = put(b, %varlabel(b) );
  put i_formatted=;
  put b_formatted=;
run;

This gave me:

i_formatted=10/06/2009
b_formatted=123,405,321
蓝天 2024-08-11 16:52:08

我可以使用宏代码和 sashelp.vcolumn 来完成此操作,但有点繁琐。

proc sql noprint;
  select trim(left(format)) into :format
    from sashelp.vcolumn
    where libname eq 'WORK' and memname eq 'TEST';
run;

data test2;
  set test;
  i_formatted = put(i, &format);
  put i_formatted;
run;

I can do this with macro code and sashelp.vcolumn but it's a bit fiddly.

proc sql noprint;
  select trim(left(format)) into :format
    from sashelp.vcolumn
    where libname eq 'WORK' and memname eq 'TEST';
run;

data test2;
  set test;
  i_formatted = put(i, &format);
  put i_formatted;
run;
2024-08-11 16:52:08

是的,有一个 putformatted() 函数。实际上有两个:putc() 和 putn()。 putc 处理字符格式,putn() 处理数字。您的代码需要查看格式名称(所有且仅以“$”开头的字符格式)来确定使用哪个格式。以下是 putc 的语法(来自交互式帮助):

PUTC(source, format.<,w>)

参数

source 
is the SAS expression to which you want to apply the format.

format. 
is an expression that contains the character format you want to apply to source.

w 
specifies a width to apply to the format. 

Interaction: If you specify a width here, it overrides any width specification
in the format. 

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):

PUTC(source, format.<,w>)

Arguments

source 
is the SAS expression to which you want to apply the format.

format. 
is an expression that contains the character format you want to apply to source.

w 
specifies a width to apply to the format. 

Interaction: If you specify a width here, it overrides any width specification
in the format. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文