Formatting Numbers with the PUT Function in SAS

 📌 Introduction

In SAS programming, presenting data in a readable and meaningful way is essential—especially in reports and dashboards. That’s where the PUT function becomes a powerful tool. This function allows you to convert numeric or date values into formatted character strings, making your output more user-friendly.

Put Function in SAS

In this blog, you’ll learn:

  • What the PUT function does
  • Syntax and usage
  • Examples using numeric, date, and categorical formats
  • Real-world applications
  • Tips & best practices


🧠 What is the PUT Function in SAS?

The PUT function in SAS is used to:

  • Convert numeric or date values to character values
  • Apply a specific format such as currency, date, percent, or custom user-defined formats


🧾 Syntax


PUT(source, format.)

  • source: The variable or value you want to format
  • format: A SAS format (e.g., dollar8.2, date9., percent6.2, etc.)


🔍 Common Use Cases of PUT Function

✅ 1. Format Numeric to Currency

data currency_format;
amount = 12345.678; formatted_amt = put(amount, dollar10.2); /* $12,345.68 */ run;

✅ 2. Convert Date to Readable Format

data date_format;
raw_date = '01jan2024'd; formatted_date = put(raw_date, date9.); /* 01JAN2024 */ run;

✅ 3. Convert Numeric Code to Label using Format

Assume we have a user-defined format for regions:

proc format;
value regionfmt 1 = 'North' 2 = 'South' 3 = 'East' 4 = 'West'; run; data region_label; region_code = 2; region_name = put(region_code, regionfmt.); run;

🧪 Real-Life Example: Sales Report Formatting

Suppose you're generating a report with sales data:

data sales;
id = 101; sale_amount = 24999.5; sale_date = '15feb2024'd; formatted_amt = put(sale_amount, dollar12.2); formatted_date = put(sale_date, worddate18.); run;

📌 Output:

  • formatted_amt = $24,999.50
  • formatted_date = February 15, 2024


💡 Best Practices

  • Always ensure the width in the format is large enough for your values, or it may display *****.
  • Use PUT only to convert to character values. For character to numeric, use INPUT.
  • Pair PUT with user-defined formats for readable labels in reports and visualizations.


❗ Common Mistakes to Avoid

MistakeWhy It's a Problem
Using PUT on a character valueWon’t change the format — use INPUT to convert first
Not specifying enough widthResults in truncated or ***** output
Forgetting to end format with a dot (.)Syntax error in SAS

🏁 Conclusion

The PUT function is a fundamental tool in SAS for formatting your output—whether it's turning raw numbers into currency, making dates human-readable, or converting codes into labels.

By mastering this function, you’ll significantly improve the clarity and professionalism of your SAS reports.

Post a Comment

0 Comments