SAS author's tip: computing a new variable with PROC REPORT


Ron Cody and his supremely helpful book Learning SAS by Example close out our weekly SAS Author's Tip of 2011. Ron Cody is a household name in the SAS user community. And he deserves the moniker on his license plate of "SASMAN". Just mentioning Ron's name at user conferences incites awe. Sometimes fans even line-up in our bookstore at SAS Global Forum to have their photos taken with him.

Besides having written several useful and bestselling SAS Press books, including his most recent SAS Statistics by Example, Ron is a private consultant and a national instructor for SAS. When he's not busy teaching, consulting, or writing (he miraculously turns around a new book every few years for us), he's often attending and/or presenting at regional user group conferences. If you haven't had the pleasure of meeting Ron in person, put this on your to-do list for 2012!

I hope that you enjoy this last tip of the year. I look forward to resuming these weekly posts in early January.

The following excerpt is from SAS Press author Ron Cody's book "Learning SAS by Example: A Programmer's Guide" Copyright © 2007,  SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED. (please note that results may vary depending on your version of SAS software

Computing a New Variable with PROC REPORT

One powerful feature of PROC REPORT is its ability to compute new variables. This makes PROC REPORT somewhat unique among SAS procedures. With most SAS procedures, you need to run a DATA step first if you want to perform any calculations.

Suppose you want to report the weights of the patients in your Medical data set, but instead of reporting the weights in pounds (the units used in the data set), you want to see the weights in kilograms. Program 15-17 uses a compute block to accomplish this:

Notice several things about this program:

  • The new variable (WtKg) and the variable used to compute it (Weight) are both listed in the COLUMN statement. It is important that you list Weight before WtKg in this statement—a computed value must follow the variable or variables used to define it. If you don’t want to include the original value of weight in the report, you use the keyword NOPRINT in the DEFINE statement for Weight.
  • Use a usage of COMPUTED in the DEFINE statement for your new variable.
  • Use COMPUTE and ENDCOMP statements to create a COMPUTE block. You define your new variable inside this block.

As you will see in the next example, this block can contain programming logic as well as arithmetic computations.

Here is the output from Program 15-17:

You can learn more about Ron and read a free chapter from each of his books by visiting his author page.


About Author

Shelly Goodin

Social Media Specialist, SAS Publications

Shelly Goodin is SAS Publications' social media marketer and the editor of "SAS Publishing News". She’s worked in the publishing industry for over thirteen years, including seven years at SAS, and enjoys creating opportunities for fans of SAS and JMP software to get to know SAS Publications' many offerings and authors.

Related Posts

Back to Top