<?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 set a variable in a table based on multiple records in another table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812457#M320566</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way.&amp;nbsp; There are better ways.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With data like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data set1;
   input pid;
datalines;
012345
012356
014589
;
run;

data set2;
   input pid type $1.;
datalines;
012345 A
012345 B
012356 A
014589 B
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you can do&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data midstep;
	merge set1 (in = a) set2;
	by pid;
	if a and type = 'B' then myvar = 1;
		else myvar = 0;
run;

proc sql;
	select pid, max(myvar)
	from midstep
	group by pid;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to get&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;&lt;BR /&gt;
&lt;TABLE class="table" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt; &lt;COL class="data" /&gt; &lt;COL class="data" /&gt; &lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="header" scope="colgroup"&gt;pid&lt;/TH&gt;
&lt;TH class="header" scope="colgroup"&gt;&amp;nbsp;&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;12345&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="b data"&gt;12356&lt;/TD&gt;
&lt;TD class="b data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="b data"&gt;14589&lt;/TD&gt;
&lt;TD class="b data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 10 May 2022 16:57:36 GMT</pubDate>
    <dc:creator>HB</dc:creator>
    <dc:date>2022-05-10T16:57:36Z</dc:date>
    <item>
      <title>How to set a variable in a table based on multiple records in another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812430#M320559</link>
      <description>&lt;P&gt;This is what I am trying to do -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data_set_1:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PID&amp;nbsp;&lt;/P&gt;&lt;P&gt;012345&lt;/P&gt;&lt;P&gt;012356&lt;BR /&gt;014589&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data_set_2:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Type&lt;/P&gt;&lt;P&gt;012345&amp;nbsp; &amp;nbsp; A&lt;/P&gt;&lt;P&gt;012345&amp;nbsp; &amp;nbsp; B&lt;/P&gt;&lt;P&gt;012356&amp;nbsp; &amp;nbsp; A&lt;/P&gt;&lt;P&gt;014589&amp;nbsp; &amp;nbsp; B&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to create a table, using the PID as the unique value between the datasets, which returns the PID from Data_set_1, and a variable field that returns a 1 value when ANY record in Data_Set_2 for that PID as a Type value of B.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I want my final data set to look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Var&lt;/P&gt;&lt;P&gt;012345&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;012356&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;014589&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 15:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812430#M320559</guid>
      <dc:creator>J_Yetman</dc:creator>
      <dc:date>2022-05-10T15:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable in a table based on multiple records in another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812457#M320566</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way.&amp;nbsp; There are better ways.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With data like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data set1;
   input pid;
datalines;
012345
012356
014589
;
run;

data set2;
   input pid type $1.;
datalines;
012345 A
012345 B
012356 A
014589 B
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you can do&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data midstep;
	merge set1 (in = a) set2;
	by pid;
	if a and type = 'B' then myvar = 1;
		else myvar = 0;
run;

proc sql;
	select pid, max(myvar)
	from midstep
	group by pid;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to get&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;&lt;BR /&gt;
&lt;TABLE class="table" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt; &lt;COL class="data" /&gt; &lt;COL class="data" /&gt; &lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="header" scope="colgroup"&gt;pid&lt;/TH&gt;
&lt;TH class="header" scope="colgroup"&gt;&amp;nbsp;&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;12345&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="b data"&gt;12356&lt;/TD&gt;
&lt;TD class="b data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="b data"&gt;14589&lt;/TD&gt;
&lt;TD class="b data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 16:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812457#M320566</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2022-05-10T16:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable in a table based on multiple records in another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812477#M320577</link>
      <description>&lt;P&gt;Can't think of anything simpler than:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select 
    pid,
    "B" in (select type from set2 where pid = set1.pid) as var
from set1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 May 2022 19:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812477#M320577</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-05-10T19:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable in a table based on multiple records in another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812504#M320590</link>
      <description>&lt;P&gt;Aha.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;"B" in (select type from set2 where pid = set1.pid)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is returning a TRUE (1) if it sees a "B" in the list of returned set2 types for each set1 PID, otherwise it is returning a FALSE (0).&amp;nbsp; Would not have ever come up with that.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 21:59:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812504#M320590</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2022-05-10T21:59:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable in a table based on multiple records in another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812536#M320607</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data set1;
   input pid;
datalines;
012345
012356
014589
;
run;

data set2;
   input pid type $1.;
datalines;
012345 A
012345 B
012356 A
014589 B
;
run;

data want;
 if _n_=1 then do;
  declare hash h(dataset:'set2(where=(type="B"))');
  h.definekey('pid');
  h.definedone();
 end;
set set1;
var=ifn(h.check()=0,1,0);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 May 2022 03:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812536#M320607</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-05-11T03:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable in a table based on multiple records in another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812546#M320612</link>
      <description>&lt;P&gt;An another one:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   merge
      set1
      set2(where=(type = 'B'))
   ;
   by pid;
   
   Var = not missing(type);
   
   drop type;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 May 2022 05:35:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812546#M320612</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-05-11T05:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to set a variable in a table based on multiple records in another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812571#M320622</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;Your Ifn Function is redundant &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;</description>
      <pubDate>Wed, 11 May 2022 09:12:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-a-variable-in-a-table-based-on-multiple-records-in/m-p/812571#M320622</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-05-11T09:12:57Z</dc:date>
    </item>
  </channel>
</rss>

