Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SAS cross table


May 27, 2021 SAS


Table of contents


Cross-tableing involves using all possible combinations of two or more variables to produce a crosstabber also known as an occasional table. I n SAS, it is created using the PROC FREQ and CABLES options. F or example - if we need the frequency of each model in each model category, then we need to use the CABLES option of PROC FREQ.

Grammar

The basic syntax for applying crosstaps in SAS is:

PROC FREQ DATA = dataset;
TABLES variable_1*Variable_2;

The following is a description of the parameters used:

  • Dataset is the name of the dataset.
  • Variable_1 and Variable_2 are variable names for datasets whose frequency distributions need to be calculated.

Cases

Consider the following from SASHELP. C ARS creates a dataset car1 that looks for the types of cars available under each car brand. I n this case, we need a single frequency value and a number of frequency values across types. W e can observe that the results show values across rows and columns.

PROC SQL;
create table CARS1 as
SELECT make,type,invoice,horsepower,length,weight
 FROM 
SASHELP.CARS
WHERE make in ('Audi','BMW')
;
RUN;

proc FREQ data=CARS1 ;
tables make*type; 
run;

When we execute the code above, we get the following results:

SAS cross table

Cross-tableing of 3 variables

When we have three variables, we can group two of them and cross the list with the third variable. S o in the result we have two crosstases.

Cases

In the following example, we find the frequency of each type of car and the model of each model relative to the car. I n addition, we use the nocol and norow options to avoid and percentage values.

proc FREQ data=CARS2 ;
tables make * (type model)  / nocol norow nopercent;   
run;

When we execute the code above, we get the following results:

SAS cross table

A cross table of 4 variables

For 4 variables, the number of pairing combinations increases to 4. Each variable from Group 1 is paired with each variable in Group 2.

Cases

In the following example, we find the frequency of each model and the length of the car for each model. S imilarly, the horsepower frequency of each manufactured and per model.

proc FREQ data=CARS2 ;
tables (make model) * (length  horsepower)  / nocol norow nopercent;   
run;

When we execute the code above, we get the following results:

SAS cross table