How to Use CARDS Statement in SAS (With Example)


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.

Additional Resources

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

How to Create New Variables in SAS
How to Replace Characters in a String in SAS
How to Replace Missing Values with Zero in SAS
How to Remove Duplicates in SAS

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *