<?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: How to evaluate a character expression to TRUE / FALSE ? in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384999#M3673</link>
    <description>&lt;P&gt;Yes. As KSharp says, it is clear HOW to do it. The questions is WHY would we do it this way if there are more efficient alternatives. And we won't know that unless we understand the process by which the conditions are generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
start EvalStrCondition(x, s);
   cmd = "b = (" + s + ");";
   call execute(cmd);
   return b;
finish;

x = {1, 2 , 3, 4};
cond = {"x = 0", "x &amp;gt; 0", "x &amp;lt; 10", "x = 10"};

do i = 1 to 4;
   if EvalStrCondition(x[i], cond[i]) then do;
      print i;
   end;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 02 Aug 2017 14:57:23 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2017-08-02T14:57:23Z</dc:date>
    <item>
      <title>How to evaluate a character expression to TRUE / FALSE ?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384925#M3665</link>
      <description>&lt;P&gt;I'd like to evaluate&amp;nbsp;conditions that are stored in a character vector with an IF clause.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ideally, the code below would print 2 and 3, but you'll noticed I used the vague denominator &lt;EM&gt;SomeFunction&lt;/EM&gt;&amp;nbsp;when evaluating the condition. My question is: Is there such a function, and if so, what's it named ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	x = {1, 2 , 3, 4};
	cond = {"x[i] = 0", "x[i] &amp;gt; 0", "x[i] &amp;lt; 10", "x[i] = 10"};

	do i = 1 to 4;
		if SomeFunction(cond[i]) then do;
			print i;
		end;
	end;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If there is a 'cleaner' aproach to this, please let me know, I am not very familiar with IML so any advice is welcome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 12:46:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384925#M3665</guid>
      <dc:creator>BogdanC</dc:creator>
      <dc:date>2017-08-02T12:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to evaluate a character expression to TRUE / FALSE ?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384929#M3666</link>
      <description>&lt;P&gt;Why do you want to evaluate character strings? Are these conditions not known until run time?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The usual approach is to use the SAS logical operators ('&amp;amp;', '|' and '^') to form logical expressions:&lt;/P&gt;
&lt;P&gt;For example, if you want the AND logic, use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
x = {1, 2 , 3, 4};
do i = 1 to 4;
	if x[i] &amp;gt;= 0 &amp;amp; x[i] &amp;lt;= 10 then do;
		print i;
	end;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have very complicated logic and you want to isolate it in a function call, you can do the following, which returns 0 (FALSE) or 1 (TRUE) for various situations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start EvalArg(a);
   if a = 0 | a = 10 then 
      return 0;
   else if a &amp;gt; 0 &amp;amp; a &amp;lt; 10 then 
      return 1;
   else 
      return 0;
finish;

do i = 1 to 4;
   if EvalArg( x[i] ) then do;
      print i;
   end;
end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Aug 2017 12:33:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384929#M3666</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-08-02T12:33:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to evaluate a character expression to TRUE / FALSE ?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384931#M3667</link>
      <description>&lt;P&gt;Rick - thank you for the answer.&lt;BR /&gt;I think I didn't express myself&amp;nbsp;clearly enough the first time: for each element in &lt;EM&gt;&lt;STRONG&gt;x&lt;/STRONG&gt;&lt;/EM&gt; there is a condition in &lt;EM&gt;&lt;STRONG&gt;cond&lt;/STRONG&gt;&lt;/EM&gt;. That condition can be different for each element, and I only need the elements in &lt;EM&gt;&lt;STRONG&gt;x&lt;/STRONG&gt;&lt;/EM&gt; for which the corresponding condition in &lt;STRONG&gt;&lt;EM&gt;cond&lt;/EM&gt;&lt;/STRONG&gt; evaluates to TRUE.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am NOT&amp;nbsp;trying to concatenate all conditions into one and there is no connection bewtween the elements of &lt;EM&gt;&lt;STRONG&gt;cond&lt;/STRONG&gt;&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully this makes more sense now.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 12:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384931#M3667</guid>
      <dc:creator>BogdanC</dc:creator>
      <dc:date>2017-08-02T12:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to evaluate a character expression to TRUE / FALSE ?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384933#M3669</link>
      <description>&lt;P&gt;You didn't answer my question:&amp;nbsp;&lt;SPAN&gt;Why do you want to evaluate character strings? Are these conditions known at the time that you write the program?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 12:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384933#M3669</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-08-02T12:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to evaluate a character expression to TRUE / FALSE ?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384937#M3670</link>
      <description>&lt;P&gt;Some conditions are known, but some&amp;nbsp;cannot be known beforehand (for instance, depending on the data, the variables that&amp;nbsp;go into the condition are completely different).&lt;/P&gt;&lt;P&gt;The reason the 'fixed' conditions are in&amp;nbsp;character strings is that they are read from an Excel file - the code has grown to a lot of line and&amp;nbsp;it's easier to manage&amp;nbsp;parameters and condition via Excel spraedsheets.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 12:53:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384937#M3670</guid>
      <dc:creator>BogdanC</dc:creator>
      <dc:date>2017-08-02T12:53:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to evaluate a character expression to TRUE / FALSE ?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384953#M3671</link>
      <description>&lt;P&gt;You say the conditions&amp;nbsp;are data dependent, but how are they determined? SAS/IML can be programmed to incorporate data-dependent conditions such as&lt;/P&gt;
&lt;P&gt;if x[i] &amp;lt; max(x) then ...&lt;/P&gt;
&lt;P&gt;or&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if x[i] &amp;gt;= mean(x) + std(x) then ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of asking "how&amp;nbsp;to evaluate a character expression in a spreadsheet," why not tell us what you are trying to accomplish. there might be an easier way to program the analysis.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 13:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384953#M3671</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-08-02T13:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to evaluate a character expression to TRUE / FALSE ?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384994#M3672</link>
      <description>&lt;P&gt;Check CALL EXECUTEFILE()&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;another way is using data step to make a macro and execute it by %include &amp;nbsp;or CALL EXECUTE()&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 14:50:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384994#M3672</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-08-02T14:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to evaluate a character expression to TRUE / FALSE ?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384999#M3673</link>
      <description>&lt;P&gt;Yes. As KSharp says, it is clear HOW to do it. The questions is WHY would we do it this way if there are more efficient alternatives. And we won't know that unless we understand the process by which the conditions are generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
start EvalStrCondition(x, s);
   cmd = "b = (" + s + ");";
   call execute(cmd);
   return b;
finish;

x = {1, 2 , 3, 4};
cond = {"x = 0", "x &amp;gt; 0", "x &amp;lt; 10", "x = 10"};

do i = 1 to 4;
   if EvalStrCondition(x[i], cond[i]) then do;
      print i;
   end;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Aug 2017 14:57:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-evaluate-a-character-expression-to-TRUE-FALSE/m-p/384999#M3673</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-08-02T14:57:23Z</dc:date>
    </item>
  </channel>
</rss>

