BookmarkSubscribeRSS Feed
SAS_New_User1
Obsidian | Level 7

I am running the below query and I keep getting the error

 

data A;

set B;

length str_2 $1000

if

(

startpos = index(str_1, "START")

and 

endpos - index(str_1, "STOP")

)

then 

str_2 = substr(str, startpos+7, endpos-(startpos+7));

str_1 = cats(substr(str_1, 1, startpos), substr(str_1, endpos));

else str_1;

run;

 

I am getting the below error:

 

Error: No matching ID-THEN clause

Error: Statement is not valid or it is not used out of proper order.

 

What am I doing wrong?

 

7 REPLIES 7
Kurt_Bremser
Super User

If you need to run more than one statement conditionally, use a DO-END:

then do;
  str_2 = substr(str, startpos+7, endpos-(startpos+7));
  str_1 = cats(substr(str_1, 1, startpos), substr(str_1, endpos));
end;
else str_1; /* zhis is not a valid statement */
SAS_New_User1
Obsidian | Level 7

I am trying to use IF then loop and see if I am able to find START and STOP, if not then I want to keep the data as is, I.e. the str_1 should not change in that case. 

 

Currently if the string either does not find START or STOP it replaces the str_1(original data) with additional data from the string. 

 

So I am trying to say that if the START and STOP exists in the query do the conditions else keep the data the same 

Kurt_Bremser
Super User

@SAS_New_User1 wrote:

I am trying to use IF then loop and see if I am able to find START and STOP, if not then I want to keep the data as is, I.e. the str_1 should not change in that case. 

 

Currently if the string either does not find START or STOP it replaces the str_1(original data) with additional data from the string. 

 

So I am trying to say that if the START and STOP exists in the query do the conditions else keep the data the same 


Then you do not need the ELSE branch at all.

SAS_New_User1
Obsidian | Level 7
In this case it is not updating anything in STR_2 column.
Reeza
Super User
data A;
set B;

length str_2 $1000;
if (startpos = index(str_1, "START") and endpos - index(str_1, "STOP")) then 
     do;
           str_2 = substr(str, startpos+7, endpos-(startpos+7));
           str_1 = cats(substr(str_1, 1, startpos), substr(str_1, endpos));
     end; 
else str_2 = str_1;

run;

 

  1. IF/THEN versus IF/THEN/DO, if there are multiple statements after the IF statement it requires a DO/END block as well. 
  2. The ELSE condition is incorrect. Should that be assigned to a variable ? Not sure what the else should do so just made a guess.
  3. There's a missing semicolon on the LENGTH statement.

 

FYI - formatting the code makes it easier to see these issues. Highlight the code and press CTRL+I in EG or click the format code button in SAS Studio to format your code before posting.

 


@SAS_New_User1 wrote:

I am running the below query and I keep getting the error

 

data A;

set B;

length str_2 $1000

if

(

startpos = index(str_1, "START")

and 

endpos - index(str_1, "STOP")

)

then 

str_2 = substr(str, startpos+7, endpos-(startpos+7));

str_1 = cats(substr(str_1, 1, startpos), substr(str_1, endpos));

else str_1;

run;

 

I am getting the below error:

 

Error: No matching ID-THEN clause

Error: Statement is not valid or it is not used out of proper order.

 

What am I doing wrong?

 


 

SAS_New_User1
Obsidian | Level 7
Hi Reeza,

I do not want STR_1 to change. Currently because of the cats function in the do loop if it doesn't find either START or strop. It will concatenate extra string at at the back of STR_1.

So I want to tell the code if A exists then do B, else keep A the same;

The above code is changing Str_1 if either of the START or STOP are missing
Reeza
Super User
I can only comment on the syntax issues. If the code has logical issues, you need to provide data, explain what logic you're trying to implement and then we can help you out from there.

Note that A is a data set, so you're checking if your data set exists within a data set? A very clear, detailed example will help you get an answer quickly.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1064 views
  • 0 likes
  • 3 in conversation