Cal11 calculator

Sas Put Mean Calculated in A Previous Step

Reviewed by Calculator Editorial Team

When working with SAS, you may need to calculate the mean of values that were processed in a previous step. This guide explains how to use the PUT statement to store the mean calculation for later use in your SAS program.

Introduction

The PUT statement in SAS is used to write data to an output file or to the SAS log. When you need to calculate a mean in one step and use it in a subsequent step, you can store the result using PUT and then retrieve it later.

This technique is particularly useful when you need to:

  • Calculate a mean from one dataset and use it in another dataset
  • Store intermediate results for later use in your SAS program
  • Create summary statistics that can be referenced in subsequent data steps

How to Use This Calculator

This calculator helps you understand how to implement the PUT statement to store a mean calculation. Enter your values in the calculator below, and it will show you the SAS code you would need to implement this functionality.

Note

The calculator demonstrates the concept but doesn't actually execute SAS code. For real implementation, you'll need to run the generated code in a SAS environment.

Formula

Mean Calculation Formula

The mean (average) is calculated as:

Mean = (Sum of all values) / (Number of values)

When using PUT to store this mean for later use, you would typically:

  1. Calculate the mean in a DATA step
  2. Use PUT to write the mean to a macro variable or output file
  3. Retrieve the stored mean in a subsequent DATA step

Worked Example

Consider a scenario where you have a dataset of exam scores and you want to calculate the class average and then use that average in another dataset.

Example SAS Code

/* Step 1: Calculate and store the mean */
data _null_;
    set exam_scores;
    array scores[*] score1-score10;
    sum = sum(of scores[*]);
    count = dim(scores);
    mean = sum/count;
    call symputx('class_mean', mean);
run;

/* Step 2: Use the stored mean in another dataset */
data student_performance;
    set student_info;
    class_average = symget('class_mean');
    if score > class_average then performance = 'Above average';
    else performance = 'Below average';
run;

In this example:

  • The first DATA step calculates the mean score and stores it in a macro variable
  • The second DATA step retrieves the stored mean and uses it to classify each student's performance

FAQ

Can I store multiple means using PUT?

Yes, you can store multiple means by using different macro variable names or by writing to different output files. Each PUT statement can target a specific macro variable or file.

What happens if I try to retrieve a mean that wasn't calculated?

If you attempt to retrieve a mean that wasn't calculated or stored, SAS will return a missing value. It's good practice to check if the value exists before using it in your program.

Can I use PUT to store means in a dataset rather than a macro variable?

Yes, you can use PUT to write means directly to a dataset by using the FILE statement to specify an output file. This approach is useful when you need to store multiple values or when working with very large datasets.