<?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: Proc SQL - IF/THEN Conditions in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533241#M73818</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id name $;
datalines;
1 Tim
2 Tom
3 Beth
4 Harry
;
run;

data two;
input id treatment_code;
datalines;
1 1
1 2
2 1
2 4
2 9
3 11
4 2
;
run;

proc sql;
create table want as
select a.*,ifc(max(treatment_code in (1,2)),'YES','NO') as In_teatment
from one a left join two b
on a.id=b.id
group by a.id,name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 Feb 2019 14:18:49 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-02-06T14:18:49Z</dc:date>
    <item>
      <title>Proc SQL - IF/THEN Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533141#M73815</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to do a conditional merge using proc SQL that:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Searches dataset &lt;EM&gt;two&lt;/EM&gt; within/grouped by&amp;nbsp;‘ID’ for treatment codes 1 or 2. Something like:By distinct id,&amp;nbsp;IF treatment_code in (1,2) then in_treatment = “Yes” else “No”.&lt;/LI&gt;&lt;/UL&gt;&lt;UL&gt;&lt;LI&gt;I want to merge datasets &lt;EM&gt;one&lt;/EM&gt; and&lt;EM&gt; two,&lt;/EM&gt; keeping all values from &lt;EM&gt;one&lt;/EM&gt; and adding the new variable in_treatment that is calculated from &lt;EM&gt;two&lt;/EM&gt;.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; &lt;EM&gt;one&lt;/EM&gt;;&lt;/P&gt;&lt;P&gt;input id name $;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 Tim&lt;/P&gt;&lt;P&gt;2 Tom&lt;/P&gt;&lt;P&gt;3 Beth&lt;/P&gt;&lt;P&gt;4 Harry&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; &lt;EM&gt;two&lt;/EM&gt;;&lt;/P&gt;&lt;P&gt;input id treatment_code;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 1&lt;/P&gt;&lt;P&gt;1 2&lt;/P&gt;&lt;P&gt;2 1&lt;/P&gt;&lt;P&gt;2 4&lt;/P&gt;&lt;P&gt;2 9&lt;/P&gt;&lt;P&gt;3 11&lt;/P&gt;&lt;P&gt;4 2&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;id name In_teatment&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp; Tim "Yes"&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp; Tom``"Yes"&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp; Beth "No"&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;4&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp; Harry "Yes"&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Feb 2019 05:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533141#M73815</guid>
      <dc:creator>eceklic</dc:creator>
      <dc:date>2019-02-06T05:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - IF/THEN Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533149#M73816</link>
      <description>&lt;P&gt;Data step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id name $;
datalines;
1 Tim
2 Tom
3 Beth
4 Harry
;
run;

data two;
input id treatment_code;
datalines;
1 1
1 2
2 1
2 4
2 9
3 11
4 2
;
run;

data two_comp;
set two;
by id;
retain in_treatment '   ';
if first.id then in_treatment = 'No';
if treatment_code in (1,2) then in_treatment = 'Yes';
if last.id then output;
drop treatment_code;
run;

data want;
merge
  one
  two_comp
;
by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SQL solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
  a.id,
  a.name,
  (select
    max(
      case
        when b.treatment_code in (1,2)
        then 'Yes'
        else 'No'
      end
    )
    from two b
    where a.id = b.id
    group by b.id
  ) as in_treatment
from one a
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result from both:&lt;/P&gt;
&lt;PRE&gt;id    name     in_treatment

 1    Tim          Yes     
 2    Tom          Yes     
 3    Beth         No      
 4    Harry        Yes     
&lt;/PRE&gt;
&lt;P&gt;Note how much better the code looks when using the "little running man" and {i} buttons for posting.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Feb 2019 06:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533149#M73816</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-06T06:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - IF/THEN Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533212#M73817</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id name $;
datalines;
1 Tim
2 Tom
3 Beth
4 Harry
;
run;

data two;
input id treatment_code;
datalines;
1 1
1 2
2 1
2 4
2 9
3 11
4 2
;
run;

proc sql;
select a.id,a.name,case when sum(treatment_code in (1,2)) &amp;gt;0 then 'Yes' else 'No ' end as want
 from one as a left join two as b 
  on a.id=b.id
   group by a.id,a.name;
 quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Feb 2019 13:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533212#M73817</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-02-06T13:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - IF/THEN Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533241#M73818</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id name $;
datalines;
1 Tim
2 Tom
3 Beth
4 Harry
;
run;

data two;
input id treatment_code;
datalines;
1 1
1 2
2 1
2 4
2 9
3 11
4 2
;
run;

proc sql;
create table want as
select a.*,ifc(max(treatment_code in (1,2)),'YES','NO') as In_teatment
from one a left join two b
on a.id=b.id
group by a.id,name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Feb 2019 14:18:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533241#M73818</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-06T14:18:49Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - IF/THEN Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533257#M73819</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id name $;
datalines;
1 Tim
2 Tom
3 Beth
4 Harry
;
run;

data two;
input id treatment_code;
datalines;
1 1
1 2
2 1
2 4
2 9
3 11
4 2
;
run;


data want;
merge one(in=a) two(in=b where=(treatment_code in (1,2)));
by id ;
if a;
if b then In_teatment='YES';
else In_teatment='NO';
if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Feb 2019 14:39:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533257#M73819</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-06T14:39:51Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - IF/THEN Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533261#M73820</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id name $;
datalines;
1 Tim
2 Tom
3 Beth
4 Harry
;
run;

data two;
input id treatment_code;
datalines;
1 1
1 2
2 1
2 4
2 9
3 11
4 2
;
run;

proc sql;
create table want as
select distinct a.*,ifc(treatment_code&amp;gt;.,'YES','NO') as In_teatment
from one a left join two(where=(treatment_code in (1,2))) b
on a.id=b.id;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Feb 2019 14:46:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533261#M73820</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-06T14:46:31Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - IF/THEN Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533267#M73821</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id name $;
datalines;
1 Tim
2 Tom
3 Beth
4 Harry
;
run;

data two;
input id treatment_code;
datalines;
1 1
1 2
2 1
2 4
2 9
3 11
4 2
;
run;

data want ;
set one;
if _n_=1 then do;
if 0 then set two;
   dcl hash H (dataset:'two',multidata:'y') ;
   h.definekey  ("id") ;
   h.definedata ("treatment_code") ;
   h.definedone () ;
end;
length In_teatment $3;
In_teatment='NO';
do while(h.do_over() eq 0);
if treatment_code in (1,2) then do;
In_teatment='YES';
leave;
end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
set one;
if _n_=1 then do;
if 0 then set two;
   dcl hash H (dataset:'two(where=(treatment_code in (1,2))',multidata:'y') ;
   h.definekey  ("id") ;
   h.definedata ("id") ;
   h.definedone () ;
end;
length In_teatment $3;
In_teatment='NO';
if h.check()=0 then In_teatment='YES';
drop treatment_code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Feb 2019 14:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-IF-THEN-Conditions/m-p/533267#M73821</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-06T14:59:29Z</dc:date>
    </item>
  </channel>
</rss>

