SAS Numeric to Character: Easy Conversion Guide with Examples
Converting numeric values to character strings is a routine but surprisingly nuanced task in SAS. Whether you are building a report, cleaning imported data, or preparing a dataset for a merge, you will eventually need to turn a number into text. The operation sounds simple, yet SAS stores numeric and character data in fundamentally different ways, so a careless conversion can introduce leading blanks, truncated values, or silent mismatches. Understanding how SAS handles numeric-to-character conversion helps you avoid these traps and write code that behaves predictably.
In SAS, a variable's type is fixed when it is created. You cannot change a numeric variable into a character variable in place. Instead, you create a new character variable using functions such as PUT, CAT, or CATS, or by relying on implicit conversion during concatenation. Each approach has trade-offs in formatting control, length management, and portability. This article explains the core techniques, advanced scenarios, and common pitfalls so you can choose the right method for every situation.
Core Methods for SAS Numeric to Character Conversion
The most explicit and reliable way to convert a numeric value to character is the PUT function. PUT applies a SAS format to a numeric value and returns a character string. The syntax is straightforward: new_char = put(numeric_var, format);. The format you choose determines the width, decimal places, and presentation of the result. For example, put(1234.56, 10.2) returns a character string with two decimal places and enough leading spaces to fill a width of 10.
Another common approach is using CAT functions, which automatically convert numeric arguments to character during concatenation. CATS, CATX, and their siblings are convenient for quick combinations of numbers and text. However, they rely on implicit conversion, which uses the BEST12. format by default. That default may not give you the decimal precision, leading zeros, or date formatting you need. For precise work, PUT remains the safer and more transparent choice.
The PUT Function: Explicit and Reliable
PUT is the workhorse for numeric-to-character conversion in SAS. It takes a numeric value and a format, then returns a character string whose length equals the format width. You can use a wide range of formats: BEST12. for general numbers, 8. for integers, 10.2 for decimals, comma12.2 for thousands separators, dollar12.2 for currency, z8. for leading zeros, and yymmdd10. for dates. For instance, if num = 123.45, then put(num, 8.2) produces a string with two leading spaces and the text "123.45".
Because PUT pads the result with leading blanks to match the format width, you often need to clean it up. The STRIP function removes both leading and trailing blanks, so strip(put(num, best12.)) gives you a compact string. If you need a specific storage length, declare it before assignment: length char_var $12; char_var = put(num, best12.);. This practice prevents SAS from guessing a length that may be too short or unnecessarily long. Remember that PUT never changes the original numeric variable; it only creates a new character value.
Handling Leading Blanks and Length with STRIP and TRIM
Leading blanks are the most frequent annoyance after numeric-to-character conversion. SAS numeric formats right-align values within the specified width, so a value like 42 under BEST12. becomes a string with ten leading spaces followed by "42". If you concatenate that directly, your report or ID field will contain ugly gaps. STRIP removes both leading and trailing blanks, while TRIM removes only trailing blanks. LEFT removes leading blanks but leaves trailing ones. For most numeric-to-character needs, STRIP is your best friend.
Consider this example: data _null_; num = 42; char1 = put(num, best12.); char2 = strip(put(num, best12.)); char3 = trim(left(put(num, best12.))); put char1= $char12. char2= $char12. char3= $char12.; run;. CHAR1 shows many leading spaces, CHAR2 is "42", and CHAR3 is also "42". However, STRIP does not shorten the stored variable length when assigned. To truly reduce storage, use a LENGTH statement before assignment, such as length char_short $2; char_short = strip(put(num, best12.));.
Using CAT Functions for Implicit Conversion
CAT functions such as CAT, CATS, CATT, and CATX concatenate character and numeric arguments, converting numerics to character automatically. CATS removes leading and trailing blanks, while CATX adds a delimiter. These functions are handy when you want to combine a number with text without writing a PUT statement. For example, data _null_; num = 123; text = "ID"; result = cats(text, num); put result=; run; yields "ID123". The numeric 123 is converted using BEST12. and then stripped.
While convenient, CAT functions give you less control over formatting. You cannot specify decimal places, date formats, or leading zeros directly. If the number needs to look like 00123, CATS will not produce that; you need PUT with the Z5. format. Also, CAT functions can return a length that is larger than necessary when creating new variables, which can waste memory in large datasets. Always use a LENGTH statement if storage matters. For precision work, PUT remains superior.
Advanced Techniques and Common Pitfalls
Once you master the basics, you will encounter more nuanced scenarios: converting dates and datetimes, controlling decimal precision, dealing with missing values, and avoiding implicit conversion in joins. SAS numeric to character conversion is not just about syntax; it is about understanding how SAS formats and lengths interact. A small mistake can lead to truncated IDs, incorrect report labels, or failed merges.
Another pitfall is assuming that all numeric values convert cleanly. Missing numeric values convert to a period or blank depending on the function and format, so test your specific case. Special missing values like .A and .B may also behave unexpectedly. Always inspect your converted values with PROC FREQ or a quick DATA step print before using them in production. The few minutes spent verifying output can save hours of downstream debugging.
Converting Dates, Datetimes, and Currency Values
Dates and datetimes are numeric in SAS, so converting them to character requires date or datetime formats. For a date variable, use PUT(date_var, yymmdd10.) to get a readable string such as "2025-01-15". For a datetime, use PUT(datetime_var, datetime20.) to get a readable timestamp. These formats produce business-friendly text. If you use BEST12., you will get the raw numeric value, such as 23456, which is meaningless to most report consumers. Always choose a format that matches your reporting need.
Currency values need special handling. PUT(num, dollar12.2) returns a string like " $1,234.56". If you need no dollar sign, use comma12.2. For percentages, use percent8.1. Remember that these formatted strings are still character variables. If you later need to sort them numerically, you must keep the original numeric variable or convert back with INPUT. Also, concatenating formatted currency with text can produce alignment issues; use STRIP or CATX to combine cleanly.
Controlling Width, Decimal Places, and Leading Zeros
Width and decimal places are controlled entirely by the format you choose in PUT. For integers, use a format like 8. or z8. The z format adds leading zeros, which is essential for fixed-length IDs, zip codes, or account numbers. For example, PUT(123, z5.) yields "00123". For decimals, use w.d where w is total width and d is decimal places. PUT(123.456, 8.2) yields " 123.46" after rounding to two decimals. If the number is too large for the width, SAS may return asterisks, so choose a width large enough for your maximum value.
When you need a specific length for the resulting character variable, declare it before assignment. For example: length id $5; id = put(num, z5.);. This ensures the variable is stored with length 5. If you omit the LENGTH statement, SAS determines the length from the format width. For BEST12., that is 12 characters. For z5., that is 5. For a format like comma12.2, that is 12. Always check the length of your new character variable using PROC CONTENTS or the LENGTH function to avoid truncation in later steps.
Avoiding Implicit Conversion in Joins and Where Clauses
Implicit conversion can silently ruin your merges and filters. When you join a numeric variable to a character variable, SAS may convert one side automatically. This conversion uses BEST12. for numerics, which can lead to leading blanks and mismatches. For example, if you have numeric customer_id = 123 and character customer_id = "123", a join might work after conversion, but if the character value has leading zeros ("00123"), the join fails because BEST12. does not add zeros. Always convert explicitly before joining or use the correct format on both sides.
In WHERE clauses, comparing a character variable to a numeric literal can trigger implicit conversion. For instance, where char_var = 123 may convert 123 to character using BEST12., which includes leading blanks. The comparison then becomes char_var = " 123", which is false if char_var is "123". Always compare like types: where char_var = "123". If you must compare character to numeric, convert explicitly with PUT or INPUT. This practice improves performance and prevents logical errors.
As you build your SAS programs, treat numeric-to-character conversion as a deliberate step rather than an afterthought. Choose PUT with a well-chosen format, strip away unwanted blanks, and declare lengths to keep your datasets lean and accurate. Test your conversions with edge cases like large numbers, negative values, missing data, and leading-zero requirements. The few extra minutes you invest in explicit conversion will pay off in cleaner reports, reliable merges, and fewer late-night debugging sessions. Keep your format catalog handy, and make conversion a standard part of your data preparation workflow.