<?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: please help simplify some codes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254288#M48472</link>
    <description>&lt;P&gt;Post sample data.&lt;/P&gt;
&lt;P&gt;Test it manually with a small subset of data. You know what the answer should be, so you'll know which code is correct.&lt;/P&gt;</description>
    <pubDate>Thu, 03 Mar 2016 18:59:14 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-03-03T18:59:14Z</dc:date>
    <item>
      <title>please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254274#M48466</link>
      <description>&lt;P&gt;my data has ID and stores where they put orders. I want to calculate how many stores one ID has put orders with, and have several steps code, and wonder if someone can help imporve it by making it shorter.&lt;/P&gt;
&lt;PRE&gt;/* first, only select ID and Store columns to make data size smaller */
proc sql;
create table table1 as
select  ID, store
from mydata;
quit;

/* count how many stores one ID has put orders with */
/* this step, in the output data, the same ID listed n times if n stores */
proc sql;
create table table2 as
select ID, store, count(*) as how_many_stores_per_ID
from table1
group by ID;
quit;

/* to remove duplicate rows for the same ID */
proc sql;
create table table3 as
select distinct ID, how_many_stores_per_ID
from table2;
quit;


/* to verify if the sum of all counts equal to the total rows of mydata */
proc means data=table3 n sum ;
var how_many_stores_per_ID;
output  n=count sum=total1;
run;&lt;/PRE&gt;
&lt;P&gt;I tested, they all worked, and the sum(how_many_stores_per_ID) equals total rows of mydata, which means it is right.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 18:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254274#M48466</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-03-03T18:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254278#M48468</link>
      <description>&lt;P&gt;I was thinking to do like below, which gave different results. which one is correct, the above moethod or the one below? Many thanks. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;&lt;BR /&gt;create table table4 as&lt;BR /&gt;select ID, store, count(*) as how_many_stores_per_ID&lt;BR /&gt;from table1&lt;BR /&gt;group by ID, store;&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 18:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254278#M48468</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-03-03T18:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254288#M48472</link>
      <description>&lt;P&gt;Post sample data.&lt;/P&gt;
&lt;P&gt;Test it manually with a small subset of data. You know what the answer should be, so you'll know which code is correct.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 18:59:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254288#M48472</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-03T18:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254289#M48473</link>
      <description>&lt;P&gt;You skipped the step where you removed duplicate rows for ID by grouping by ID and Store.&amp;nbsp; Also, you can do this pulling from your mydata dataset, eliminating the first step.&amp;nbsp; Try the code below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table table4 as
	select ID, count(store) as how_many_stores_per_ID
from mydata
group by ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 19:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254289#M48473</guid>
      <dc:creator>dcruik</dc:creator>
      <dc:date>2016-03-03T19:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254292#M48475</link>
      <description />
      <pubDate>Fri, 04 Mar 2016 15:22:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254292#M48475</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-03-04T15:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254293#M48476</link>
      <description>I am confused with: should I use group by ID or&lt;BR /&gt;group by ID, store?</description>
      <pubDate>Thu, 03 Mar 2016 19:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254293#M48476</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-03-03T19:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254295#M48477</link>
      <description>&lt;P&gt;If you use group by ID, store then you will get duplicate ID's if they are related to more than one store.&amp;nbsp; In your original code, you grouped by ID, store; however, you then proceeded to eliminate duplicate IDs with your select distinct statement.&amp;nbsp; You can group just by ID, and that will give you a count of all the stores for each unique ID.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 19:12:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254295#M48477</guid>
      <dc:creator>dcruik</dc:creator>
      <dc:date>2016-03-03T19:12:38Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254297#M48478</link>
      <description>&lt;P&gt;One step should be enough:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select id, count(distinct store)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Mar 2016 19:18:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254297#M48478</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-03-03T19:18:51Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254299#M48479</link>
      <description>&lt;P&gt;and use group by id?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 19:24:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254299#M48479</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-03-03T19:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254300#M48480</link>
      <description>&lt;P&gt;Yep&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 19:26:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254300#M48480</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-03-03T19:26:17Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254458#M48532</link>
      <description>&lt;P&gt;Did the count(distinct...) work for you?&lt;/P&gt;
&lt;P&gt;If yes, mark as solved.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2016 14:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254458#M48532</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-03-04T14:32:04Z</dc:date>
    </item>
    <item>
      <title>Re: please help simplify some codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254479#M48541</link>
      <description>yes, it is the simplest code. &lt;BR /&gt;Thanks</description>
      <pubDate>Fri, 04 Mar 2016 15:22:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/please-help-simplify-some-codes/m-p/254479#M48541</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-03-04T15:22:49Z</dc:date>
    </item>
  </channel>
</rss>

