How to extract datepart from macro variable
%let dt1='25APR2022:00:00:00.0000';
DATA WANT;
FORMAT DT1 MMDDYY10.;
START_DT=DATEPART("&DT1."d);
RUN;
Issue:
Output should be 04/25/2022, but displays as 01/01/1960.
Pls help
Hi:
I think the basic issue is that you're telling SAS the value is a date constant with the "&macvar"d but you should be telling SAS That it is a datetime constant using the "&macvar"dt specification. Do you want a DATA step variable or do you just want another macro variable? You could just do the DATEPART inside of a %SYSFUNC if you just needed a macro variable:
Note how the values can be created as macro variables using %LET or can also be used in a DATA step program. Either way, to use DATEPART with a datetime value, you need to use the dt, not just the d for the constant.
Cynthia
Hi:
I think the basic issue is that you're telling SAS the value is a date constant with the "&macvar"d but you should be telling SAS That it is a datetime constant using the "&macvar"dt specification. Do you want a DATA step variable or do you just want another macro variable? You could just do the DATEPART inside of a %SYSFUNC if you just needed a macro variable:
Note how the values can be created as macro variables using %LET or can also be used in a DATA step program. Either way, to use DATEPART with a datetime value, you need to use the dt, not just the d for the constant.
Cynthia
Thank you so much for your time and effort. Really appreciated.
Your code illustrates two interesting points about the flexibility of specifying a DATE constant.
But your actual problem is that you used the DATEPART() function on a value that was already a DATE value. Since the DATEPART() function is essentially just dividing the number of second by 86000 seconds per day to get a number of days the result of that when applied to any reasonable date value is going to end as zero, the base date value that SAS uses to number days, which is 01JAN1960.
So either convert the string directly into a DATE literal.
START_DT="&DT1."d;
Or convert it into a DATETIME literal, and then you can later convert the number of seconds into number of days.
START_DT=DATEPART("&DT1."dt);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.