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

SAS SUBSTRN Function: Syntax and SUBSTRN vs SUBSTR

SAS SUBSTRN syntax, worked examples and how it differs from SUBSTR on out-of-range, missing and left-side use, from SAS documentation.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

SUBSTRN is a SAS function that extracts part of a character string, starting at a position you give and running for a length you give. Its syntax is SUBSTRN(string, position <, length>). Unlike SUBSTR, it does not write a note to the log or set _ERROR_ when the position or length falls outside the string; it returns a shortened or zero-length result instead. This page covers the syntax, the edge cases that separate it from SUBSTR, and how to read the output. Behaviour is taken from the SAS 9.2 function reference and SAS Usage Note 30911 (both read 2026-11-08).

What SUBSTRN is used for in research

Analysts use SUBSTRN to pull fixed-position pieces out of coded text variables, such as a site code, a year or a record prefix inside an identifier, while cleaning a dataset in a DATA step. Its advantage over SUBSTR is predictability on messy data: when some records are shorter than expected, or a computed position or length can be zero, negative or missing, SUBSTRN returns a defined result rather than flooding the log with notes.

Install or access (official source only)

SUBSTRN is part of Base SAS, so it is available wherever SAS is licensed and running; there is nothing separate to install. The authoritative reference is the SAS documentation at support.sas.com, listed under Sources. Documentation for your own SAS release is the version to trust for anything not covered here.

Step-by-step: extract a substring with SUBSTRN

  1. Open a program in your SAS interface and start a DATA step that reads the dataset holding the character variable.
  2. Set the string argument to the variable (or a constant or expression). A numeric argument is accepted and is converted using the BEST32. format; leading and trailing blanks from that conversion are removed without a log message.
  3. Set position to the integer position of the first character you want.
  4. Optionally set length to the number of characters to return. If you omit it, the substring runs from position to the end of the string.
  5. Assign the result to a new variable on the left of the equals sign, for example part = substrn(id, 2, 3);.
  6. Give the receiving variable a LENGTH statement before first use. In a DATA step, a variable with no prior length takes the length of the function’s first argument.

Worked example from the SAS documentation

For the string "abcd", the SAS reference shows these results:

Call Result
SUBSTRN("abcd", 0, 2) a
SUBSTRN("abcd", 2, 3) bcd

In the first row, a position of 0 is non-positive, so the result is cut at the beginning: it starts at the first character and the length shrinks accordingly, giving one character. In the second row the requested length runs past the end of the string, so the result is truncated at the last character.

SUBSTRN vs SUBSTR: edge cases side by side

This is the practical difference, per the SAS 9.2 reference.

Situation SUBSTRN SUBSTR
Position is zero or negative Result is cut at the beginning, starts at the first character, and the length shrinks Writes a note to the log, sets _ERROR_ to 1, returns from the position to the end of the string as documented
Length is zero or negative Returns a zero-length string Writes a note, sets _ERROR_ to 1, returns from the position to the end of the string
Length runs past the end of the string Truncated at the last character Writes a note, sets _ERROR_ to 1
Position or length is missing Returns a zero-length string See the SAS reference for SUBSTR with missing arguments
Left side of an assignment Not allowed Allowed

Output and how to read it

The result is a character string. A zero-length result is not an error: it means the position or length you supplied was missing, or the length was non-positive. If you expected text and got nothing, check the arguments before suspecting the data.

With a numeric argument, SUBSTRN converts the number using BEST32. first. The SAS reference example uses 1234.5678: SUBSTRN(1234.5678, 2, 6) returns 234.56, while the same call with SUBSTR yields a result with a leading-blank problem and is not equivalent.

Troubleshooting

“ERROR: Undeclared array referenced: substrn.”

SAS Usage Note 30911 states that SUBSTRN can be used on the right side of an assignment only. Placing it on the left of the equals sign makes SAS treat it as an array reference, producing ERROR: Undeclared array referenced: substrn. and ERROR: Variable substrn has not been declared as an array. To overwrite part of a string, use SUBSTR on the left side instead.

Result is truncated or the wrong length

Because a new variable takes the length of the first argument when no length was set earlier, declare the length yourself if the result must be a different size. Inside %SYSFUNC, the macro processor resolves the first argument before the call, so use macro quoting when the text contains special characters.

Non-English text

The SAS 9.3 reference lists KSUBSTR as the equivalent for internationalised (multibyte) text; check your release documentation before relying on SUBSTRN for such data.

Free alternatives

Tool Approach
R Base substr() or the stringr package; see how to load data into R and data frames in R
Python String slicing with text[start:stop]

These tools define their own indexing and out-of-range behaviour, so do not assume results match SUBSTRN on edge cases. For another statistics package, see SPSS statistical analysis software.

FAQ

What is the SAS SUBSTRN function?

A Base SAS function that returns a substring defined by a start position and optional length, using the syntax SUBSTRN(string, position <, length>).

What is the difference between SUBSTRN and SUBSTR?

SUBSTR writes a log note and sets _ERROR_ to 1 when the position or length is invalid or runs past the string; SUBSTRN returns a truncated or zero-length result. SUBSTR can also appear on the left of an assignment; SUBSTRN cannot.

What does SUBSTRN return when the length is omitted?

The substring from the given position to the end of the string.

What does SUBSTRN return for a missing position or length?

A string with a length of zero.

Sources

  • SAS 9.2 Functions and CALL Routines: SUBSTRN Function, support.sas.com (read 2026-11-08): documentation page
  • SAS Usage Note 30911, support.sas.com (read 2026-11-08): usage note

Follow CASRAI

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

Ask CASRAI · free to try

Ask about SAS SUBSTRN Function: Syntax and SUBSTRN vs SUBSTR

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

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 free 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 →