Solved
Valued Guide
Posts: 619

# Setting a DateTime with DatePart

This is similar to my last post I’m trying to understand date part usage.

How can I use datepart() to set a macro variable equal to:

%let StartYear = '1Jul&currentYear:0:0:0'dt;

The idea is to pass the current day month and year at the start of a day and make a date time.

Is there a good link for DatePart() documentation?  I’ve googled it a bit and most of what I see is in pieces.

Accepted Solutions
Solution
‎12-02-2015 12:46 PM
Posts: 1,256

## Re: Setting a DateTime with DatePart

[ Edited ]

As to the documentation: For SAS keywords I mostly use the built-in "SAS Help and Documentation" from the Help menu. The page on the DATEPART function is very brief there, as expected, since there is not much more to say than that it "extracts the date from a SAS datetime value."

That is, if you apply it to a value such as '01JUL2015:00:00'dt=1751328000, it will return 20270='01JUL2015'd.

What you want to do, looks rather like the converse of this. It is the DHMS function which computes the datetime value from a date value and hours, minutes and seconds:

With the above example: DHMS(20270,0,0,0)=1751328000. For the special case of (h,m,s)=(0,0,0) you actually don't need the DHMS function, because DHMS(x,0,0,0)=86400*x for all SAS date values x (as Patrick has pointed out already while I was typing this post).

For the "current day month and year" you could use the TODAY (alias DATE) function and obtain the corresponding datetime value "at the start of a day" as DHMS(TODAY(),0,0,0) or simply 86400*TODAY().

Your example suggests you want to specify day and month yourself and take the year from the current date, right? No problem with the MDY (month, day, year) and YEAR functions:

MDY(7,1,YEAR(TODAY()))=20270 (in 2015), so that 86400 times this is the desired datetime value.

To get this into a macro variable I would prefer a data-_null_ step over multiple applications of %SYSFUNC:

``````data _null_;
dt=86400*mdy(7,1,year(today()));
call symputx('StartYear',dt);
run;

/* is less cryptic than */

%let StartYear=%eval(86400*%sysfunc(mdy(7,1,%sysfunc(year(%sysfunc(today()))))));

/* not to speak of */

%let StartYear=%sysfunc(dhms(%sysfunc(mdy(7,1,%sysfunc(year(%sysfunc(today()))))),0,0,0));``````

Literally, your example aims at a character string "'1Jul ..." in macro variable StartYear, but I would strongly prefer the corresponding numeric datetime value instead.

All Replies
Super User
Posts: 5,884

## Re: Setting a DateTime with DatePart

datapart() takes the dat part from  a datetime value. Is that what you have, and in what form (macro variable, data set variable)?

There a lot of ways of manipulating SAS date/datetime values, both using functions and formats.

Data never sleeps
Posts: 4,736

## Re: Setting a DateTime with DatePart

SAS functions are fully documented here:

https://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#p0rttbu7w6...

Once you understand how SAS Dates and Datetimes work things become simple.

- A SAS Date value is nothing else than the number of days since 1/1/1960 - stored in a numeric variable

- A SAS Datetime value is nothing else than the number of seconds since 1/1/1960 - stored in a numeric variable

Without using a function converting a SAS Date to a SAS Datetime beginning of the day:

SAS_Datetime=SAS_Date * 86400; /* 86400: Number of seconds in a day *

There are quite a few SAS functions, formats and informats which help you dealing with dates.

%sysfunc() allows you to use SAS functions on macro level.

The idea is to pass the current day month and year at the start of a day and make a date time

datepart() converts a SAS datetime value to a SAS date value - you want to do the opposite.

You want to create and populate a macro variable containing some sort of datetime value. Can you please tell us:

- What do you have?

- What do you want?

- How do you intend to use this macro variable?

Posts: 3,061

## Re: Setting a DateTime with DatePart

UNTESTED CODE

%let startdate=%sysfunc(datepart(&startyear));

--
Paige Miller
Solution
‎12-02-2015 12:46 PM
Posts: 1,256

## Re: Setting a DateTime with DatePart

[ Edited ]

As to the documentation: For SAS keywords I mostly use the built-in "SAS Help and Documentation" from the Help menu. The page on the DATEPART function is very brief there, as expected, since there is not much more to say than that it "extracts the date from a SAS datetime value."

That is, if you apply it to a value such as '01JUL2015:00:00'dt=1751328000, it will return 20270='01JUL2015'd.

What you want to do, looks rather like the converse of this. It is the DHMS function which computes the datetime value from a date value and hours, minutes and seconds:

With the above example: DHMS(20270,0,0,0)=1751328000. For the special case of (h,m,s)=(0,0,0) you actually don't need the DHMS function, because DHMS(x,0,0,0)=86400*x for all SAS date values x (as Patrick has pointed out already while I was typing this post).

For the "current day month and year" you could use the TODAY (alias DATE) function and obtain the corresponding datetime value "at the start of a day" as DHMS(TODAY(),0,0,0) or simply 86400*TODAY().

Your example suggests you want to specify day and month yourself and take the year from the current date, right? No problem with the MDY (month, day, year) and YEAR functions:

MDY(7,1,YEAR(TODAY()))=20270 (in 2015), so that 86400 times this is the desired datetime value.

To get this into a macro variable I would prefer a data-_null_ step over multiple applications of %SYSFUNC:

``````data _null_;
dt=86400*mdy(7,1,year(today()));
call symputx('StartYear',dt);
run;

/* is less cryptic than */

%let StartYear=%eval(86400*%sysfunc(mdy(7,1,%sysfunc(year(%sysfunc(today()))))));

/* not to speak of */

%let StartYear=%sysfunc(dhms(%sysfunc(mdy(7,1,%sysfunc(year(%sysfunc(today()))))),0,0,0));``````

Literally, your example aims at a character string "'1Jul ..." in macro variable StartYear, but I would strongly prefer the corresponding numeric datetime value instead.

Super User
Posts: 8,125

## Re: Setting a DateTime with DatePart

[ Edited ]
DavidPhillips2 wrote:

This is similar to my last post I’m trying to understand date part usage.

How can I use datepart() to set a macro variable equal to:

%let StartYear = '1Jul&currentYear:0:0:0'dt;

The idea is to pass the current day month and year at the start of a day and make a date time.

Is there a good link for DatePart() documentation?  I’ve googled it a bit and most of what I see is in pieces.

You cannot directly use the DATEPART() function to create a datetime value, since the purpose of the DATETIME() function is do the opposite of that.

If you have the DAY, MONTH and YEAR in macro varaibles you can make a datetime literal directly from them. Note that the MONTH must be expressed as the three letter abbreviation of name of the month (JAN, FEB, etc.).

``````%let day=01;
%let month=JAN;
%let year=2015;
%let StartYear = "&day.&month.&year:00:00"dt ;``````

If you have the MONTH as the number of the month in the year (1,2,...,12) then you will need to use the MDY() function to create the date. In macro code you will need to wrap the function call in the macro function %SYSFUNC().  You can add a format to have it return the date in DATE9. format and then append the :00:00 to make it look like a datetime literal.

``````%let day=01;
%let month=01;
%let year=2015;
%let StartYear = "%sysfunc(mdy(&month,&day,&year),date9):00:00"dt ;``````

SAS Super FREQ
Posts: 825

## Re: Setting a DateTime with DatePart

Have a look at the DHMS function, it will create a datetime value from a date, hour, minute and second

Bruno

🔒 This topic is solved and locked.