How can I create tables in SAS? Can you provide some examples? 2

How can I create tables in SAS? Can you provide some examples?

Creating tables in SAS is a simple and efficient way to organize and analyze data. To create a table in SAS, you can use the PROC SQL or DATA step functions. The PROC SQL function allows you to directly create and manipulate tables using SQL statements, while the DATA step function involves creating a data set and then converting it into a table using the PROC TABLE procedure.

For example, to create a table named “students” with columns for student ID, name, and grades, you can use the following PROC SQL statement:

PROC SQL;
CREATE TABLE students (
student_id INT,
student_name VARCHAR(50),
grade1 INT,
grade2 INT,
grade3 INT
);
QUIT;

In the above example, we have specified the data types for each column, which is optional. The table “students” will be created with the specified columns and data types.

Similarly, using the DATA step function, you can create a table named “sales” with columns for product name, price, and quantity, as shown below:

DATA sales;
INPUT product_name $ price quantity;
DATALINES;
Milk 2.50 10
Bread 3.00 15
Coffee 5.00 8
;
RUN;

In the above code, we have used the INPUT statement to define the columns and their respective data types. The DATALINES statement is used to input the data for the table. The resulting table “sales” will have three columns and three rows of data.

In conclusion, creating tables in SAS is a straightforward process that can be done using PROC SQL or DATA step functions. These are just a few examples, and there are various other ways to create tables in SAS depending on your specific data and analysis needs.

Create Tables in SAS (With Examples)


You can use proc sql to quickly create tables in SAS.

There are two ways to do so:

1. Create a Table from Scratch

2. Create a Table from Existing Data

The following examples show how to do both using proc sql.

Example 1: Create a Table from Scratch

The following code shows how to create a table with three columns using proc sql in SAS:

/*create empty table*/
proc sql;
   create table my_table
       (team char(10),
        points num,
        rebounds num);

/*insert values into table*/          
insert into my_table
      values('Mavs', 99, 22)
      values('Hawks', 104, 20)
      values('Hornets', 88, 25)
      values('Lakers', 113, 19)
      values('Warriors', 109, 32);

/*display table*/
select * from my_table;
run;

create table in SAS

We used create table to create an empty table, then used insert into to add values to the table, then used select * from to display the table.

The result is a table with three columns that show various information for different basketball teams.

Example 2: Create a Table from Existing Data

The following code shows how to use proc sql to create a table by using an existing dataset that we created in the previous example:

/*create table from existing datset*/
proc sql;
   create table my_table2 asselect team as Team_Name,
             points as Points_Scored
         from my_table;
         
/*display table*/
select * from my_table2;
run;

The result is a table that contains two columns with values that come from an existing dataset.

Note: We used the as function to specify the column names to be used in the table, but you don’t have to use the as function if you don’t want to rename the columns.

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

Cite this article

stats writer (2024). How can I create tables in SAS? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-tables-in-sas-can-you-provide-some-examples/

stats writer. "How can I create tables in SAS? Can you provide some examples?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-tables-in-sas-can-you-provide-some-examples/.

stats writer. "How can I create tables in SAS? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-tables-in-sas-can-you-provide-some-examples/.

stats writer (2024) 'How can I create tables in SAS? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-tables-in-sas-can-you-provide-some-examples/.

[1] stats writer, "How can I create tables in SAS? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create tables in SAS? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top