<?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: Output a group if conditional statement is met in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/879106#M347316</link>
    <description>&lt;P&gt;See if this does the job - It's returning the same number of records as your output above, but in a slightly different order.&amp;nbsp; &amp;nbsp;Just using some proc sort and SQL....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;proc sort data=have nodupkey out=have_1_nvx;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;by person_id;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;where mx = 'NVX';&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;proc sql;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;select have.obs, have.person_id format=12., have.mx&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;from have_1_nvx,have&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;where have_1_nvx.person_id = have.person_id;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;quit;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="donricardo_0-1685741384740.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84709i74CF3BD394E70B7A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="donricardo_0-1685741384740.png" alt="donricardo_0-1685741384740.png" /&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>Fri, 02 Jun 2023 21:30:51 GMT</pubDate>
    <dc:creator>donricardo</dc:creator>
    <dc:date>2023-06-02T21:30:51Z</dc:date>
    <item>
      <title>Output a group if conditional statement is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/878199#M346958</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a dataset of 18 million records. There are repeating person ids. I need this dataset split into two separate datasets. One dataset should be for a person who has ever had mx = NVX. The other data set would be for everyone else.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Have:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Obs person_id MX 
1 10000103722 PFR 
2 10000103722 PFR 
3 10000181132 NVX 
4 10000181132 MOD 
5 10000189649 PFR 
6 10000189649 PFR 
7 10000189649 NVX 
8 10000189649 PFR 
9 10000371289 PFR 
10 10000371289 PFR 
11 10000751831 NVX
12 10000810473 MOD 
13 10000810473 MOD 
14 10000829168 JSN 
15 10000864651 PFR 
16 10000864651 PFR 
17 10000865007 PFR 
18 10000865007 NVX 
19 10000865500 PFR 
20 10000865500 PFR 
21 10000865883 MOD 
22 10000865883 MOD 
23 10000866093 PFR 
24 10000866093 NVX 
25 10000866093 NVX&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Want:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;STRONG&gt;&lt;CODE class=""&gt;Obs person_id MX 
3 10000181132 NVX 
4 10000181132 MOD 
5 10000189649 PFR 
6 10000189649 PFR 
7 10000189649 NVX 
11 10000751831 NVX
17 10000865007 PFR 
18 10000865007 NVX 
23 10000866093 PFR 
24 10000866093 NVX 
25 10000866093 NVX&lt;/CODE&gt;&lt;/STRONG&gt;&lt;/PRE&gt;&lt;P&gt;I used the below code to try and do this, but it is only outputting records when mx=NVX and not outputting the entire group when NVX is found.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Data admin_adult2 admin_nvx;
Set admin_adult;
By person_id;
	if mx IN ('NVX') then output admin_nvx;
	Else output admin_adult2;
Run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 11:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/878199#M346958</guid>
      <dc:creator>OlsabeckT29</dc:creator>
      <dc:date>2023-05-30T11:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: Output a group if conditional statement is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/878228#M346971</link>
      <description>&lt;P&gt;One way to get the result is to create a macro variable to reference all person_id that have met the criteria., then subset the data based on the macro variable values.&amp;nbsp;&lt;BR /&gt;Eg (assuming person_id is a numeric variable):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Obs person_id MX $;
cards; 
1 10000103722 PFR 
2 10000103722 PFR 
3 10000181132 NVX 
4 10000181132 MOD 
5 10000189649 PFR 
6 10000189649 PFR 
7 10000189649 NVX 
8 10000189649 PFR 
9 10000371289 PFR 
10 10000371289 PFR 
11 10000751831 NVX
12 10000810473 MOD 
13 10000810473 MOD 
14 10000829168 JSN 
15 10000864651 PFR 
16 10000864651 PFR 
17 10000865007 PFR 
18 10000865007 NVX 
19 10000865500 PFR 
20 10000865500 PFR 
21 10000865883 MOD 
22 10000865883 MOD 
23 10000866093 PFR 
24 10000866093 NVX 
25 10000866093 NVX
; 
proc print; run; 

proc sql noprint;
	select distinct person_id format=16.
		into: ids separated by ','
			from have
		where upcase(strip(mx)) eq 'NVX';
	%put Selected Person_id= &amp;amp;ids;
quit; 
data want1 want2;
	set have;
	if person_id in (&amp;amp;ids) then output want1;
	else output want2;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 May 2023 13:31:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/878228#M346971</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-05-30T13:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: Output a group if conditional statement is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/878401#M347045</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Obs person_id MX $;
cards; 
1 10000103722 PFR 
2 10000103722 PFR 
3 10000181132 NVX 
4 10000181132 MOD 
5 10000189649 PFR 
6 10000189649 PFR 
7 10000189649 NVX 
8 10000189649 PFR 
9 10000371289 PFR 
10 10000371289 PFR 
11 10000751831 NVX
12 10000810473 MOD 
13 10000810473 MOD 
14 10000829168 JSN 
15 10000864651 PFR 
16 10000864651 PFR 
17 10000865007 PFR 
18 10000865007 NVX 
19 10000865500 PFR 
20 10000865500 PFR 
21 10000865883 MOD 
22 10000865883 MOD 
23 10000866093 PFR 
24 10000866093 NVX 
25 10000866093 NVX
; 


proc sql;
create table NVX          as select distinct person_id from have where MX='NVX';
create table admin_nvx    as select * from have where person_id in (select  person_id from NVX);
create table admin_adult2 as select * from have where person_id not in (select  person_id from NVX);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 May 2023 11:35:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/878401#M347045</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-05-31T11:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: Output a group if conditional statement is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/878433#M347058</link>
      <description>&lt;P&gt;Assuming your input data is sorted by person_id, a fast way to do it is this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data admin_nvx admin_adult2;
  merge have(where=(mx='NVX') in=NVX) have;
  by person_id;
  if nvx then output admin_nvx;
  else output admin_adult2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 May 2023 14:09:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/878433#M347058</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-05-31T14:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: Output a group if conditional statement is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/879106#M347316</link>
      <description>&lt;P&gt;See if this does the job - It's returning the same number of records as your output above, but in a slightly different order.&amp;nbsp; &amp;nbsp;Just using some proc sort and SQL....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;proc sort data=have nodupkey out=have_1_nvx;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;by person_id;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;where mx = 'NVX';&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;proc sql;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;select have.obs, have.person_id format=12., have.mx&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;from have_1_nvx,have&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;where have_1_nvx.person_id = have.person_id;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;quit;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="donricardo_0-1685741384740.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84709i74CF3BD394E70B7A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="donricardo_0-1685741384740.png" alt="donricardo_0-1685741384740.png" /&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>Fri, 02 Jun 2023 21:30:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/879106#M347316</guid>
      <dc:creator>donricardo</dc:creator>
      <dc:date>2023-06-02T21:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: Output a group if conditional statement is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/879124#M347320</link>
      <description>&lt;P&gt;If you data are sorted by person_id, then a self merge will help you deterimine where person_id's have an 'NVX' record:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Obs person_id MX $;
cards; 
1 10000103722 PFR 
2 10000103722 PFR 
3 10000181132 NVX 
4 10000181132 MOD 
5 10000189649 PFR 
6 10000189649 PFR 
7 10000189649 NVX 
8 10000189649 PFR 
9 10000371289 PFR 
10 10000371289 PFR 
11 10000751831 NVX
12 10000810473 MOD 
13 10000810473 MOD 
14 10000829168 JSN 
15 10000864651 PFR 
16 10000864651 PFR 
17 10000865007 PFR 
18 10000865007 NVX 
19 10000865500 PFR 
20 10000865500 PFR 
21 10000865883 MOD 
22 10000865883 MOD 
23 10000866093 PFR 
24 10000866093 NVX 
25 10000866093 NVX
; 
data want1 want2;
  merge have (where=(mx='NVX') in=found_nvx)
        have ;
  by person_id;
  if found_nvx then output want1;
  else output want2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The IN parameter FOUND_NVX will be 1 for an entire person_id even if only 1 such record is found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And it's crucial to this self-merge that the dataset name with the WHERE=(mx='NVX') parameter appears first.&amp;nbsp; This generates the desired creation of the FOUND_NVX dummy, while allowing the sequence of data records from the second reference to HAVE overwrite records from the first, to be passed on to the output datasets.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Jun 2023 03:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/879124#M347320</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-06-03T03:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: Output a group if conditional statement is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/879138#M347321</link>
      <description>&lt;P&gt;Use a double DO loop (your dataset seems to already be sorted by person_id):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.person_id);
  set have;
  by person_id;
  if mx = "NVX" then flag = 1;
end;
do until (last.person_id);
  set have;
  by person_id;
  if flag then output;
end;
drop flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 03 Jun 2023 05:08:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-a-group-if-conditional-statement-is-met/m-p/879138#M347321</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-03T05:08:23Z</dc:date>
    </item>
  </channel>
</rss>

