2012-10-02 16 views
1

考虑以下几点:为什么MAX函数去掉proc sql中的SAS变量格式?

data; 
format x datetime19.; 
x=datetime(); 
flag='FIRST'; 
do x=datetime() to x+10 by 1; 
    output; 
    flag=''; 
end; 
proc sql noprint; 
select x into : test1 from &syslast where flag='FIRST'; 
select max(x) into: test2 from &syslast; 
%put now we see that &test1 is in a different format to &test2; 

data _null_; set; 
put x=; /* formatted */ 
call symput('test3',x); 
call symput('test4',max(x,sum(x+1000))); 
stop; 
run; 
%put The data step is more consistent - &test3 = &test4; 

似乎不一致给我。为什么proc sql在这种情况下保留格式?此行为是否记录在案?

回答

3

SAS无法知道如何格式化函数的结果。在这种情况下,你的max()函数只是返回一个日期时间,但是如果它里面有嵌套函数或算术的话。因为SAS只是将它视为一个全新的变量,默认情况下它没有设置格式。如果你想相同的格式适用于它,你可以更改您的代码如下:

select max(x) format=datetime19. into: test2 from &syslast; 
+0

此外 - 你可以修改理查德宏(http://www.devenezia.com/downloads/sas/macros/index.php?m=samelabl)将格式从一个数据集复制到另一个数据集。 –

+0

功能 - 有道理。解释数据步骤结果(symput函数) –

2

在您的例子中,MAX函数创建一个新列,并创造新的列时,你需要指定所有列属性。因此,只需添加一个FORMAT子句SELECT语句:

proc sql noprint; 
    select x into : test1 from &syslast where flag='FIRST'; 
    select max(x) format=datetime19. into: test2 from &syslast; 
quit; 
%put now we see that &test1 is in a different format to &test2;