Skip to main content
v2026.11,986 entries · CC-BY 4.0

SAS PUT Numeric to Character: Formats and Examples

Convert SAS numeric values into text with explicit formats, deliberate lengths and checks for padding, missing values and truncation.

Written and maintained by CASRAI Editorial Board

Published

On this page

To convert a numeric value to character in SAS, assign the result of PUT(numeric_variable, format.) to a new character variable. For example, id_text = put(id, z6.); produces a six-position, zero-padded identifier. Choose the format and receiving length deliberately, then check both the text and the variable type.

What it is used for in research

Numeric-to-character conversion is useful when a research-data workflow needs a textual identifier or a formatted measurement string. The task has two separate decisions: what the text should look like, and which variable will store it. SAS’s PUT reference defines a function that returns a formatted value; it does not change an existing numeric column into a character column in place.

For example, a numeric participant code of 42 can become the character identifier 000042 when the intended identifier convention has six positions. A score can remain numeric for analysis while a second variable holds a two-decimal representation for a text output. These are different uses of the same function: one applies an identifier convention, while the other creates presentation text.

Keep those purposes explicit. In the worked example below, the original numeric variables remain available alongside the derived strings. This makes it possible to review the conversion before deciding whether to remove an input column. If the unexpected type originated during file ingestion, examine the PROC IMPORT variable-type checks first; converting the result is a separate operation from deciding how the source file should be read.

Install or access

Use an existing SAS programming environment to run the DATA step and procedures shown here. PUT is documented as a SAS function, so the conversion requires no separate conversion package. The instructions use SAS language statements rather than interface menus, allowing the program to be entered in the code editor supplied by your SAS environment.

For learning with the illustrative data, SAS’s official OnDemand for Academics support page provides a cloud route to SAS Studio. It directs independent learners to its Getting Started instructions and identifies the service as available for learning purposes. Follow that official route to obtain access. The example is a small practice dataset; access to a learning service should not be treated as approval to move an institution’s research records there.

Step-by-step task: convert numeric values with PUT

The basic pattern is new_text = put(old_number, numeric_format.);. The format describes the numeric source even though the returned value is character. Use a new receiving name, declare sufficient character length, and inspect the result before replacing any original column.

  1. Inspect the input variable. Run proc contents data=your_data; run; and check its type. A value that looks like digits on screen is not sufficient evidence that the underlying variable is numeric. SAS Sample 24590 specifically recommends PROC CONTENTS for checking conversion results and variable attributes.
  2. Choose the intended text representation. For the six-position example identifier, use z6.. For a measurement string with two decimal places in an eight-position field, use 8.2. The width includes punctuation and a minus sign where needed; it is not simply a count of digits before the decimal.
  3. Declare the receiving character columns. The example uses length id_text $6 score_text $8;. PUT otherwise derives the length of a previously undefined receiving variable from the format width. An explicit declaration makes the intended storage visible alongside the conversion.
  4. Assign the converted values. The example checks missing identifiers before formatting them. It uses a blank character value for a missing identifier, while retaining the numeric source for review.
  5. Inspect values and metadata. PROC PRINT shows the original and derived columns together. PROC CONTENTS checks that the derived columns are character and have the intended lengths. Both checks matter because printed output alone can conceal padding or an unintended type.

Worked example: identifiers and measurement text

This illustrative composite uses invented practice records, not observations from a study. The identifiers are nonnegative integers, and the chosen text convention is six positions. Use different dataset names if these example names already identify work you need to keep.

data work.put_demo_input;
  input id score;
  datalines;
7 12.5
42 0
1350 -3.25
. 8.25
;
run;

data work.put_demo_output;
  set work.put_demo_input;
  length id_text $6 score_text $8;
  if missing(id) then id_text = '';
  else id_text = put(id, z6.);
  score_text = strip(put(score, 8.2));
run;

proc print data=work.put_demo_output;
  var id id_text score score_text;
run;

proc contents data=work.put_demo_output;
run;

The expected identifier strings are 000007, 000042 and 001350, followed by a blank identifier. The score strings begin with 12.50, 0.00, -3.25 and 8.25. Their receiving column still has a fixed length of eight, so shorter values have trailing padding after assignment.

The missing-value branch is an explicit policy for this example. SAS’s MISSING function recognizes ordinary numeric missing values and special numeric missing values. Mapping all of them to a blank string removes their distinctions from the text output. Preserve the original variable, or design a different mapping, when those distinctions carry meaning in your data.

Keep the original name after conversion

To give the resulting character variable the original name, SAS documents creating the new column, dropping the numeric column and renaming the new one. Apply that pattern in a new output dataset after checking the conversion. Do not assign the string back to an existing numeric variable and expect its type to change.

data work.put_demo_replaced;
  set work.put_demo_input;
  length id_text $6;
  if missing(id) then id_text = '';
  else id_text = put(id, z6.);
  drop id;
  rename id_text=id;
run;

Here, the output column named id is the renamed character column. The input dataset remains the source of the numeric values. Inspect the output with PROC CONTENTS before handing it to another step that expects a particular column type.

Output and how to read it

Read the output in two layers: the representation of each value and the metadata of the receiving variable. In this example, id_text should be character with length six, and score_text should be character with length eight. Use the PROC CONTENTS output guide to interpret the variable listing.

The formats describe different output rules. A basic 12. format gives a numeric value a twelve-position field without a decimal point. 8.2 requests two decimal places. z6. pads the left side with zeros. The data table below compares these choices with the PUT alignment modifier, STRIP and PUTN, including their different length behavior.

Padding and length are separate. A numeric source is right-aligned by default, so put(id, 12.) can return leading blanks. put(id, 12. -L) requests left alignment. strip(put(id, 12.)) removes surrounding blanks from the intermediate string, but storing that result in a fixed-length character variable adds trailing padding as needed. STRIP does not reduce that variable’s declared length.

Once a value is character, later string operations act on its text representation. If a subsequent task needs part of the identifier, the SAS SUBSTR extraction guide explains that separate step. Establish the desired padding and width before extracting positions; otherwise, the position you select may contain a padding blank rather than the digit you intended.

Troubleshooting

The column is still numeric

Check the receiving variable, not just the function call. SAS states that PUT cannot directly change a dataset variable’s type from numeric to character. An assignment to a new character variable is the conversion pattern; DROP and RENAME provide the documented route when the output must retain the original name. Check the final output metadata after the rename.

The format has a dollar sign or is quoted

For a numeric source, use a numeric format such as z6. or best32.; a character format beginning with a dollar sign is the wrong format type. The period is part of the format specification. PUT expects the format specification in the program, rather than a quoted format name or a variable containing that name.

When the format must be selected at run time, the documented alternative is PUTN. For example, id_text = putn(id, 'z6.'); supplies the format as character data. Declare the receiving length deliberately: unlike PUT’s format-width default, PUTN gives a previously undefined receiving variable a default length of 200 bytes.

The result is truncated or has too few digits

Review both the format width and the receiving length. They control different stages: formatting produces the string, and assignment places it in the receiving column. Increasing only the receiving length cannot recover digits omitted when the value was formatted. Increasing only the format width cannot make a short receiving column store the whole result.

SAS’s w.d reference describes rounding to a number that fits the output field and possible fallback to BEST when the field is too small. BEST can choose scientific notation, omit trailing zeros and, when neither decimal nor scientific notation fits, fill the field with asterisks. A successful conversion therefore does not establish that a string preserves every detail needed for later analysis.

Leading zeros do not match the source records

The Z format supplies zeros to a specified width; it does not discover the historical spelling of an identifier. In the example, six positions are a stated convention. If different source identifiers had different meaningful widths, applying one Z format would impose a new representation rather than recover those original strings. Use the identifier definition to establish the required width.

A PUT statement prints a value but creates no column

The PUT function and PUT statement have different jobs. A function call such as id_text = put(id, z6.); returns a value that the assignment stores. A statement such as put id z6.; writes externally, including to the SAS log when applicable. Seeing formatted text in a log does not prove a new dataset column exists.

Free alternatives

R and Python provide freely available ways to format numeric values as strings when the surrounding workflow uses those languages. Their syntax differs from SAS formats. The following small examples illustrate the same six-position identifier and two-decimal presentation goals; they are not translations of the complete SAS dataset program.

Tool Illustrative commands How to read the result
R sprintf('%06d', 42)
sprintf('%.2f', 12.5)
Returns character values 000042 and 12.50. The official sprintf documentation describes integer conversion, zero padding and fixed-decimal formatting.
Python format(42, '06d')
format(12.5, '.2f')
Returns strings 000042 and 12.50. Python’s format specification defines integer output, field width, zero padding and fixed-point precision.

R’s project page identifies it as Free Software under the GNU GPL, and Python’s official site describes its open-source license as allowing free use and distribution. For either alternative, handle missing values explicitly and check the result against the receiving system’s expectations. Matching these two sample strings does not establish identical behavior for every SAS format, missing-value code or numeric value.

Sources

Data: SAS numeric-to-character expressions: formats and receiving lengths

Original compilation from SAS function and format reference entries linking six numeric-to-character expressions to their output rules and default receiving lengths. Lengths apply to a previously undefined character variable in a DATA step; an earlier declaration takes precedence.

SAS numeric-to-character expressions: formats and receiving lengths
ExpressionOutput ruleDefault new variable length (bytes)Review point and reference
put(id, z6.)Numeric output padded on the left with zeros6Choose a width that accommodates the value; rounding and width restrictions apply. SAS Zw.d format and PUT entries.
put(score, 8.2)Numeric output with two decimal places in a width-eight field8Width includes the sign and decimal point. Too-narrow fields can change representation. SAS w.d format and PUT entries.
put(value, best32.)Width-dependent numeric representation; may use scientific notation32Trailing zeros are not written; not a fixed-decimal representation. SAS BESTw. format and PUT entries.
put(id, 12. -L)Left-aligned numeric output in a width-twelve field12Alignment changes placement within the field, not the receiving length. SAS PUT entry.
strip(put(id, 12.))Removes surrounding blanks from the intermediate character string12Assignment does not shorten the receiving variable; trailing blanks can be added again. SAS STRIP and PUT entries.
putn(id, 'z6.')Numeric format supplied at run time200Declare the target length explicitly when the default is unsuitable. SAS PUTN entry.

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about SAS PUT Numeric to Character: Formats and Examples

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →