Changing Data Types Using INPUT Function in SAS

📌 Introduction

In data analysis, raw data often comes as character strings—even when the values are clearly numeric or dates. That’s where the INPUT function in SAS comes into play. It allows you to convert character data into numeric or date values, making it suitable for calculations, filtering, and modeling.

In this blog, we’ll cover:

  • What the INPUT function does
  • Syntax and parameters
  • Numeric and date conversion examples
  • Common pitfalls
  • Practical use cases
Input and Put


🧠 What is the INPUT Function in SAS?

The INPUT function converts a character string to a numeric or date value using a SAS informat.


🧾 Syntax

INPUT(source, informat.)

  • source: Character variable or string literal
  • informat: SAS informat (e.g., 8., mmddyy10., comma10., etc.)


🔍 Use Cases and Examples

✅ 1. Convert Character to Numeric

data convert_numeric;
char_value = '12345.67'; num_value = input(char_value, 8.2); /* Output: 12345.67 */ run;

✅ 2. Convert Character Date to SAS Date

data convert_date;
char_date = '02/15/2024'; sas_date = input(char_date, mmddyy10.); formatted = put(sas_date, date9.); /* Output: 15FEB2024 */ run;

✅ 3. Convert Formatted Numeric String

data comma_format;
char_value = '12,345'; num_value = input(char_value, comma6.); /* Output: 12345 */ run;

⚠️ Common Errors to Avoid

MistakeWhy It's a Problem
Using INPUT on numeric dataIt expects character input; gives wrong results
Missing or incorrect informatFails to interpret the data accurately
Wrong lengthCan truncate or misread values

💡 Best Practices

  • Use INPUT with raw data imports from Excel, CSV, or flat files.
  • Always match the correct informat to the incoming string format.
  • Combine with PUT to round-trip between formats:

num = input(char, 8.);
char = put(num, 8.);

🧪 Real-Life Example: Reading Survey Scores

Suppose you receive survey data as text:

data survey;
raw_score = '87.5'; numeric_score = input(raw_score, 4.1); run;

You can now calculate average scores, grades, or segments based on numeric_score.


🏁 Conclusion

The INPUT function is your go-to for type conversion in SAS. Whether you’re reading numeric values from character fields or parsing dates from strings, mastering INPUT helps you unlock the full power of SAS analytics.

Post a Comment

0 Comments