SAS Date & Time – How to Read Date & Time in SAS

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

Today we will learn What is SAS Date Time, how to display Time & Date in SAS Programming. Like we discussed numeric and character data types earlier, dates in SAS is a special case of numeric values. Here, we are going to study SAS Date as a character variable and informats and SAS Date Formats.

So, let’s start How to Read/Display SAS Date & Time.

What is a SAS Date?

Unlike dates in other languages, dates in SAS have a specific numeric value assigned to each day. All SAS dates begin from January 1st, 1960 and this date is taken as day zero (0).

All previous and subsequent dates are therefore represented by keeping this date in mind with numeric values plus (+) or minus (-) from this starting point. For example-

December 29, 1959      -3
December 31, 1959      -1
January 1, 1960            0
January 3, 1960            2
January 5, 1960            4
January 23, 1963          1118
Oct 21, 2008                17826

How to Read and Display Date and Time in SAS

We know that SAS has a different mechanism for understanding SAS date. Let us see how we can resolve this issue.
Let us consider this example

DATA Employee_Info;
input Emp_ID Emp_Name$ Emp_Vertical$ DOJ;
datalines;
101 Mak SQL 18/08/2013
102 Rama SAS 25/06/2015
103 Priya Java 21/02/2010
104 Karthik Excel 19/05/2007
105 Mandeep SAS 11/09/2016
;
Run;
PROC PRINT DATA=Employee_Info;
Run;

 

The output of the above code in SAS will look like this

 

SAS Date

How to Read and Display Date and Time in SAS

We see in the above code that the DOJ column remains empty, this was because we specified the DOJ variable as numeric but it had special characters (/) in it, so SAS could not understand it and displayed an error message in the log window.

How can we resolve this? There are two ways of doing so-

a. Defining SAS Date as a Character Variable

If we put a $ sign at the end of the date variable, SAS would accept it as a variable of character data type. So know our code looks like this-

DATA Employee_Info;
input Emp_ID Emp_Name$ Emp_Vertical$ DOJ$;
datalines;
101 Mak SQL 18/08/2013
102 Rama SAS 25/06/2015
103 Priya Java 21/02/2010
104 Karthik Excel 19/05/2007
105 Mandeep SAS 11/09/2016
;
Run;
PROC PRINT DATA=Employee_Info;
Run;

 

The output of the above code will now look like-

 

SAS Date

SAS Date as a Character Variable

 

Another way of doing this is through the use of informats and formats like we saw in earlier tutorials.

b. Using SAS Informats and Formats

We are going to study SAS Formats and informats through this example. Let us look at the same example used before-

DATA Employee_Info;
input Emp_ID Emp_Name$ Emp_Vertical$ DOJ;
INFORMAT DOJ ddmmyy10.;
datalines;
101 Mak SQL 18/08/2013
102 Rama SAS 25/06/2015
103 Priya Java 21/02/2010
104 Karthik Excel 19/05/2007
105 Mandeep SAS 11/09/2016
;
Run;
PROC PRINT DATA=Employee_Info;
Run;

 

In the above code, we added a SAS date informat. Now the SAS system will display an output something like shown below, because like mentioned before, it will process dates from Jan 1960.

 

SAS Informats and Formats

SAS Informats and Formats

 

Now to display our SAS date correctly, we will use a format. Let us see how-

 

DATA Employee_Info;
input Emp_ID Emp_Name$ Emp_Vertical$ DOJ;
INFORMAT DOJ ddmmyy10.;
FORMAT DOJ ddmmyy10.;
datalines;
101 Mak SQL 18/08/2013
102 Rama SAS 25/06/2015
103 Priya Java 21/02/2010
104 Karthik Excel 19/05/2007
105 Mandeep SAS 11/09/2016
;
Run;
PROC PRINT DATA=Employee_Info;
Run;

The output of the above code will look like-

 

SAS Formats and Informats

SAS Formats and Informats

This was all about SAS Date Time Tutorial. Hope you like our explanation.

Conclusion

Hence, unlike other languages, SAS Programming Language has a different mechanism for understanding dates. Today we learned two interesting methods SAS Date and SAS Time, how can we display/read SAS Date time, and how SAS date is defined as a character variable.

At last, we SAS how can we use SAS date formate & informates. Wasn’t this interesting? For any queries, post your doubts in the comments section below. Stay tuned for more interesting topics in SAS.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

1 Response

  1. Kushal says:

    2 outputs given here are wrong. Please correct them

Leave a Reply

Your email address will not be published. Required fields are marked *