BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

 

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.

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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.

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20

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
Patrick
Opal | Level 21

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. 

 

As for your concrete question:

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?

 

PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

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

--
Paige Miller
FreelanceReinh
Jade | Level 19

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.

Tom
Super User Tom
Super User
@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 ;

 

BrunoMueller
SAS Super FREQ

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

 

Bruno

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 4631 views
  • 6 likes
  • 7 in conversation