How to Use CARDS Statement in SAS?

The CARDS statement is used in SAS to read data from an external file or from the DATA step input buffer. The data is read in line-by-line and each line is considered as a record. The CARDS statement is typically used with the INPUT statement to read and process the data from the external file. The data can be read from either a text file or a data file. The CARDS statement can also be used to write data to an external file. The data can be written to either a text file or a data file.


You can use the CARDS statement in SAS to input values into a new dataset.

You can use the following basic syntax to do so:

data my_data;
    input var1 $ var2;
    cards;
A 12
B 19
C 23
D 40
;
run;

Here’s what each statement does:

  • data: The name of the dataset
  • input: The name and type of each variable in the dataset
  • cards: The actual values in the dataset

Once SAS sees the CARDS statement, it knows that data values follow it immediately on the next line.

Note #1: A dollar sign “$” following a variable name tells SAS that the variable is a character variable.

Note #2: The statement is named CARDS because many years ago programmers had to feed actual cards into computers with holes punched in them that represented data values.

The following examples show how to use the CARDS statement in practice.

Example: How to Use CARDS Statement in SAS

The following code shows how to use the CARDS statement to create a dataset with three numeric variables: team, points, assists:

/*create dataset*/
data my_data;
    input team $ points assists;
    cards;
Mavs 14 9
Spurs 23 10
Rockets 38 6
Suns 19 4
Kings 30 4
Blazers 19 6
Lakers 22 14
Heat 19 5
Magic 14 8
Nets 27 8
;
run;
/*view dataset*/
proc print data=original_data;

The result is a dataset with three variables.

It’s worth noting that the alternative to the CARDS statement is the DATALINES statement, which can also be used to input values into a dataset.

If we use the DATALINES instead of the CARDS statement, we can create the exact same dataset:

/*create dataset*/
data my_data;
    input team $ points assists;
    datalines;
Mavs 14 9
Spurs 23 10
Rockets 38 6
Suns 19 4
Kings 30 4
Blazers 19 6
Lakers 22 14
Heat 19 5
Magic 14 8
Nets 27 8
;
run;
/*view dataset*/
proc print data=original_data;

This dataset is the exact same as the one created using the CARDS statement.

In the real world, you will likely encounter the DATALINES statement used more often than the CARDS statement.

However, both statements are equivalent.

The following tutorials explain how to perform other common tasks in SAS:

x