Conversion of SAS Macro Variable to time stamp

11,542

Solution 1

That is not a DATETIME, that is a DATE format (to INPUT, which depends on the incoming data, not the outgoing). You also need to remove the quotes, SYSFUNC treats quotes as characters, not as string delimiters.

%let date = 03/15/2013;
%put %sysfunc(inputn(&date,MMDDYY10.));

To actually create the datetime, you need to use PUT:

%let date = 03/15/2013;
%put %sysfunc(putn(%sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0)),datetime26.));

However, the better way to do this if you can is to use a date constant...

%let date=15MAR2013;
%put "&date."d;

Solution 2

Joe is mostly correct. If you want a datetime string of midnight 3/15/13, then use

%let date = 03/15/2013;
%put %sysfunc(putn(%sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0)),datetime26.));

Just using PUTN on a date string to "convert" a date to datetime will convert the number of days from epoch (01JAN1960) to the number of seconds from epoch.

Solution 3

My preference for working with dates in macro variables is to store the actual numeric value in the macro variable, and if I need to view/print the formatted value then assign a format to it on the fly:

%let date = %sysfunc(mdy(3,15,2013));
%put %sysfunc(putn(&date,date9.));

That allows you to use it in comparisons like the below (which I find is the most common task):

data xx;
  set something;
  where datefield = &date;
run;
Share:
11,542

Related videos on Youtube

LonelySoul
Author by

LonelySoul

Still wondering whether Robot will surpass us and make us their slave...

Updated on September 18, 2022

Comments

  • LonelySoul
    LonelySoul over 1 year

    I am trying to convert SAS Macro variable to timestamp and stumped while conversion. The code which I am using is given below.

    %let date = '03/15/2013';
    %put %sysfunc(inputn(&date,datetime26.6));
    

    Error which I am getting is

    WARNING: Argument 1 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range. NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set to a missing value.

    Please let me know if someone knows answers to this.

  • LonelySoul
    LonelySoul over 10 years
    Thanks DomPazz . The trick of "DHMS" is new to me and obviously both of you are very correct. Unfortunately I am getting dates from a DB which enforces this.
  • Joe
    Joe over 10 years
    Eep, that's what I get for answering during a boring IT conference call and not testing. Thanks for the catch.
  • Robert Penridge
    Robert Penridge over 10 years
    @DomPazz Hi Dom - %sysfunc supports 2 arguments, the second of which will apply a format to the result. This means you don't need to call putn() or putc() to format the result anymore. I think it was added in version 9.x... e.g. %put %sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0),datet‌​ime26.);
  • DomPazz
    DomPazz over 10 years
    @RobPenridge good to know! I think I have seen that used but I am still a little old school on some things like that.