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?
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 */
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
@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.
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;
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?
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.
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.