Sentry Page Protection
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:
The DATE_VAR column is defined as numeric, and it should capture the three date values: However, the DATE_VAR column is missing all of its observations. 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. 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: 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: 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; Need some help?
HINT: SOLUTION: data demo;
Fill out my online form.
|