Domanda

Consider the following:

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;

Seems inconsistent to me. Why does proc sql retain the format in this case? Is this behaviour documented?

È stato utile?

Soluzione

There's no way for SAS to know how the result of a function should be formatted. In this case your max() function is simply returning a datetime, but what if there were nested functions or arithmetic inside it. Because of this SAS just treats it as a brand new variable which by default has no format set. If you would like to apply the same format to it you can change your code to this:

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

Altri suggerimenti

In your example, the MAX function creates a new column and when creating new columns, you need to specify all column attributes. So, just add a FORMAT clause to your select statement:

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top