<?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 Compare observations within groups in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232209#M54681</link>
    <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;I have a road block in conduct a project. The problem is how to compare observations within groups. My data set has a column named STORE_ID, which is the ID number of each&amp;nbsp;store in a city. Another column is customer_id, obveriously it is ID of each consumed customer. I want to know the list of&amp;nbsp;Customer_id that&amp;nbsp;consumed not only in store A, but also in&amp;nbsp;store B. I think correlated sub-query&amp;nbsp;might be&amp;nbsp;a possible solution, but&amp;nbsp;don't know how to&amp;nbsp;develop code.&amp;nbsp;Any suggestions and sample codes are highly appreciated.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Harry&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 29 Oct 2015 14:19:38 GMT</pubDate>
    <dc:creator>HarryLiu</dc:creator>
    <dc:date>2015-10-29T14:19:38Z</dc:date>
    <item>
      <title>Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232209#M54681</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;I have a road block in conduct a project. The problem is how to compare observations within groups. My data set has a column named STORE_ID, which is the ID number of each&amp;nbsp;store in a city. Another column is customer_id, obveriously it is ID of each consumed customer. I want to know the list of&amp;nbsp;Customer_id that&amp;nbsp;consumed not only in store A, but also in&amp;nbsp;store B. I think correlated sub-query&amp;nbsp;might be&amp;nbsp;a possible solution, but&amp;nbsp;don't know how to&amp;nbsp;develop code.&amp;nbsp;Any suggestions and sample codes are highly appreciated.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Harry&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Oct 2015 14:19:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232209#M54681</guid>
      <dc:creator>HarryLiu</dc:creator>
      <dc:date>2015-10-29T14:19:38Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232213#M54683</link>
      <description>I think that a group by with a having count(*) =2 clause would work, given that the store and customer the combined key.</description>
      <pubDate>Thu, 29 Oct 2015 14:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232213#M54683</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-10-29T14:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232216#M54684</link>
      <description>Do you only have two stores or multiple stores?</description>
      <pubDate>Thu, 29 Oct 2015 14:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232216#M54684</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-10-29T14:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232219#M54685</link>
      <description>&lt;P&gt;Post some test data (in the form of a datastep) and required output.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Oct 2015 14:51:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232219#M54685</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-10-29T14:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232221#M54686</link>
      <description>&lt;P&gt;If you are only concerned with those two stores, here's a way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by customer_id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data a_only b_only both;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; merge have (where=(store_id='A') in=shopped_at_a)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; have (where=(store_id='B') in=shopped_at_b);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by customer_id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if shopped_at_a=0 then output b_only;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if shopped_at_b=0 then output a_only;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else output both;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that this doesn't account for the possibility that a customer shopped more than one time at a store.&amp;nbsp; There are ways to handle that, but we'd need a better picture of what is in your data and what you are trying to achieve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Oct 2015 14:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232221#M54686</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-10-29T14:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232227#M54687</link>
      <description>Thanks so much for everybody's suggestion. I have hundreds of stores and need to compare any interested stores (usually less than 4 stores). Attached is a sample of data set. Astounding's code looks good, but will be time consuming, since I have hundred stores. &lt;BR /&gt;Here is my data set,&lt;BR /&gt;Store_id	Customer_ID&lt;BR /&gt;CAL	11542&lt;BR /&gt;CAL	11526&lt;BR /&gt;CHI	10069&lt;BR /&gt;CHI	11433&lt;BR /&gt;CHI	11542&lt;BR /&gt;GFC	11493&lt;BR /&gt;GFC	11526&lt;BR /&gt;GFC	10068&lt;BR /&gt;KAN	11482&lt;BR /&gt;KAN	11493&lt;BR /&gt;	&lt;BR /&gt;If I compare CAL with CHI, the result should be 11542. IF between CAL and GFC, the result should be 11526. If between GFC and KAN, the result should be 11493. If between CAL and KAN, result should be nothing.&lt;BR /&gt;Thanks,&lt;BR /&gt;Harry</description>
      <pubDate>Thu, 29 Oct 2015 15:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232227#M54687</guid>
      <dc:creator>HarryLiu</dc:creator>
      <dc:date>2015-10-29T15:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232231#M54689</link>
      <description>&lt;P&gt;Hi, how about INTERSECT in SQL (works for 2 stores , works for 'n' stores) ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;data x;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;input store_id :$1. customer_id @@;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;datalines;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;A 123 B 234 A 789 B 123 A 999 A 234 B 765 C 123&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;title 'CUSTOMERS COMMON TO STORES A &amp;amp; B';&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;select customer_id from x where store_id = 'A'&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;intersect&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;select customer_id from x where store_id = 'B';&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;title 'CUSTOMERS COMMON TO STORES A &amp;amp; B &amp;amp; C;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;select customer_id from x where store_id = 'A'&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;intersect&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;select customer_id from x where store_id = 'B'&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;intersect&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;select customer_id from x where store_id = 'C';&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;CUSTOMERS COMMON TO STORES A &amp;amp; B&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;customer_id&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 123&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 234&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CUSTOMERS COMMON TO STORES A &amp;amp; B &amp;amp; C&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;customer_id&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 123&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Oct 2015 15:25:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232231#M54689</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2015-10-29T15:25:14Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232248#M54692</link>
      <description>&lt;P&gt;Try this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input Store_id $ Customer_ID;&lt;BR /&gt;datalines;&lt;BR /&gt;CAL 11542&lt;BR /&gt;CAL 11526&lt;BR /&gt;CHI 10069&lt;BR /&gt;CHI 11433&lt;BR /&gt;CHI 11542&lt;BR /&gt;GFC 11493&lt;BR /&gt;GFC 11526&lt;BR /&gt;GFC 10068&lt;BR /&gt;KAN 11482&lt;BR /&gt;KAN 11493&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table store as&lt;BR /&gt;select * from have group by customer_id&lt;BR /&gt;having count(distinct store_id)&amp;gt;1;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=store out=want(drop=_Name_);&lt;BR /&gt;by customer_id;&lt;BR /&gt;var store_id;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc print data=want;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Oct 2015 15:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232248#M54692</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2015-10-29T15:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232251#M54693</link>
      <description>&lt;P&gt;A more general approach might be just what you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have (keep=customer_id store_id) out=full_list nodupkey;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by customer_id store_id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data full_list;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set full_list;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; shopped_there='Y';&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=full_list out=want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by customer_id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; id store_id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var shopped_there;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It assumes that the store names form valid variable names in SAS, and gives you a "Y" for every store where a customer shopped.&amp;nbsp; You can use that for any combination of subsetting going forward.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Oct 2015 16:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232251#M54693</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-10-29T16:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: Compare observations within groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232299#M54696</link>
      <description>Thanks everybody for various suggestions and helps. I found all code work very well for my project but this one is the most efficient. The only reason is that my data set is complex and there will be tons of comparison.  &lt;BR /&gt;I appreciate everybody's time.&lt;BR /&gt;Thanks,&lt;BR /&gt;Harry</description>
      <pubDate>Thu, 29 Oct 2015 18:36:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-observations-within-groups/m-p/232299#M54696</guid>
      <dc:creator>HarryLiu</dc:creator>
      <dc:date>2015-10-29T18:36:05Z</dc:date>
    </item>
  </channel>
</rss>

