Reading Data Values Separated by Commas or Other Delimiters - SAS Programming

A fairly common practice is to separate adjacent data values with a comma (,).These comma-delimited data can be easily read by a SAS program as long as you tell the program what to expect.In this case you do so through the use of an INFILE statement as follows(output would be identical to the previous output, with the exception of the title):

Example

DATA COMMAS;
INFILE DATALINES DLM=',';
INPUT ID HEIGHT WEIGHT GENDER $ AGE;
DATALINES;
1,68,144,M,23
2,78,202,M,34
3,62,99,F,37
4,61,101,F,45
;
PROC PRINT DATA=COMMAS;
TITLE 'Example 2.1';
RUN;

As you will see later in this chapter,an INFILE statement is usually used to indicate that the raw data are stored in,and are being read from,an external file.The location,or source,or file specification of the external data is named in the INFILE statement, and a number of options that control how the data are read can also be included.

By using the reserved filename DATALINES,you can apply some of these options to instream data.(Note:the older term CARDS still works even if you use a DATALINES statement to begin your data.)In the present example, the option DLM=','tells the program to use commas rather than spaces as data delimiters.You may choose any data delimiter you wish with this option.You can even choose multiple characters such as DLM='XX' for your delimiter.

An improvement to the DLM= option was made available in Release 6.07 of the SAS System.A new option, DSD,allows you to treat two consecutive deli miters as containing a missing value.In addition,you can read a text string that contains the delimiter if it is contained in quotes. Further,quoted text strings can also be read.

In both cases,the quotes surrounding the text string are not included in the stored value.If the DSD option is used without the DLM= option,the SAS System assumes that you are using commas as your delimiter.The following program demonstrates the use of the DSD option:

Example

DATA COMMAS
INFILE DATALINES DSD;
INPUT X Y TEXT S;
ATALINES;
1,2,XYZ
3, ,STRING
4,5,"TESTING"
6, ,"ABC,XYZ"
; PROC PRINT DATA=COMMAS;
TITLE ' Example 2.2':
RUN;

Output from Example - Reading Data Values Separated by Commas or Other Delimiters]

OBS X Y TEXT
1 1 2 XYZ
2 3 . STRING
3 4 5 TESTING
4 6 . ABC,XYZ

Notice that the SAS System treats the consecutive commas as containing a missing value,omits the quotes from the data values,and allows you to include a comma in a text string.The DSD option is probably most useful in reading files produced by spreadsheet and database programs that produce DIF (data interchange format)files.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics