Thursday, May 4, 2017

DATALINES (SAS)


In today’s article we will be discussing datalines, which is a SAS statement for reading in data. Typically, this statement is used to read in small data sets for testing. 

The standard template for the utilization of this statement is:

DATA <nameofthedataset>;
INPUT <VARA> <datatype> <VARB> <datatype> <VARC> <datatype>;
DATALINES;
VAR1 VAR2 VAR3
VARA VAR2 VAR3
;

RUN;

For example, let’s say that we wanted to read in data that refers to various individuals by first and last name to create a new SAS data set. You could utilize the following code to do so:

DATA Nameset;
Length FirstName $20 MiddleInt $5 LastName $50; /* Get into the habit of defining lengths */
INPUT FirstName $ MiddleInt $ LastName $;
DATALINES;
Bob J. Bobowski
Susan L. Patel
Roy M. Peterson
Jamie Q. Jones
;
RUN;


This would result in the creation of the SAS data set “Nameset”, with the following entries:


You also have the ability to utilize various read-in options while creating data sets through the usage of the DATALINES statement. The code for such would resemble:

DATA <nameofthedataset>;
<INFILE DATALINES> <INPUTOPTION>;
INPUT <VARA> <datatype> <VARB> <datatype> <VARC> <datatype>;
DATALINES;
VAR1, VAR2, VAR3
VARA, VAR2, VAR3
;
RUN;


Below is an example of code that utilizes the DELIMITER= option.

DATA Nameset;
INFILE DATALINES DELIMITER=','
Length FirstName $20 MiddleInt $5 LastName $50; /* Get into the habit of defining lengths */
INPUT FirstName $ MiddleInt $ LastName $;
DATALINES;
Bob, J., Bobowski
Susan, L., Patel
Roy, M., Peterson
Jamie, Q., Jones
;
RUN;


The data set data would remain the same, even though the input method differed.


Datalines is a quick and relatively painless method for inputting small SAS data sets. I would recommend familiarizing yourself with this particular concept, as it does come in handy from time to time.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.