Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Sentry Page Protection
Please Wait...
Handling Date Values [7-7]


Datetime Variable
A datetime variable is a numeric variable that stores both the date and time data.

​Let's look at an example.

The CLINIC data set contains a list of clinic visit dates:
Picture

The VISITDATE column is a datetime variable that stores both the date and time of the clinic visit.

For example, the date of the first visit is January 1, 2019 at 10:00 am:
Picture


Defining a Datatime Variable

A datetime variable can be defined using a datetime constant.

​Example
data example;

format app_dt deadline_dt datetime.;

app_dt = '05MAY2019/18:30:07'dt;
deadline_dt = '01MAY2019/12:30:00'dt;

run;

The APP_DT is the date the application was submitted.

The DEADLINE_DT is the application deadline.

Both are created using the datetime constant:
Picture

The variables are assigned the (datetime.) format as well:
Picture

The full datetime values are displayed for both variables:
Picture


Comparing Datetime Variables

Similar to the date and time variables, we can compare two datetime variables to get the correct sequence of the events.

Example
data example;

format app_dt deadline_dt datetime.;
app_dt = '05MAY2019/18:30:07'dt;
deadline_dt = '01MAY2019/12:30:00'dt;

if app_dt > deadline_dt then Late_flag = "Y";
else Late_flag = "N";


run;

In this example, we checked whether the application was submitted after the deadline and created a LATE_FLAG variable that indicates whether the application was submitted late.
Picture

It turns out that the application was indeed submitted late:
Picture


DATEPART and TIMEPART Functions

The DATEPART and TIMEPART functions allow you to extract the date and time value from a datetime variable.

Let's look at an example.
data example;

format app_dt datetime.;
app_dt = '05MAY2019/18:30:07'dt;

format date date9. time time8.;

date = datepart(app_dt);
time = timepart(app_dt);


run;

The DATE and TIME variables are created using the DATEPART and TIMEPART functions, respectively, on the APP_DT variable:
Picture

The date and time of the application date are extracted into the DATE and TIME variables:
Picture


DHMS Function

The DHMS function allows you to combine the date, hours, minutes and seconds into a datetime variable.

Let's look at an example.
data example2;

format date date9.;
date = '15AUG2018'd;
hour = 8;
minute = 30;
second = 45;


format datetime datetime.;
datetime = dhms(date, hour, minute, second);

run;

In this example, the DATE, HOUR, MINUTE and SECOND columns are created and they are used in the parameters of the DHMS function:
Picture

The DATETIME variable is created based on the data from the DATE, HOUR, MINUTE and SECOND variables:
Picture


Common Usage of the DHMS Function

The DHMS function is commonly used to combine the date and time variables.

Let's look at an example.
data example3;

format visitdate date9. visittime time8.;
visitdate = '10NOV2019'd;
visittime = '08:00:00't;


format visitdt datetime.;
visitdt = dhms(visitdate, 0, 0, visittime);

run;

In this example, we have defined the VISITDATE and VISITTIME variables:
Picture

We have also created the VISITDT variable using the DHMS function with the VISITDATE and VISITTIME variables:
Picture

Please note that you only have to specify the first and the fourth parameters of the DHMS function when you are combining the date and time variables.

The second and third parameters (i.e. hours and minutes) can be left as zero:
Picture

The VISITDATE and VISITTIME variables are combined into the VISITDT datetime variable:
Picture

Exercise

Copy and run the code from the yellow box below:

The SALES data set contains two variables:
  • CUSTNO
  • ORDERDT

The data set contains the list of sales orders in January 2018.

A promotion was launched between January 20 to January 26 in 2018.

Create a new data set called SALES2 that contains the list of orders during the promotion period.
Next

Need some help? 


HINT:
The DATEPART function can be used to extract the date from the datetime variable.


SOLUTION:

data sales2;
set sales;
format date date9.;
date = datepart(orderdt);
if '20JAN2018'd <= date <= '26JAN2018'd;
run;

proc means data=shop2 sum maxdec = 0;
var quantity_rem;
run;


Fill out my online form.
Already a member? Go to member's area.