datalines sas

Datalines SAS Demystified: The Ultimate Guide to Inline Data Input

By Anthony Moore
AD

When you need to feed small amounts of data directly into a SAS DATA step without creating an external file, the DATALINES statement is often the fastest route. Also known as the CARDS statement, datalines sas lets you embed raw records inside your program, immediately after the statement, so the DATA step can read them with an INPUT statement. This approach is especially useful for demonstrations, testing, and one-off analyses where portability matters more than scale.

Although DATALINES looks simple, it has precise rules about semicolons, delimiters, missing values, and line handling. A misplaced terminator or an INPUT statement that does not match the data layout can generate confusing notes in the SAS log. Learning how SAS DATALINES works at a practical level helps you write cleaner code, debug faster, and recognize when an external file or database connection is a better fit.

What Is the DATALINES Statement in SAS?

The DATALINES statement is an in-stream data source used inside a DATA step. It tells SAS that the lines following the statement are raw data records, not executable statements, until SAS encounters a terminating semicolon on its own line. The INPUT statement that appears before DATALINES defines how those records should be parsed into variables. Together, they create a compact way to build a dataset without leaving the program editor.

AD
SAS Loops – Understanding Leave and Continue
SAS Loops – Understanding Leave and Continue
This Week's Big Data Big Ten
This Week's Big Data Big Ten
a screenshot of a dashboard showing the number of people in each region, and where they are currently working
a screenshot of a dashboard showing the number of people in each region, and where they are currently working

Because DATALINES is processed at DATA step execution, it works naturally with common SAS options such as DLM, DSD, and TRUNCOVER when paired with an INFILE DATALINES statement. It also supports character and numeric variables, missing values, and fixed or delimited input. The main limitation is scale: DATALINES is ideal for small, static datasets, but it is not intended for large files that change frequently or arrive from another system.

How the DATA Step Processes DATALINES

When SAS compiles the DATA step, it reads the INPUT statement and builds a program data vector for the variables you name. At execution, the DATA step moves through the lines supplied by DATALINES one record at a time. Each line is matched against the INPUT statement, and the resulting values are written to the output dataset. The process repeats until SAS reaches the ending semicolon that marks the close of the in-stream data.

8 Ways to count the number of observations in a SAS dataset.
8 Ways to count the number of observations in a SAS dataset.
Keeping an open mind about open analytics
Keeping an open mind about open analytics
Untitled design (9)
Untitled design (9)

For example, a simple program can be written as data work.sales; input Region $ Month $ Revenue; datalines; East Jan 1200 West Feb 950 ; run; In this case, SAS reads two records and creates three variables. If a line contains too few values, the behavior depends on options such as TRUNCOVER and DSD. If it contains extra values, SAS may ignore them or issue a note, depending on the INPUT style being used.

DATALINES vs. Infile and External Files

The alternative to DATALINES is usually the INFILE statement, which points to an external text file, a fileref, or a cloud location. INFILE is better for large datasets, recurring imports, and files that are updated by other processes. DATALINES, by contrast, keeps everything in one program and avoids file path issues, which makes it convenient for examples, small lookup tables, and reproducible test cases.

AD

Consider a scenario where you need to test a reporting macro before connecting to a production database. You can create a tiny dataset with DATALINES, run the macro, and verify the output without waiting for network access. Once the logic is correct, you can replace the DATALINES block with an INFILE or LIBNAME statement that reads the real data. This workflow keeps development fast while preserving a clear path to production.

SaaS Motion Design For Myna.cs | Ui Animation | SaaS | Motion Graphics
SaaS Motion Design For Myna.cs | Ui Animation | SaaS | Motion Graphics
SAS Clinical Data Management Online Training And Placement Assistance Job Support
SAS Clinical Data Management Online Training And Placement Assistance Job Support
an instruction to sas's visual analyses and gain into your data book by tricia aderud, rob colum - ryan kumpull
an instruction to sas's visual analyses and gain into your data book by tricia aderud, rob colum - ryan kumpull
SaaS Data Visualization UI
SaaS Data Visualization UI
SaaS Explainer Motion Video
SaaS Explainer Motion Video
First Look – SAS In-Database Analytics - SmartData Collective
First Look – SAS In-Database Analytics - SmartData Collective

Writing Effective DATALINES Code

Effective DATALINES code starts with a clean INPUT statement. You need to know whether your values are separated by spaces, commas, tabs, or fixed columns, and whether character values contain spaces. You also need to decide how missing numeric and character values should be represented. A little planning here prevents most data-reading errors before they reach the SAS log.

Another practical concern is placement. DATALINES must appear after the INPUT statement in the DATA step, and the data lines must follow immediately. The statement itself ends with a semicolon, but the data records do not. A single semicolon on a line by itself closes the data block. This distinction is a common source of confusion for new SAS programmers.

AD

Basic Syntax and Delimiters

A basic DATALINES block uses list input with spaces as delimiters. Character variables are usually marked with a dollar sign in the INPUT statement, while numeric variables are left unmarked. For instance, data work.employees; input Name $ Department $ Salary; datalines; Avery Sales 62000 Jordan IT 71000 ; run; creates a small employee table. The names and departments are character values, and salary is numeric.

an image of a cartoon character sitting in a chair with the words sol above it
an image of a cartoon character sitting in a chair with the words sol above it
Vanrise Teams Up with SAS to Support Telecom Operators in Fraud Management | Al Bawaba
Vanrise Teams Up with SAS to Support Telecom Operators in Fraud Management | Al Bawaba
the data pipeline architecture diagram is shown in red, white and green colors with arrows pointing to
the data pipeline architecture diagram is shown in red, white and green colors with arrows pointing to
what is idc in sap? info sheet with information about it and how to use it
what is idc in sap? info sheet with information about it and how to use it

When your data contains commas or missing character values, add an INFILE DATALINES statement before the INPUT statement and specify options such as DLM=',' and DSD. The DATALINES statement itself remains the marker that starts the embedded records. For example, infile datalines dlm=',' dsd; input Name $ Department $ Salary; datalines; Avery,Sales,62000 Jordan,IT,71000 ; run; reads comma-delimited rows and treats consecutive delimiters as missing values when DSD is active.

Common Pitfalls and Debugging

One common mistake is forgetting the final semicolon on its own line. Without it, SAS may treat the following statements as data, leading to errors or truncated output. Another issue is a mismatch between the INPUT statement and the actual records. If you define four variables but supply three values per line, SAS may read across line boundaries or set values to missing, depending on the input method. Column input is especially sensitive to spacing and alignment.

AD

When DATALINES does not behave as expected, check the SAS log first. Notes about invalid numeric data, missing values, or too few variables often point to the exact record. You can also add a PUT statement or use PROC PRINT immediately after the DATA step to inspect what was created. For fixed-column data, PROC CONTENTS helps confirm variable lengths, while a simple listing of the raw input can reveal hidden spaces or tab characters.

Advanced DATALINES Techniques and Best Practices

As you gain experience, DATALINES becomes more than a beginner convenience. It can support advanced testing patterns, macro-driven sample data, and small configuration tables that live beside the code that uses them. The key is to treat the embedded data as part of the program and apply the same discipline you would apply to any other source file.

Advanced use also means knowing when to stop. If a DATALINES block grows beyond a screen or two, or if it needs frequent updates from non-programmers, an external file or database table is usually easier to maintain. DATALINES shines when the data is short, stable, and tightly coupled to the logic being demonstrated or tested.

DATALINES4 and Special Characters

The DATALINES4 statement is a variant that closes with four consecutive semicolons instead of one. This allows the data itself to contain semicolons, which is useful for text fields, JSON-like fragments, or delimited strings that include punctuation. The four-semicolon terminator gives SAS a clear end marker without conflicting with semicolons inside the records.

For example, you might use DATALINES4 when a comment field contains semicolons, such as a note that reads "Review; approve; archive." A standard DATALINES block would close prematurely at the first standalone semicolon, but DATALINES4 waits for the full four-semicolon sequence. This makes it a practical choice for small text-heavy datasets, though you should still verify that no data line accidentally begins with four semicolons.

Using DATALINES for Testing and Automation

DATALINES is excellent for unit-style testing in SAS. You can create expected input, run a transformation, and compare the result with a known output. Because the data is embedded in the program, the test travels with the code and does not depend on a shared drive or database connection. This is valuable in regulated environments where reproducibility and auditability matter.

For automation, keep DATALINES blocks small, clearly labeled, and close to the logic they support. Document the expected variables and any delimiter options in a comment above the DATA step. When the test grows into a reusable fixture, move it to an external file and read it with INFILE. That progression keeps early development fast while ensuring long-term maintainability.

Put DATALINES to work the next time you need a quick, self-contained dataset for a SAS experiment. Start with a simple INPUT statement, add only the options you truly need, and check the log before building anything larger. Once the pattern feels natural, you will know exactly when to keep data in-stream and when to hand it over to a more scalable source.

AD