Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท Reading and Writing Data

Reading Instream Data with DATALINES and INPUT

Learn the three styles of the SAS INPUT statement (list, column and formatted), pointer controls, DLM and DSD, MISSOVER and trailing @ for reading data typed into your program.

Before SAS can analyze anything, it must read the data. The simplest source is data typed directly into the program with DATALINES, which is perfect for small examples and tests. The INPUT statement describes how to cut each line into variables, and everything you learn here applies later to files too.

DATALINES and INPUT together

SAS
data pets;
   input name $ species $ age weight;
   datalines;
Rex dog 5 31.5
Mia cat 3 4.2
Bo dog 9 24
;
run;
  • input lists the variable names in the order they appear in each line. A $ after a name marks it as character; without it the variable is numeric.
  • datalines; starts the raw data. Every following line is one record until a line containing only a semicolon. (You may also write cards;, an older synonym.)
  • If the data itself contains semicolons, use datalines4; and end with ;;;;.

The step executes the input statement once per iteration of the DATA step loop, so each data line becomes one observation.

List input

By default, SAS uses list input: fields are separated by one or more blanks and read left to right. Its limitations matter:

  • Character values default to 8 characters and cannot contain spaces.
  • Missing values must be written as a period (.), not just left blank, otherwise the fields shift.
  • A value that is too long for a character variable is truncated at its length.

To lengthen a character variable, give a length in the input statement with a colon modifier:

SAS
input name :$20. city :$15. sales;

The colon tells SAS to use the format's width as a maximum but still read until the next delimiter. This is the most useful form of list input.

Column input

When fields always sit in the same columns, name the start and end columns:

SAS
data fixed;
   input id 1-3 name $ 4-13 score 14-16;
   datalines;
101Ana       95
102Ben       87
103Carla    100
;
run;

Column input needs no delimiters, can read values with embedded spaces, and can skip fields. You can also read columns in any order. Its weakness is that every line must line up exactly.

Formatted input

Formatted input reads a field using an informat (see Informats and Formats) and is the most flexible:

SAS
data sales;
   input @1 region $4. @6 date date9. @16 amount comma8.;
   datalines;
East 01MAR2024 1,250
West 15MAR2024   980
;
run;

@n moves the column pointer to column n. Informats such as date9. and comma8. convert text into the right numeric value. Without a date format on the output you will see the stored day number when you print, so add format date date9.; in the step.

You can mix styles within one input statement. A pointer moves with +n (skip n columns) and / (go to the next line), and #n selects a line number when each observation spans several lines.

Delimiters: DLM and DSD

Data separated by commas, tabs or other characters needs the infile statement even for in-stream data:

SAS
data csvish;
   infile datalines dlm="," dsd missover;
   input name :$12. city :$12. score;
   datalines;
Ana,"Paris, TX",91
Ben,Oslo,
Cara,,77
;
run;
  • DLM="," sets the delimiter (dlm="09"x is the tab character).
  • DSD means delimiter-sensitive data: two commas in a row mean a missing value, quotation marks around a value are removed, and a comma inside the quotes is not a delimiter. It also sets the default delimiter to a comma.
  • MISSOVER keeps a short line from pulling values from the next line; the variables not found are set to missing. TRUNCOVER is similar and is better for character data of varying width.

Always use DSD for comma-separated data. Without it, the quoted city above would split into two fields.

Reading multiple observations per line

A trailing @@ after input keeps the line held for the next iteration, so one data line can contain many observations:

SAS
data pairs;
   input x y @@;
   datalines;
1 10  2 20  3 30
4 40
;
run;

SAS loops through the line, reading x, y pairs, until it runs out. A single @ (the trailing at) holds the line for the next input statement in the same iteration, which is useful for looking at a field before deciding how to read the rest.

Checking what was read

PROC PRINT displays the data and PROC CONTENTS shows the variable types. Always check these after reading new data. In the log, look for NOTE: Invalid data for ..., which means a value did not fit the variable's type, and NOTE: SAS went to a new line when INPUT statement reached past the end of a line, a sign of missing values without missover.

A list-input reader

This Python stand-in (not SAS) implements the logic of list input with DSD rules for a comma-separated record, including quoted text and missing values.

Python Python stand-in: DSD-style splitting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A character variable read by plain list input has a default length of 8: Elizabeth is stored as Elizabet. Use :$20. (or a length statement) whenever text can be longer.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the $ after a variable name in INPUT mean?

    A dollar sign marks a character variable.
  2. 2. What does the DSD option do for comma-separated data?

    DSD sets the delimiter to a comma by default, reads two commas in a row as a missing value and strips quotes.
  3. 3. Which style reads fixed positions such as 'id 1-3 name $ 4-13'?

    Column input gives the start and end column of each field.
  4. 4. What does @@ at the end of an INPUT statement allow?

    The double trailing at holds the line so the next iteration continues reading the same line.

Summary

  • datalines; supplies in-stream data; input describes each record.
  • List input splits on delimiters (use :$n. for long text), column input uses fixed positions, formatted input uses @ and informats.
  • infile datalines dlm=... dsd missover handles delimited text with quotes and missing values.
  • @@ reads many observations per line; always check the log for invalid data notes.

Next: Informats and Formats