Search code examples
sqlsas

SAS Proc SQL what is the datetime function resolution time?


In the following piece of code:

proc sql;
    select 
        datetime() format = datetime22.5, 
        datetime() format = datetime22.5, 
        case
            when datetime() = datetime() then 'equal'
            else 'no equal'
        end
    from data1 (obs=1);
quit;

Is there any option that all the datetime() calls will resolve to a slightly different timestamp?

In datastep all instructions are run one after the other so putting multiple datetime() calls in follow up lines ends up returning timestamps differing on decimal positions. Is there any similar risk in SQL call or are all of the datetime() calls resolved at the very same moment?

I want to confirm that putting multiple datetime() calls in a single SQL step will not resolve in milliseconds differences.


Solution

  • If you want to ensure that datetime() does not result in millisecond differences, create a datetime macro variable that holds the value before you run SQL (or embed it directly). For example:

    %let now = %sysfunc(datetime());
    
    proc sql;
        create table want as
            select &now as datetime format=datetime., *
            from sashelp.cars;
    quit;
    
    datetime                Make     Model
    09APR2024:13:27:35.473  Acura    MDX
    09APR2024:13:27:35.473  Acura    RSX Type S 2dr
    09APR2024:13:27:35.473  Acura    TSX 4dr
    ...                     ...      ...