SAS Formatted Input - Reading External text Files

When external file contains some values in some specific formats (e.g. birth-date in DDMONYYYY format, currency in $ format etc.) then we can use this method.

Here we need to specify informats of the data to be read (date9. ,dollar10. etc). Following example demonstrates this. Consider Following file to be read

SAS External File - emp.txt

1011 Rogger Jones 20JUL1978 $3,400

1012 Nicole Davis 17JAN1983 $3,000

1013 Rachel Brown 30APR1981 $3,600

1014 Michael Daniel 11SEP1979 $3,800

As one can see from extract of the given employee information, in 4th column employee birth date has been recorded in DDMONYYYY format which is recognized as date9. format in SAS. While 5th column records employee basic salary in dollar format.

Remember we don't want save those values as it is in character format, but we have to save those values in corresponding date and numeric format.

Now lets see how one can achieve reading this into SAS system.

SAS Code:

data emp_data;

/*infile "D:\emp.txt";*/

input emp_id emp_fname:$20. emp_lname:$20. emp_bdate:date9. emp_sal:dollar7.;

cards;

1011 Rogger Jones 20JUL1978 $3,400

1012 Nicole Davis 17JAN1983 $3,000

1013 Rachel Brown 30APR1981 $3,600

1014 Michael Daniel 11SEP1979 $3,800

run;

/* proc print without formats */

title 'proc print without formats';

proc print data=emp_data;

run;

/* proc print with applied formats */

title 'proc print with formats';

proc print data=emp_data;

format emp_bdate date9. emp_sal dollar7.;

run;

Following is the output for the code above. One can observe with first PROC PRINT statement without formats, how SAS systems stores the date values internally and salary value is also stored in plain numeric format.

But, by applying appropriate SAS formats for corresponding values we can get the values to be printed with corresponding formats

Output:

This kind of input statement is also called as list input as the variables to be read are listed one after another. However it is more specific a example of formatted input because we have supplied the instruction to SAS that in which format SAS should read the values.

The ':' sign mentioned between variable name and format name instructs SAS to go by the delimiting character instead of the format specified. To explain it more clearly; suppose for one character variable we provided the format $40. (i.e. the character value with length of 40) and if in reality it happens to be only of 25 characters then ':' character specified will tell SAS to truncate the value of the variable with 25 characters.

Otherwise SAS will capture the values from next adjacent varaiable to make the current vairable of length 40 but ultimately the columns are going to get mis-aligned.