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 [2-7]


Date Informat
A date informat can be used to read date values into a SAS data set.

Let's look at an example.
data test;
input Num_var 
      Char_var $ 
      date_var
;
datalines;
1 ABC 20SEP2000
2 DEC 21SEP2000
3 KLM 22SEP2000
;
run;

In this example, we are creating a new TEST data set that has three variables:
  • NUM_VAR
  • CHAR_VAR
  • DATE_VAR

The DATE_VAR column is defined as numeric, and it should capture the three date values:
Picture

However, the DATE_VAR column is missing all of its observations.
Picture

Why does the DATE_VAR variable fail to capture the date values?

The date values listed under datalines are in the format of 'DDMMMYYYY' (e.g. 20SEP2000).

SAS treats these as character values because the values contain characters (i.e. 20SEP2000)!

As a result, SAS fails to read these 'character' values into a numeric variable.

In order for us to read the date values properly, we have to tell SAS these are date values instead of character values.

This can be done using an informat.

Example
data test;
input Num_var 
      Char_var $ 
      date_var : date9.;
datalines;
1 ABC 20SEP2000
2 DEC 21SEP2000
3 KLM 22SEP2000
;
run;

In this example, we assigned the informat of (date9.) to the DATE_VAR variable.
Picture

This tells SAS the data being read for this variable are date values in the format of DDMMMYYYY.

SAS will convert character-values into the corresponding date values in SAS:
Picture

The DATE_VAR variable contains numeric values, such as 14873 and 14874, etc.

We are going to assign a proper date format to the DATE_VAR variable so that the dates are displayed in a readable form.

Example
data test;
input Num_var 
      Char_var $ 
      date_var : date9.;
format date_var date9.;
datalines;
1 ABC 20SEP2000
2 DEC 21SEP2000
3 KLM 22SEP2000
;
run;

The (date9.) informat is assigned to the DATE_VAR variable.

The variable is now displayed in the format of DDMMMYYYY:
Picture

Exercise

The code below reads the NAME, GENDER and DOB variables into the DEMO data set.

Assign an appropriate informat to the DOB variable so that the values are read and displayed properly in SAS.
data demo;
input name $
      gender $
      DOB;
datalines;
Mary F 01JAN1960
Tina F 09FEB1965
John M 15MAY1968
;
run;
Next

Need some help? 


HINT:
The date values are formatted as 'DDMMMYYYY'. The appropriate informat is (date9.).


SOLUTION:

data demo;
input name $
gender $
DOB : date9.;
format DOB date9.;
datalines;
Mary F 01JAN1960
Tina F 09FEB1965
John M 15MAY1968
;
run;


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