<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Error using if then else loop in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713489#M27163</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;IF/THEN versus IF/THEN/DO, if there are multiple statements after the IF statement it requires a DO/END block as well.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;The ELSE condition is incorrect. Should that be assigned to a variable ? Not sure what the else should do so just made a guess.&lt;/LI&gt;
&lt;LI&gt;There's a missing semicolon on the LENGTH statement.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361602"&gt;@SAS_New_User1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am running the below query and I keep getting the error&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data A;&lt;/P&gt;
&lt;P&gt;set B;&lt;/P&gt;
&lt;P&gt;length str_2 $1000&lt;/P&gt;
&lt;P&gt;if&lt;/P&gt;
&lt;P&gt;(&lt;/P&gt;
&lt;P&gt;startpos = index(str_1, "START")&lt;/P&gt;
&lt;P&gt;and&amp;nbsp;&lt;/P&gt;
&lt;P&gt;endpos - index(str_1, "STOP")&lt;/P&gt;
&lt;P&gt;)&lt;/P&gt;
&lt;P&gt;then&amp;nbsp;&lt;/P&gt;
&lt;P&gt;str_2 = substr(str, startpos+7, endpos-(startpos+7));&lt;/P&gt;
&lt;P&gt;str_1 = cats(substr(str_1, 1, startpos), substr(str_1, endpos));&lt;/P&gt;
&lt;P&gt;else str_1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am getting the below error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Error: No matching ID-THEN clause&lt;/P&gt;
&lt;P&gt;Error: Statement is not valid or it is not used out of proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 22 Jan 2021 21:35:40 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-01-22T21:35:40Z</dc:date>
    <item>
      <title>Error using if then else loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713485#M27161</link>
      <description>&lt;P&gt;I am running the below query and I keep getting the error&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data A;&lt;/P&gt;
&lt;P&gt;set B;&lt;/P&gt;
&lt;P&gt;length str_2 $1000&lt;/P&gt;
&lt;P&gt;if&lt;/P&gt;
&lt;P&gt;(&lt;/P&gt;
&lt;P&gt;startpos = index(str_1, "START")&lt;/P&gt;
&lt;P&gt;and&amp;nbsp;&lt;/P&gt;
&lt;P&gt;endpos - index(str_1, "STOP")&lt;/P&gt;
&lt;P&gt;)&lt;/P&gt;
&lt;P&gt;then&amp;nbsp;&lt;/P&gt;
&lt;P&gt;str_2 = substr(str, startpos+7, endpos-(startpos+7));&lt;/P&gt;
&lt;P&gt;str_1 = cats(substr(str_1, 1, startpos), substr(str_1, endpos));&lt;/P&gt;
&lt;P&gt;else str_1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am getting the below error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Error: No matching ID-THEN clause&lt;/P&gt;
&lt;P&gt;Error: Statement is not valid or it is not used out of proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 21:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713485#M27161</guid>
      <dc:creator>SAS_New_User1</dc:creator>
      <dc:date>2021-01-22T21:23:53Z</dc:date>
    </item>
    <item>
      <title>Re: Error using if then else loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713487#M27162</link>
      <description>&lt;P&gt;If you need to run more than one statement conditionally, use a DO-END:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 */&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jan 2021 21:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713487#M27162</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-22T21:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Error using if then else loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713489#M27163</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;IF/THEN versus IF/THEN/DO, if there are multiple statements after the IF statement it requires a DO/END block as well.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;The ELSE condition is incorrect. Should that be assigned to a variable ? Not sure what the else should do so just made a guess.&lt;/LI&gt;
&lt;LI&gt;There's a missing semicolon on the LENGTH statement.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361602"&gt;@SAS_New_User1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am running the below query and I keep getting the error&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data A;&lt;/P&gt;
&lt;P&gt;set B;&lt;/P&gt;
&lt;P&gt;length str_2 $1000&lt;/P&gt;
&lt;P&gt;if&lt;/P&gt;
&lt;P&gt;(&lt;/P&gt;
&lt;P&gt;startpos = index(str_1, "START")&lt;/P&gt;
&lt;P&gt;and&amp;nbsp;&lt;/P&gt;
&lt;P&gt;endpos - index(str_1, "STOP")&lt;/P&gt;
&lt;P&gt;)&lt;/P&gt;
&lt;P&gt;then&amp;nbsp;&lt;/P&gt;
&lt;P&gt;str_2 = substr(str, startpos+7, endpos-(startpos+7));&lt;/P&gt;
&lt;P&gt;str_1 = cats(substr(str_1, 1, startpos), substr(str_1, endpos));&lt;/P&gt;
&lt;P&gt;else str_1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am getting the below error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Error: No matching ID-THEN clause&lt;/P&gt;
&lt;P&gt;Error: Statement is not valid or it is not used out of proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 21:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713489#M27163</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-01-22T21:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: Error using if then else loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713490#M27164</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Currently if the string either does not find START or STOP it replaces the str_1(original data) with additional data from the string.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 21:36:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713490#M27164</guid>
      <dc:creator>SAS_New_User1</dc:creator>
      <dc:date>2021-01-22T21:36:11Z</dc:date>
    </item>
    <item>
      <title>Re: Error using if then else loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713495#M27165</link>
      <description>Hi Reeza,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;So I want to tell the code if A exists then do B, else keep A the same;&lt;BR /&gt;&lt;BR /&gt;The above code is changing Str_1 if either of the START or STOP are missing</description>
      <pubDate>Fri, 22 Jan 2021 21:46:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713495#M27165</guid>
      <dc:creator>SAS_New_User1</dc:creator>
      <dc:date>2021-01-22T21:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: Error using if then else loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713498#M27166</link>
      <description>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. &lt;BR /&gt;&lt;BR /&gt;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.</description>
      <pubDate>Fri, 22 Jan 2021 21:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713498#M27166</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-01-22T21:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: Error using if then else loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713502#M27167</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361602"&gt;@SAS_New_User1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Currently if the string either does not find START or STOP it replaces the str_1(original data) with additional data from the string.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then you do not need the ELSE branch at all.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 22:05:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713502#M27167</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-22T22:05:41Z</dc:date>
    </item>
    <item>
      <title>Re: Error using if then else loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713505#M27168</link>
      <description>In this case it is not updating anything in STR_2 column.</description>
      <pubDate>Fri, 22 Jan 2021 22:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Error-using-if-then-else-loop/m-p/713505#M27168</guid>
      <dc:creator>SAS_New_User1</dc:creator>
      <dc:date>2021-01-22T22:10:58Z</dc:date>
    </item>
  </channel>
</rss>

