Creating New Variables in PROC SQL (SAS) Using the CALCULATED Keyword
In PROC SQL (SAS), you can dynamically create new variables within your query and assign them a name. If a name is not provided, these dynamically created variables will appear in the output without a column heading.
For example, in the SQL code below, we create a new variable called NEWWT, calculated as:
NEWWT = WT * 0.5
After creating this variable, we can reference it again in the same query by using the CALCULATED keyword. This tells SAS that the value is already computed within the query and can be reused for further calculations.
Here, we use CALCULATED NEWWT to derive another variable called REV_WT:
REV_WT = CALCULATED NEWWT
Why Use the CALCULATED Keyword in PROC SQL?
- It allows you to reference variables created within the same SQL step.
- Prevents the need to recalculate expressions multiple times.
- Ensures clarity in your SQL queries when working with derived variables.
Example Output
The resulting output will display both the NEWWT and REV_WT columns with proper headings.

The Output looks like:

Leave a comment