BookmarkSubscribeRSS Feed

SAS Tip: DATALINES statement overview

Started ‎05-10-2019 by
Modified ‎05-15-2019 by
Views 11,599

The DATALINES statement introduces lines of data in a DATA Step. There are two alternative statement keywords that can be used for this statement; the CARDS statement and the LINES statement.

Syntax

DATALINES ;

or

CARDS ;

or

LINES ;

followed by

<any number of lines of data>
;
RUN statement;

Usage

The DATALINES statement precedes any lines of data that are going to be read into the DATA step. The lines of data that immediately follow this statement end when the first semicolon is encountered. If the data contains semicolons then the DATALINES4 statement needs to be used instead.

The DATALINES statement must be used at the end of a DATA Step after all the other statements in the data step but before the lines of data.

The terminating semicolon should be the first character on a new line and can be followed by the RUN statement.

To read the lines of data that follow the DATALINES statement the DATA step must also contain at least one INPUT statement to read the data. An INFILE statement that associates the datalines with an infile name can also be used if the data is delimited or other infile options need to be used to read the data.

Example

DATA lines_of_data;
     INPUT a_number ;
     DATALINES ;
1
23
4
567
;
RUN;

Will produce the dataset LINES_OF_DATA containing

A_NUMBER
       1
      23
       4
     567

See also

Thanks to Cameron for originally contributing this content to sasCommunity.org.

Comments

I often use CARDS4 (4 fewer characters to type than DATALINES4) but I've never used the LINES alias.  I suppose it's been around for 30 years and I'd just never noticed it.

 

I think DATALINES is newer than CARDS, but I don't remember when I first saw it.

 

 

Thanks @JackHamilton ! We're porting some of this popular content from sasCommunity.org, so you'll find lots of hidden nuggets like that.

Why use 'RUN;' at the end of the programa.

I think it works fine without it.

Or may be I'm wrong?

 

@sardav I think you need the RUN; (and it's good practice) -- but when using SAS Enterprise Guide or SAS Studio, those tools add the run; statement for you.

Tom

You can insert as many extra RUN; statements in your code as you want.  If there is no step to end they are essentially NOOP statements. 

 

The reason to NOT use them is to avoid confusing novice users into thinking they can insert data steps statements after the data and before the RUN.

 

The reason you might want to insert them after in-line data is to help users that are used to scanning for them to find the ends of steps. 

 

If you are using the CARDS statement (or LINES or LINES4 or CARDS4 or DATALINES or DATALINES4), the terminating ";" or ";;;;" appears to serve the same purpose as the RUN statement in a regular data step, but I don't see that documented so I wouldn't count on it.

 

Otherwise, yes, you should always close your run groups with RUN or QUIT, depending on the procedure, or you run the risk of global statements like TITLE and OPTIONS being applied in a way you don't expect, and of log NOTES appearing far away from the code that generated them.

Consider the following code:

title 'The Force That Through The Green Fuse';

data verse1;
    infile cards;
    input myitem $char60.;
cards4;
The force that through the green fuse drives the flower
Drives my green age; that blasts the roots of trees
Is my destroyer.
And I am dumb to tell the crooked rose
My youth is bent by the same wintry fever.
;;;;

title1 'First 3 lines';
proc print data=verse1 (obs=3);


/* Dylan Thomas was born in Swansea, Wales */
title1 'Next 2 lines';
proc print data=verse1 (firstobs=4);
run;

Both PROC PRINTs have the title "Next 2 lines", even though it appears from a quick scan that the titles would be different.

 

I always use CARDS4 because it's easy to spot 4 semicolons in a row when scanning through a program.  That pretty much never occurs outside the CARDS4 context.

Hello Chris + Tom + Jack!

Some days after I'm back! 🙂

Chris: I run 'data step' with 'datalines' in E. Guide and in the classical programming interface too (I'm not so young...).

And a 'data step' + 'datalines' without a run statement al the end is not needed. Believe it or not.

Tom: I'm agree. You can insert as many 'Run;' statements as you want. But may be is not needed when use 'datalines' or 'cards' in a data step. And this is the question with no answer to me.

Jack: good idea! I'm going to use 'cards4' from now on

I found some link using 'run;' at the end with 'datalines' or 'cards'. But i also found other not using it. Examples? SAS documentation!

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0114gachtut3nn1and4ap8ke9nf.htm#p...

https://www.caee.utexas.edu/prof/MAIDMENT/StatWR2009/SAS/Manuals/base_LanguageRefernceConcept_9196.p... (page 533)

Why this topic is important to me? Because I teach Programming courses at SAS.

Thanks for your posts.

 

Sorry. I wrote:

"And a 'data step' + 'datalines' without a run statement al the end is not needed"

And meaned:

And a 'data step' + 'datalines' with a run statement al the end is not needed.

As you may know, double denial has different meaning in Latin languages.

Thanks.

Tom

When you use CARDS/DATALINES statement to include in-line then the data step ends when the data ends.  The data ends on the line before the first line with any semi-colon (or if using CARDS4 or DATALINES4 the line before the line that consists of four semi-colons).

Any RUN statements after the data is not needed, but does no real harm other than confuse programmers, like @sardav .

Version history
Last update:
‎05-15-2019 04:23 PM
Updated by:

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags