Read Generic Delimited Files Version 2, April 11, 1996 Charles Burchill, Shelley Derksen Manitoba Centre for Health Policy and Evaluation This macro will read in simple delimited ASCII files. Variables may be named, or labeled based on the delimited values in one of the records. This macro is a very simplistic program, it is not meant to replace the input statements normally used when reading flat files into SAS. Usage: _readdlm Options: delim= Delimiter that separates variables. Default delim=','. nvar= Number of variables to import, the number of should the same, or fewer than the number that exists in the data. vtype= Variable name type. default vtype=label name - Record with var names in import file is used to create variable names. label - Record with var in import file is used to define variable labels. none - If your data does not contain any labels this option may be used. Variable names, as with the label option, will be named var1, var2 ... var. varlist= Will allow you to define names for variables when vtype is label, or none. Default is missing. Must be a quoted string (varlist="region age sex pop") If a varlist is specified nvar will be calculated. vtype=name takes priority. ltype= Define how character variable lengths are set. Default ltype=$40. $xxx - where xxx is the length up to 200. fstobs - length is determined by the first record. firstobs= First record in import file that actually contains data. default firstobs=3 (comment, names, data). The line of labels, and any comments must be above this line. input= Input file (in quotes) or fileref. out= Output data set name. Input: The input file must be a delimited ASCII file. The delimiters may be any character that does not occur in the value of the variables (e.g. quote/comma separated files). There must be one record at the top of the data that contains the names, or labels for each variable. Each variable must be separated by the same delimiter. You may have any number of comment lines at the top of the file. Output: SAS data set containing the variables from the delimited file. Example of a quote comma file (file.txt): "This is a comment" "Region","Age","Gender","Pop" "A",5,1,556 "B",4,2,998 "C",6,1,4455 _readdlm input="file.txt" out=temp nvar=4 firstobs=3 ; NOTES: 1. Character variables are currently imported with a length of 40 by default - if a variable is longer than this it is truncated. This can be changed using the ltype= option. If you use ltype=fstobs then the length is determined by the variables in the first data record. 2. Numeric variables are currently imported with a length of 8. 3. Character variables are determined by the first data record. Any variables with a non-numeric character are defined as character. If a variable contains characters, but not in the first record it will be imported incorrectly. 4. This program is a companion to the _lotus macro when type=qc (default) or comma is specified. If the qc option was used, and there are missing values SAS may issue incompatible type errors. If type=comma is used with formated numerics the program may issue incompatible type errors. 5. If the original input data set that was used in the _lotus macro is available then it should be used instead of re-reading the data. 6. The type and length of imported data may not be the same as the original exported data. Example programs: _readdlm input='/dsd1/sderksn/rha/tables/thspa4.prn' vtype=none out=test ltype=fstobs varlist="region test test2" ; ** Note nvar is set to 3 ; _readdlm input='/dsd1/sderksn/rha/tables/thspa4.prn' vtype=label nvar=13 firstobs=3 out=test ; proc print data=test label ; run; _lotus input=phis.pop9394 output='junk.this' comment='testing' ; _readdlm input='junk.this' vtype=name nvar=4 firstobs=3 out=test ; proc contents data=test ; run;