Table of Contents
The CARDS statement is a powerful tool in SAS that allows for the direct input of data into a program without the need for external data files. It is often used in situations where data is small and does not require a separate file, or when testing and debugging code. To utilize the CARDS statement, the data must be entered in a specific format with each column representing a variable and each row representing an observation. An example of using the CARDS statement in SAS would be:
DATA sales;
INPUT country $ sales;
CARDS;
USA 10000
Canada 5000
Mexico 8000
;
RUN;
This code creates a data set called “sales” with two variables, “country” and “sales”, and three observations. The data is then directly input using the CARDS statement, with the country names and sales amounts entered in the same order as the variables in the INPUT statement. This allows for quick and efficient data input without the need for an external data file.
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 printdata=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 printdata=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:
Cite this article
stats writer (2024). How can the CARDS statement be utilized in SAS, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-cards-statement-be-utilized-in-sas-and-can-you-provide-an-example/
stats writer. "How can the CARDS statement be utilized in SAS, and can you provide an example?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-cards-statement-be-utilized-in-sas-and-can-you-provide-an-example/.
stats writer. "How can the CARDS statement be utilized in SAS, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-cards-statement-be-utilized-in-sas-and-can-you-provide-an-example/.
stats writer (2024) 'How can the CARDS statement be utilized in SAS, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-cards-statement-be-utilized-in-sas-and-can-you-provide-an-example/.
[1] stats writer, "How can the CARDS statement be utilized in SAS, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the CARDS statement be utilized in SAS, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
