SAS: How to Use NWAY in PROC SUMMARY


You can use the NWAY statement in PROC SUMMARY in SAS to only calculate summary statistics at a group level rather than calculating them for an entire dataset.

The following example shows how to use the NWAY statement in practice.

Example: How to Use NWAY in PROC SUMMARY

For this example, we’ll use the SAS built-in dataset called Fish, which contains various measurements for 159 different fish caught in a lake in Finland.

We can use PROC PRINT to view the first 10 observations from this dataset:

/*view first 10 observations from Fish dataset*/
proc print data=sashelp.Fish (obs=10);

run;

We can use the following code with PROC SUMMARY to calculate descriptive statistics for the variable Weight, grouped by the variable Species:

/*calculate descriptive statistics for Weight, grouped by Species*/
proc summary data=sashelp.Fish;
    var Weight;
    class Species;  
    output out=summaryWeight;
run;

/*print output dataset*/
proc print data=summaryWeight;

Note: There are a total of 40 rows in the output but we’ve only taken a screenshot of the first 20 rows.

Here’s how to interpret the output table:

  • _TYPE_: This column shows whether or not every row in the dataset was used to calculate the descriptive statistics. 0 = Every row was used.
  • _FREQ_: The number of rows used to calculate each descriptive statistic.
  • _STAT_: The name of the descriptive statistic.
  • Weight: The numerical value for the corresponding descriptive statistic.

The first five rows show summary statistics for the entire dataset.

For example:

  • The total number of observations was 158.
  • The minimum weight value was 0.
  • The maximum weight value was 1,650.
  • The mean weight value was 398.70.
  • The standard deviation of weight values was 359.09.

The next five rows show these summary statistics only for the rows in the dataset where the Species is equal to Bream.

The next five rows show these summary statistics only for the rows in the dataset where the Species is equal to Parkki.

And so on.

If we use the NWAY statement in PROC SUMMARY, we specify that we only want to display the rows with the highest value in the _TYPE_ column of the output.

This means that only rows with a value of 1 in the _TYPE_ column will be shown. In other words, the first five rows that show summary statistics for the entire dataset will no longer be shown.

The following code shows how to use the NWAY statement in practice:

/*calculate descriptive statistics for Weight, grouped by Species*/
proc summary data=sashelp.Fish nway;
    var Weight;
    class Species;  
    output out=summaryWeight;
run;

/*print output dataset*/
proc print data=summaryWeight;

Notice that the summary statistics for the entire dataset are no longer shown.

Only the summary statistics for the individual Species are shown.

Additional Resources

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

How to Use Proc Append in SAS
How to Use Proc Tabulate in SAS
How to Calculate Correlation in SAS
How to Create Frequency Tables in SAS

Leave a Reply

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