<?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 keep only obs with all values of a category in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247750#M6563</link>
    <description>&lt;P&gt;The SQL approach is neat, but requires that you really have clean data. A single non expected category value can affect the total result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step approach includes hard coding values, which in most real world&amp;nbsp;scenarios is not preferable/possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use a separate&amp;nbsp;look-up table (with category master data)&amp;nbsp;you could be sure to count only valid categories (one way is to us in a sub-query&amp;nbsp;to a where in-clause).&lt;/P&gt;</description>
    <pubDate>Wed, 03 Feb 2016 14:18:46 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2016-02-03T14:18:46Z</dc:date>
    <item>
      <title>How to keep only obs with all values of a category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247640#M6555</link>
      <description>&lt;P&gt;I'd like to keep only observations that include all values of a category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp;Season&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;Winter&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;Winter&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;Spring&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;Summer&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;Summer&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;Fall&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;Winter&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; Winter&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;Spring&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;Winter&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;Fall&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; Fall&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I'd like to keep only ID 1 b/c it includes all categories for the season var, and I'd like to delete IDs 2 &amp;amp; 3 because they do not include all 4 categories for the season var. &amp;nbsp;Also, some IDs have have repeat observation for the same season category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does anyone have a suggestion for this? &amp;nbsp;I'm using SAS Studio. Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2016 00:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247640#M6555</guid>
      <dc:creator>jlajla</dc:creator>
      <dc:date>2016-02-03T00:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only obs with all values of a category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247641#M6556</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID    Season $;
cards;
1      Winter
1      Winter
1      Spring
1      Summer
1      Summer
1      Fall
2      Winter
2       Winter
2      Spring
3      Winter
3      Fall
3       Fall
;
run;
proc sql;
 create table want as 
  select *
   from have
    group by id
     having count(distinct Season)=
      (select count(distinct Season) from have);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Feb 2016 01:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247641#M6556</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-03T01:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only obs with all values of a category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247654#M6557</link>
      <description>&lt;P&gt;great, thank you very much.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2016 02:57:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247654#M6557</guid>
      <dc:creator>jlajla</dc:creator>
      <dc:date>2016-02-03T02:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only obs with all values of a category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247719#M6560</link>
      <description>&lt;P&gt;data step method (more stuff to write, might be faster with big data):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data int (keep=id);
set have;
by id;
retain Spring Summer Fall Winter;
if first.id then do;
  Spring = 0; Summer = 0; Fall = 0; Winter = 0;
end;
select (Season);
  when ('Spring') Spring = 1;
  when ('Summer') Summer = 1;
  when ('Fall') Fall = 1;
  when ('Winter') Winter = 1;
end;
if last.id and sum(Spring,Summer,Fall,Winter) = 4 then output;
run;

data want;
merge
  int (in=a)
  have
;
by id;
if a;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Feb 2016 12:09:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247719#M6560</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-02-03T12:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only obs with all values of a category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247750#M6563</link>
      <description>&lt;P&gt;The SQL approach is neat, but requires that you really have clean data. A single non expected category value can affect the total result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step approach includes hard coding values, which in most real world&amp;nbsp;scenarios is not preferable/possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use a separate&amp;nbsp;look-up table (with category master data)&amp;nbsp;you could be sure to count only valid categories (one way is to us in a sub-query&amp;nbsp;to a where in-clause).&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2016 14:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247750#M6563</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-02-03T14:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only obs with all values of a category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247757#M6564</link>
      <description>&lt;P&gt;Well, the hard-coding of values can be automated in a data step with call execute, so if that problem arises, it can be solved.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2016 14:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247757#M6564</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-02-03T14:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only obs with all values of a category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247878#M6565</link>
      <description>&lt;P&gt;Thank you everyone for the suggestions. I forgot to include in my original question that i needed to keep one obs per year as well. So I needed one observation per year per month and needed to keep only the IDs that had values for months 6-9. &amp;nbsp;This is the solution I found, after spending too much time on it!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;data have;
input site year month;
cards;
1   97   6
1   97  6
1   97  7
1   98   8
1   99  8
1    97  9
2    97  7
2    97  7
2    97  8
3   98 8
3   99 9
3   99 9
.....;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*keeping only 1 obs per month/yr;&lt;BR /&gt;proc sort data1=test nodupkey;&lt;BR /&gt; by site year month; &lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;*transpose by site and year, narrow to wide, w/ id month and var year;&lt;BR /&gt;proc transpose data=test1 out=test2 prefix=monthyr; &lt;BR /&gt; by site year;&lt;BR /&gt; id month;&lt;BR /&gt; var year;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*make all_month var for only obs w/ months 6-9;&lt;BR /&gt;data test3;&lt;BR /&gt; set test2;&lt;BR /&gt; if monthyr6 ne '.' and monthyr7 ne '.' and monthyr8 ne '.' and monthyr9 ne '.' then all_month=1; else all_month=0;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*merge into narrow dataset;&lt;BR /&gt;proc sort data=test; by site; run;&lt;BR /&gt;proc sort data=test3; by site year; run;&lt;BR /&gt;data test4;&lt;BR /&gt; MERGE test test3; &lt;BR /&gt; BY site year; &lt;BR /&gt; drop _name_;&lt;BR /&gt; drop monthyr6-monthyr10;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*delete obs w/o months 6-9;&lt;BR /&gt;proc sort data=test4; by site year; run;&lt;BR /&gt;data test5;&lt;BR /&gt; set test4;&lt;BR /&gt; if all_month=0 then delete;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2016 23:23:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-keep-only-obs-with-all-values-of-a-category/m-p/247878#M6565</guid>
      <dc:creator>jlajla</dc:creator>
      <dc:date>2016-02-03T23:23:24Z</dc:date>
    </item>
  </channel>
</rss>

