<?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: Using column value as condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943733#M369857</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data person;
infile cards dsd dlm=' ';
input name $ condition : $30.;
datalines;
John "name not in ('Mary')"
Mary "name not in ('Jack', 'Bob')"
;

data _null_;
set person;
call execute(cat('data pass error;set person; if ',condition,' then output error;else output pass;run;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 13 Sep 2024 01:02:01 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-09-13T01:02:01Z</dc:date>
    <item>
      <title>Using column value as condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943723#M369851</link>
      <description>&lt;P&gt;Hello all. I have two columns, a name and a condition.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data person;&lt;BR /&gt;input name $ condition $;&lt;BR /&gt;datalines;&lt;BR /&gt;John "name not in ('Mary')"&lt;BR /&gt;Mary "name not in ('Jack', 'Bob')"&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I use condition when looping through the data step?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is some code that doesn't work but tries to get what I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data pass error;&lt;BR /&gt;set person;&lt;BR /&gt;call symput('criteria', condition);&lt;BR /&gt;if not(&amp;amp;criteria.) then output error;&lt;BR /&gt;else output pass;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 22:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943723#M369851</guid>
      <dc:creator>cyz</dc:creator>
      <dc:date>2024-09-12T22:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using column value as condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943731#M369856</link>
      <description>&lt;P&gt;If I understand right what you're trying to do then below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data person;
  infile datalines truncover dsd dlm=' ';
  input name $ condition $100.;
  datalines;
John name not in ('Mary')
Mary name not in ('Jack', 'Bob')
;

filename codegen temp;
data _null_;
  file codegen;
  set person end=last;
  if _n_&amp;gt;1 then put 'else ' @;
  put 'if ' condition 'then output pass;';
  if last then put 'else output error;';
run;

data error pass;
  set person;
  %include codegen / source2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Generated code from SAS log:&lt;/P&gt;
&lt;PRE&gt;45         data error pass;
46           set person;
47           %include codegen / source2;
NOTE: %INCLUDE (level 1) file CODEGEN is file 
      C:\Users\*****\AppData\Roaming\SAS\EnterpriseGuide\EGTEMP\SEG-11160-3cba2e81\contents\SAS Temporary 
      Files\_TD1604_SSAPAM3_\#LN00150.
48        +if name not in ('Mary') then output pass;
49        +else if name not in ('Jack', 'Bob') then output pass;
50        +else output error;
NOTE: %INCLUDE (level 1) ending.
51         run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...but then looking at your sample data may be you want below code generated?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename codegen temp;
data _null_;
  file codegen;
  set person end=last;
  _cond=condition;
  _cond=tranwrd(_cond,'name',cats("'",name,"'"));
  if _n_&amp;gt;1 then put 'else ' @;
  put 'if ' _cond 'then output pass;';
  if last then put 'else output error;';
run;

data error pass;
  set person;
  %include codegen / source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;47         data error pass;
48           set person;
49           %include codegen / source2;
NOTE: %INCLUDE (level 1) file CODEGEN is file 
      C:\Users\*****\AppData\Roaming\SAS\EnterpriseGuide\EGTEMP\SEG-11160-3cba2e81\contents\SAS Temporary 
      Files\_TD1604_SSAPAM3_\#LN00198.
50        +if 'John' not in ('Mary') then output pass;
51        +else if 'Mary' not in ('Jack', 'Bob') then output pass;
52        +else output error;
NOTE: %INCLUDE (level 1) ending.
53         run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 01:12:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943731#M369856</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-09-13T01:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using column value as condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943733#M369857</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data person;
infile cards dsd dlm=' ';
input name $ condition : $30.;
datalines;
John "name not in ('Mary')"
Mary "name not in ('Jack', 'Bob')"
;

data _null_;
set person;
call execute(cat('data pass error;set person; if ',condition,' then output error;else output pass;run;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2024 01:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943733#M369857</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-09-13T01:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using column value as condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943734#M369858</link>
      <description>&lt;P&gt;The CODE of the data step is compiled BEFORE the step starts running.&amp;nbsp; So you cannot reference the value of a macro variable created while the step is running&amp;nbsp; to change the code since you are already past that point in time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use DATA as CODE then the easiest thing is to write it to a code that that you can %INCLUDE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I could not make any sense out of your example so here is a slightly different one that uses TWO datasets, one with the conditions and another which is the data that will be evaulated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data conditions;
  infile datalines dsd dlm=' ';
  input result $ condition :$40.;
datalines;
AAA "name in ('Mary')"
BBB "name in ('Jack', 'Bob')"
;

data person;
  input name $;
datalines;
Mary
Jack
Bob
Fred
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So convert the conditions into code.&amp;nbsp; Perhaps a series of IF/THEN/ELSE statements?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set conditions;
  if _n_&amp;gt;1 then put 'else ' @;
  put 'if (' condition ') then ' result= $quote. ';' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use that code in a data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set person;
%include code / source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    name    result

 1     Mary     AAA
 2     Jack     BBB
 3     Bob      BBB
 4     Fred

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 01:03:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-column-value-as-condition/m-p/943734#M369858</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-13T01:03:12Z</dc:date>
    </item>
  </channel>
</rss>

