<?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 sum obs according to many tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443355#M110909</link>
    <description>&lt;P&gt;Still don't know what you want or what you are doing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just guessing a this point that instead of&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="language-sas lia-code-sample" data-lia-code-lang="sas" data-lia-code-macro="true"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; temp&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;database&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;maps&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Storelist
&lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; Storelist&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;ID&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;Database&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;ID&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;you may want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;from temp.database maps.Storelist inner join Storelist.ID=Database.ID
where database.store in (002,005,008)
&lt;/CODE&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;In the IN clause you may need 2, '2', '002', or something.&amp;nbsp; Don't know how your data is stored.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR data-mce-bogus="1" /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR data-mce-bogus="1" /&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 07 Mar 2018 15:34:09 GMT</pubDate>
    <dc:creator>HB</dc:creator>
    <dc:date>2018-03-07T15:34:09Z</dc:date>
    <item>
      <title>How to sum obs according to many tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/442673#M110709</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a database, it has content like this&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp.database ;
input
STORE YEAR SALE ID;
datalines;
001 2001 12 1212
001 2002 12&amp;nbsp;1212
001 2003 12&amp;nbsp;1212
002 2001 12&amp;nbsp;1212
002 2002 12&amp;nbsp;1212
002 2003 12&amp;nbsp;1212
003 2001 12&amp;nbsp;1216
003 2002 12&amp;nbsp;1216
003 2003 12&amp;nbsp;1216
004 2001 12&amp;nbsp;1216
004 2002 12&amp;nbsp;1216
004 2003 12&amp;nbsp;1216
005 2001 12&amp;nbsp;1216
005 2002 12&amp;nbsp;1216
005 2003 12&amp;nbsp;1216
006 2001 12&amp;nbsp;1212
006 2002 12&amp;nbsp;1212
006 2003 12&amp;nbsp;1212
007 2001 12&amp;nbsp;1212
007 2002 12&amp;nbsp;1212
007 2003 12&amp;nbsp;1212
008 2001 12&amp;nbsp;1219
008 2002 12&amp;nbsp;1219
008 2003 12&amp;nbsp;1219
009 2001 12&amp;nbsp;1219
009 2002 12&amp;nbsp;1219
009 2003 12&amp;nbsp;1219
010 2001 12&amp;nbsp;1219
010 2002 12&amp;nbsp;1219
010 2003 12&amp;nbsp;1219;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Firrst, I am not looking for all the STORE in the database, I only care about the STORE I am interested in, the interested STORE list is like this&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data maps.Storelist ;
input
STORE;
datalines;
002
005
008
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Firrst, I want to SUM all the &lt;STRONG&gt;SALE&lt;/STRONG&gt; (let's call it &lt;STRONG&gt;SUMSALE&lt;/STRONG&gt;) under the same&lt;STRONG&gt; ID&lt;/STRONG&gt; and same &lt;STRONG&gt;YEAR&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;Then, I want the &lt;STRONG&gt;SALE&lt;/STRONG&gt; of each &lt;STRONG&gt;STORE&lt;/STRONG&gt; of each &lt;STRONG&gt;YEAR&lt;/STRONG&gt; in the Storelist. (This is directly given in the Database)&lt;/P&gt;&lt;P&gt;Then &lt;STRONG&gt;SALE/SUMSALE as SHARE.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code is like this,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select
Database.STORE,
Database.YEAR,
Database.SALE,
Database.ID,
sum(Database.SALE) as SUMSALE,
Database.SALE / SUMSALE as SHARE  /* This is the part I am not clear. Can I directly use the new variable?*/
from temp.database,maps.Storelist&lt;BR /&gt;where Storelist.ID=Database.ID &lt;BR /&gt;group by Database.SALE, Database.ID &lt;BR /&gt;order by Database.STORE,Database.YEAR; &lt;BR /&gt;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But SAS says the SHARE variable is not found in the contributing tables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can I directly use the variable I am creating now in the next step calculation?&lt;/P&gt;&lt;P&gt;Would this be solved by this?&lt;CODE class=" language-sas"&gt; &lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Database.SALE /sum(Database.SALE)as SHARE&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best wish.&lt;/P&gt;&lt;P&gt;&lt;CODE&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 00:22:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/442673#M110709</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-07T00:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum obs according to many tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443011#M110802</link>
      <description>&lt;P&gt;Not exactly sure what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But with your data,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table sum_of_st_sales_by_st_n_year as 
select a.year, a.store, sum(a.sale) as annual_st_sales, b.totalsales, sum(a.sale)/b.totalsales as sales_percent  
from mydatabase a
inner join (select year, sum(sale) as totalsales from mydatabase group by year) b on a.year = b.year
group by a.year, store
order by a.year, store;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;creates a table that has total store sales and a percentage of the total sales for a store in a year(although the data is rather boring and it is hard to tell).&amp;nbsp; You can subset this for the stores and years in which you are interested.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 19:26:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443011#M110802</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-03-06T19:26:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum obs according to many tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443020#M110805</link>
      <description>&lt;P&gt;Your requirement is not clear enough. What do you mean by&amp;nbsp;&lt;STRONG&gt;002SALEALL&lt;/STRONG&gt;? Are you missing something. Please provide your expected result.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 20:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443020#M110805</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-06T20:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum obs according to many tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443107#M110833</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;I have figured out what I need using SQL without JOIN. I have changed my question so it is easier to read.&lt;/P&gt;&lt;P&gt;So my question is&lt;/P&gt;&lt;P&gt;Can I directly use the new variable I am creating in SQL to calculate another variable?&lt;/P&gt;&lt;P&gt;Like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table want as select&lt;/P&gt;&lt;P&gt;A,B,C&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;SUM(A) as ATOT&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ATOT / B as SHARE&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;group by C&lt;/P&gt;&lt;P&gt;When I do this, SAS says the ATOT is not found in the contributing tables.&lt;/P&gt;&lt;P&gt;Can I solve this issue by change to this?&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;SUM(A) as ATOT&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;SUM(A) / B as SHARE&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 00:27:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443107#M110833</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-07T00:27:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum obs according to many tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443156#M110846</link>
      <description>&lt;P&gt;Yes, you can do that. Also if you want it for filter then you can use HAVING clause instead of WHERE&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 02:25:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443156#M110846</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-07T02:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum obs according to many tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443169#M110851</link>
      <description>&lt;P&gt;There is a prroblem with this code.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select
Database.STORE,
Database.YEAR,
Database.SALE,
Database.ID,
sum(Database.SALE) as SUMSALE,
Database.SALE / sum(Database.SALE) as SHARE  
from temp.database,maps.Storelist
where Storelist.ID=Database.ID 
group by Database.SALE, Database.ID 
order by Database.STORE,Database.YEAR; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The SUMSALE only sum the SALE for STORE 002 005 008? Do they?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 04:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443169#M110851</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-07T04:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum obs according to many tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443355#M110909</link>
      <description>&lt;P&gt;Still don't know what you want or what you are doing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just guessing a this point that instead of&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="language-sas lia-code-sample" data-lia-code-lang="sas" data-lia-code-macro="true"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; temp&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;database&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;maps&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Storelist
&lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; Storelist&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;ID&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;Database&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;ID&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;you may want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;from temp.database maps.Storelist inner join Storelist.ID=Database.ID
where database.store in (002,005,008)
&lt;/CODE&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;In the IN clause you may need 2, '2', '002', or something.&amp;nbsp; Don't know how your data is stored.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR data-mce-bogus="1" /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR data-mce-bogus="1" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 15:34:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443355#M110909</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-03-07T15:34:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum obs according to many tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443359#M110910</link>
      <description>&lt;P&gt;The SUM() function in PROC SQL is applied to&amp;nbsp; GROUPED variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 15:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-obs-according-to-many-tables/m-p/443359#M110910</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-07T15:36:27Z</dc:date>
    </item>
  </channel>
</rss>

