Tuesday, November 22, 2011

Formatting SAS Dates and Other Variables

Yesterday, I was approached by a coworker to convert dates using SAS that were in the following format:

yyyymmdd

My coworker desired that the data be displayed in this format:

mm/dd/yyyy

The data originated in a .txt file and was being imported using a data step and an input statement. Each field began at the same character on the line. Originally the data was coded to find the data within a range of characters on each line:


data test;
infile test.txt;
input C_O7 $575-579;
run;

However, when inputting code in this manner, it is not possible to designate the type of variable, preventing it from being converted to a date.

Instead, instruct SAS as to where the beginning of each column is, which allows you to specify a data type:


data test;
infile test.txt;
input @42 date yymmdd8.; /* This should match the way the original data is formatted */
C_O7 $575-579; /* You can mix and match how to find the data columns */
format date mmddyy10.; /* This is the format you would like it presented in */
run;

There are many different formats available, and SAS gives you pretty good control over how to format types like dates and currency.

No comments:

Post a Comment