<?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: Find if a group contains a value to create new variable (SAS 9.4) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423780#M104256</link>
    <description>&lt;P&gt;Here is one way to do it using a datastep:&lt;/P&gt;
&lt;PRE&gt;data have;
  infile cards dlm='09'x;
  input (group value) ($);
  cards;
WB	abc
WB	asdf
WB	cwe
AP	sdf
AP	xxxxxx
AP	ter
TN	asdf
TN	ad
TN	sdr
TN	wer
OD	sser
OD	wry
OD	gyu
OD	xxxxxx
;

data want;
  do until (last.group);
    set have;
    by group notsorted;
    if value='xxxxxx' then drop=1;
  end;
  do until (last.group);
    set have;
    by group notsorted;
    if not drop then output;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Dec 2017 19:43:36 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-12-27T19:43:36Z</dc:date>
    <item>
      <title>Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423771#M104251</link>
      <description>&lt;P&gt;I want to find if a group has a certain value and delete the group if it does.&lt;/P&gt;&lt;P&gt;I have:&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;WB&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;WB&lt;/TD&gt;&lt;TD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B&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;WB&lt;/TD&gt;&lt;TD&gt;asdf&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;WB&lt;/TD&gt;&lt;TD&gt;cwe&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TN&lt;/TD&gt;&lt;TD&gt;asdf&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TN&lt;/TD&gt;&lt;TD&gt;ad&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TN&lt;/TD&gt;&lt;TD&gt;sdr&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TN&lt;/TD&gt;&lt;TD&gt;wer&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please advice.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 19:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423771#M104251</guid>
      <dc:creator>putteringpluie</dc:creator>
      <dc:date>2017-12-27T19:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423772#M104252</link>
      <description>What code have you tried?  Are you looking for a DATA step solution or also open to PROC SQL solution?</description>
      <pubDate>Wed, 27 Dec 2017 19:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423772#M104252</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-12-27T19:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423776#M104253</link>
      <description>I'm new to SAS as I'm coming from the world of stata. My logic in stata would be generate a tag of 1 if a group has the certain value. Use "egen" to create another variable which is the max value in a group. And delete if the group max value is 1. I don't know which function would generate the max value by group in SAS. Proc sql solution will also be helpful.&amp;nbsp;</description>
      <pubDate>Wed, 27 Dec 2017 19:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423776#M104253</guid>
      <dc:creator>putteringpluie</dc:creator>
      <dc:date>2017-12-27T19:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423777#M104254</link>
      <description>&lt;P&gt;data have;&lt;BR /&gt;input grp $ value $;&lt;BR /&gt;datalines;&lt;BR /&gt;WB abc&lt;BR /&gt;WB asdf&lt;BR /&gt;WB cwe&lt;BR /&gt;AP sdf&lt;BR /&gt;AP xxxxxx&lt;BR /&gt;AP ter&lt;BR /&gt;TN asdf&lt;BR /&gt;TN ad&lt;BR /&gt;TN sdr&lt;BR /&gt;TN wer&lt;BR /&gt;OD sser&lt;BR /&gt;OD wry&lt;BR /&gt;OD gyu&lt;BR /&gt;OD xxxxxx&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want(drop=v) as&lt;BR /&gt;select *, max(value = 'xxxxxx') as v&lt;BR /&gt;from have&lt;BR /&gt;group by grp&lt;BR /&gt;having v = 0;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 19:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423777#M104254</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-12-27T19:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423778#M104255</link>
      <description>&lt;P&gt;This is cleaner than previous:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select *&lt;BR /&gt;from have&lt;BR /&gt;group by grp&lt;BR /&gt;having max(value = 'xxxxxx')=0;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 19:40:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423778#M104255</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-12-27T19:40:12Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423780#M104256</link>
      <description>&lt;P&gt;Here is one way to do it using a datastep:&lt;/P&gt;
&lt;PRE&gt;data have;
  infile cards dlm='09'x;
  input (group value) ($);
  cards;
WB	abc
WB	asdf
WB	cwe
AP	sdf
AP	xxxxxx
AP	ter
TN	asdf
TN	ad
TN	sdr
TN	wer
OD	sser
OD	wry
OD	gyu
OD	xxxxxx
;

data want;
  do until (last.group);
    set have;
    by group notsorted;
    if value='xxxxxx' then drop=1;
  end;
  do until (last.group);
    set have;
    by group notsorted;
    if not drop then output;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 19:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423780#M104256</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-12-27T19:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423781#M104257</link>
      <description>&lt;P&gt;This can be done with DATA step, but the PROC SQL solution is probably more straight forward:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input group $2. value $10.;
  cards;
WB abc 
WB asdf 
WB cwe 
AP sdf 
AP xxxxxx 
AP ter 
TN asdf 
TN ad 
TN sdr 
TN wer 
OD sser 
OD wry 
OD gyu 
OD xxxxxx 
;

proc sql;
  create table want as 
  select *
  from have 
  group by group
  having max(value='xxxxxx')=0
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;On the having clause, SAS will evaluate value='xxxxxx' as either 1 (true) or 0 (false).&amp;nbsp; You can use the aggregate MAX() function to find those records where the maximum value within the group is 0 (i.e. no records have a value of 'xxxxxx').&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works in PROC SQL (but would not work in most SQL implementations) because in SAS you are allowed to include non-aggregated variables on the select clause even though there is a&amp;nbsp; group by clause.&amp;nbsp; You will see a note in the log that SAS "remerged" the records.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 19:43:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423781#M104257</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-12-27T19:43:57Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423784#M104259</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input group $2. value $10.;
  cards;
WB abc 
WB asdf 
WB cwe 
AP sdf 
AP xxxxxx 
AP ter 
TN asdf 
TN ad 
TN sdr 
TN wer 
OD sser 
OD wry 
OD gyu 
OD xxxxxx 
;

proc sql;
   create table want as
   select *
   from have
   where group not in
   (select distinct group from have where value="xxxxxx");
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Dec 2017 19:56:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423784#M104259</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-12-27T19:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423789#M104260</link>
      <description>&lt;P&gt;You solution is interesting. Just so I better understand, can you please explain, how this is working and why we need two passes at data instead of one.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 20:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423789#M104260</guid>
      <dc:creator>putteringpluie</dc:creator>
      <dc:date>2017-12-27T20:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: Find if a group contains a value to create new variable (SAS 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423793#M104261</link>
      <description>&lt;P&gt;Two passes are needed in both the datastep and with proc sql, first to identify whether any record in a by group meets the condition, then a second to actually select the records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution I suggested uses what is called a DOW loop. It takes over the normal record by record processing normally accomplished by SAS in a datastep.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first part:&lt;/P&gt;
&lt;PRE&gt;data want;
  do until (last.group);
    set have;
    by group notsorted;
    if value='xxxxxx' then drop=1;
  end;
&lt;/PRE&gt;
&lt;P&gt;goes through each by group, identifies if any of its records contain 'xxxxxx' for the variable value and, if at least one does, it sets the value of drop to equal 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second part:&lt;/P&gt;
&lt;PRE&gt;  do until (last.group);
    set have;
    by group notsorted;
    if not drop then output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;is run as soon as the last record in a by group is read and only works on that by group. The value of drop is available throughout the processing of the by group. When it get to the last record of a by group it then passes control back to the first part of the DOW loop to begin processing the next group. When it does so, the value of drop is initially set to missing for the next by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A lot more is going on and can be done using a DOW loop. You can read about it at:&amp;nbsp;&lt;SPAN&gt;support.sas.com/resources/papers/proceedings12/052-2012.pdf and&amp;nbsp;support.sas.com/resources/papers/proceedings12/156-2012.pdf&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I find&amp;nbsp;the DOW loop to be more versatile than trying to do the same thing in SQL and it runs more than twice as fast as its SQL alternative.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Art, CEO, AnalystFinder.com&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 20:38:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-if-a-group-contains-a-value-to-create-new-variable-SAS-9-4/m-p/423793#M104261</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-12-27T20:38:40Z</dc:date>
    </item>
  </channel>
</rss>

