BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

%let e=""xyz"" ;
%put %substr(&e,3,2);

when I am executing above programming I am getting error like

ERROR: Open code statement recursion detected.

 

 

6 REPLIES 6
AzizAA
Calcite | Level 5

You do not need the quotation marks around xyxz 

The problem is that the range specified is out of bounds. Try this

 

%let e=xyz;
%put %substr(&e,2,1);

thanikondharish
Fluorite | Level 6
Don't remove quotations
PaigeMiller
Diamond | Level 26

@thanikondharish wrote:
Don't remove quotations

meaning?

--
Paige Miller
andreas_lds
Jade | Level 19

@thanikondharish wrote:
Don't remove quotations

Quoting values when assigning macro-variables is a bad idea in most cases i have seen in the last decade. It is almost always better to use the quotes when you want the value of the variable inserted into sas code.

 

Astounding
PROC Star

First, figure out what you are working with.  Try this test initially:

 

%put **&e**;

FreelanceReinh
Jade | Level 19

Hello @thanikondharish,

 

Normally your code should work, as shown in the log below:

1    %let e=""xyz"";
2    %put %substr(&e,3,2);
xy

(Note that the four quotation marks got part of the macro variable value.)

 

So the error message is most likely the result of submitting different code before (see your log).

 

Examples include:

  • Missing semicolon after the %LET statement (see line 1 below). In this situation submit a single semicolon (line 3) to repair the damage. Then submit the %PUT statement again (line 4) and add the missing semicolon in the code.
    1    %let e=""xyz""
    2    %put %substr(&e,3,2);
    ERROR: Open code statement recursion detected.
    3    ;
    4    %put %substr(&e,3,2);
    xy
  • Unbalanced quotes in the %LET statement (see line 1 below). In this situation submit the missing quotation mark followed by a semicolon (line 3) to repair the damage. Then add the missing quotation mark in the code and submit both statements again (lines 4, 5).
    1    %let e=""xyz";
    2    %put %substr(&e,3,2);
    ERROR: Open code statement recursion detected.
    3    ";
    4    %let e=""xyz"";
    5    %put %substr(&e,3,2);
    xy
  • Unbalanced quote(s) in the expression in the %PUT statement (after resolution, see line 2 below) and then trying to submit one or both of the statements again (line 3) because the %PUT statement "didn't work." In this situation, similar to the previous bullet point, submit a quotation mark followed by a semicolon (line 4) to repair the damage. Then submit the corrected %PUT statement so that it doesn't create an unprotected, unbalanced quotation mark (line 5 or line 6). The %QSUBSTR function (line 6) masks the quotation mark and thus avoids the problem.
    1    %let e=""xyz"";
    2    %put %substr(&e,1,1);
    ERROR: Open code statement recursion detected.
    3    %put %substr(&e,1,1);
    4    ";
    ";"
    5    %put %substr(&e,3,2);
    xy
    6    %put %qsubstr(&e,1,1);
    "

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1515 views
  • 1 like
  • 6 in conversation