Monday, December 06, 2010

load delimited file with :

From here
If your data is longer than the default length, you need to use informats. For example, date or time values, names, and addresses can be longer than eight characters. In such cases, you either need to add an INFORMAT statement to the DATA step or add informats directly in the INPUT statement. However, when the informats are used in the INPUT statement, care must be taken to honor the function of the delimiter to prevent read errors. If you add informats in an INPUT statement, you must add a colon (:) in front of the informat, as shown in this example:



data a;
infile 'C:\sas\example2.csv' dlm='09'X dsd truncover;
input fname :$20. lname :$30. address1 :$50. address2 :$50. city :$40.
state :$2. zip phone :$12.;
run;

The colon indicates that SAS should read from the first character after the current delimiter to the number of characters specified in the Informat or to the next delimiter, whichever comes first. The colon also positions the input pointer for the next variable, if there is one.

When a file contains numeric variables or character variables with a length of eight characters or less, as shown in the following example, you do not need Informats to read in the data:

data a;
infile 'C:\sas\example1.csv' dlm='|' dsd truncover;
input fname $ lname $ age;
run;

In this example, the variables FNAME and LNAME are character variables and AGE is a numeric variable. All three variables have a length of 8 bytes, the default length, which is evident because no informats are specified.

No comments: