为什么MAX函数在proc sql中剥离SAS变量格式?

为什么MAX函数在proc sql中剥离SAS变量格式?,sas,proc-sql,Sas,Proc Sql,考虑以下几点: 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; %p

考虑以下几点:

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保留该格式?是否记录了这种行为?

SAS无法知道函数结果应如何格式化。在本例中,您的
max()
函数只是返回一个日期时间,但如果其中有嵌套函数或算术,该怎么办。由于这个原因,SAS只是将其视为一个全新的变量,默认情况下没有设置格式。如果要对其应用相同的格式,可以将代码更改为:

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

SAS无法知道函数结果的格式。在本例中,您的
max()
函数只是返回一个日期时间,但如果其中有嵌套函数或算术,该怎么办。由于这个原因,SAS只是将其视为一个全新的变量,默认情况下没有设置格式。如果要对其应用相同的格式,可以将代码更改为:

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

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

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;

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

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;

此外,您还可以修改Richard的宏()以将格式从一个数据集复制到另一个数据集。该函数很有意义。还解释了数据步骤结果(symput函数),您可以修改Richard的宏()将格式从一个数据集复制到另一个数据集。该函数很有意义。还解释了数据步结果(symput函数)