BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
West26
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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:

Cynthia_sas_0-1654476929927.png

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

View solution in original post

4 REPLIES 4
Cynthia_sas
SAS Super FREQ

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:

Cynthia_sas_0-1654476929927.png

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

West26
Obsidian | Level 7

Thank you so much for your time and effort. Really appreciated.

Tom
Super User Tom
Super User

Your code illustrates two interesting points about the flexibility of specifying a DATE constant.

  • You can have extra quotes in the value.
  • You can include a time part string that will be ignored when calculating a DATE 

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);

 

West26
Obsidian | Level 7
Thank you for explaining in such a great detail. It is really helpful.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1667 views
  • 2 likes
  • 3 in conversation