<?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: Need help with a view, a total and an alter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777548#M247395</link>
    <description>&lt;P&gt;Start by making your code work; as it is, it causes a syntax error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sums are easily done in SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sum(uk,us,eu,amea) as total_sales&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then use PROC RANK to get the top three products. Or you can order the view by descending calculated total_sales, and use OUTOBS=3 on the PROC SQL statement.&lt;/P&gt;
&lt;P&gt;From which total volume do you want to calculate the percentages? From all products, or just the top three?&lt;/P&gt;</description>
    <pubDate>Sun, 31 Oct 2021 08:09:07 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-10-31T08:09:07Z</dc:date>
    <item>
      <title>Need help with a view, a total and an alter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777547#M247394</link>
      <description>&lt;P&gt;I need help with this:&lt;/P&gt;&lt;P&gt;Create a VIEW called ‘top_three’ that shows the stock levels for the top&lt;BR /&gt;three selling products (by volume, not value). The VIEW should contain the following&lt;BR /&gt;fields:&lt;BR /&gt;- name (from the product table)&lt;BR /&gt;- A field totalling the volume of sales called ‘number_of_sales’&lt;BR /&gt;- The four stock level fields: eu, us, uk and amea&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;(b) alter the script to add a field to the view for that shows the total sales as a&lt;BR /&gt;percentage of total stock. Call this field ‘sale_stock_percentage’&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;The tables were imported from excel so below are the tables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;product&lt;/P&gt;&lt;P&gt;product_id name unit_cost&lt;BR /&gt;222uuu laptop 1996.99&lt;BR /&gt;222vvv headphones 320.95&lt;BR /&gt;222www speakers 150.99&lt;BR /&gt;222xxx keyboard 85.00&lt;BR /&gt;222yyy mouse 55.00&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Stock&lt;/P&gt;&lt;P&gt;product_id uk eu us amea&lt;BR /&gt;222uuu 219 80 150 353&lt;BR /&gt;222vvv 532 243 299 256&lt;BR /&gt;222www 34 90 15 148&lt;BR /&gt;222xxx 277 309 265 321&lt;BR /&gt;222yyy 56 17 54 234&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Orders&lt;/P&gt;&lt;P&gt;product_id quantity total_gbp&lt;/P&gt;&lt;P&gt;222uuu 1 1996.99&lt;BR /&gt;222yyy 2 110.00&lt;BR /&gt;222vvv 7 2246.65&lt;BR /&gt;222www 4 603.96&lt;BR /&gt;222vvv 16 5135.20&lt;BR /&gt;222vvv 13 4172.35&lt;BR /&gt;222yyy 16 880.00&lt;BR /&gt;222xxx 10 850.00&lt;BR /&gt;222www 5 754.95&lt;BR /&gt;222www 1 150.99&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;The code I have written:&lt;/P&gt;&lt;PRE&gt;proc sql;
create view top_three as
select a.name, 
b.quantity,
c.uk, 
c.eu, 
c.us, 
c.amea
from work.product a
full join work.stock c
on a.product_id = c.product_id
full join work.orders b
on a.product_id = b.product_id
;&lt;/PRE&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 09:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777547#M247394</guid>
      <dc:creator>don_dadda</dc:creator>
      <dc:date>2021-10-31T09:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with a view, a total and an alter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777548#M247395</link>
      <description>&lt;P&gt;Start by making your code work; as it is, it causes a syntax error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sums are easily done in SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sum(uk,us,eu,amea) as total_sales&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then use PROC RANK to get the top three products. Or you can order the view by descending calculated total_sales, and use OUTOBS=3 on the PROC SQL statement.&lt;/P&gt;
&lt;P&gt;From which total volume do you want to calculate the percentages? From all products, or just the top three?&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 08:09:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777548#M247395</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-31T08:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with a view, a total and an alter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777551#M247396</link>
      <description>So i need the totals to be totals per row (product) so a column at the end totalling each row.&lt;BR /&gt;&lt;BR /&gt;The percentage will be a total of the quantity column from another table called orders and joined on the product_id. Would i just join all three tables to create the view first? I guess i need that table in there because it has the quantity of sales</description>
      <pubDate>Sun, 31 Oct 2021 08:17:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777551#M247396</guid>
      <dc:creator>don_dadda</dc:creator>
      <dc:date>2021-10-31T08:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with a view, a total and an alter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777554#M247397</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/404644"&gt;@don_dadda&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;So i need the totals to be totals per row (product) so a column at the end totalling each row.&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's what my formula does.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;BR /&gt;The percentage will be a total of the quantity column from another table called orders and joined on the product_id. Would i just join all three tables to create the view first? I guess i need that table in there because it has the quantity of sales&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In order to understand what you really want, please supply examples for the three tables involved by posting code (data steps with datalines) that create the example datasets. Also show the expected result from these.&lt;/P&gt;
&lt;P&gt;Use the "little running man" next to the one indicated to post the codes:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54552i914D97BE1B0F21E5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post data steps, no pictures, no Excel files.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 08:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777554#M247397</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-31T08:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with a view, a total and an alter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777555#M247398</link>
      <description>&lt;P&gt;&amp;nbsp;The tables have been imported from excel, so below are examples of what they look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;product&lt;/P&gt;&lt;P&gt;product_id name unit_cost&lt;/P&gt;&lt;P&gt;222uuu laptop 1996.99&lt;BR /&gt;222vvv headphones 320.95&lt;BR /&gt;222www speakers 150.99&lt;BR /&gt;222xxx keyboard 85.00&lt;BR /&gt;222yyy mouse 55.00&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;stock&lt;/P&gt;&lt;P&gt;product_id uk eu us amea&lt;/P&gt;&lt;P&gt;222uuu 219 80 150 353&lt;BR /&gt;222vvv 532 243 299 256&lt;BR /&gt;222www 34 90 15 148&lt;BR /&gt;222xxx 277 309 265 321&lt;BR /&gt;222yyy 56 17 54 234&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;orders&lt;/P&gt;&lt;P&gt;product_id quantity total_gbp&lt;/P&gt;&lt;P&gt;222uuu 1 1996.99&lt;BR /&gt;222yyy 2 110.00&lt;BR /&gt;222vvv 7 2246.65&lt;BR /&gt;222www 4 603.96&lt;BR /&gt;222uuu 5 9984.96&lt;BR /&gt;222vvv 16 5135.20&lt;BR /&gt;222vvv 13 4172.35&lt;BR /&gt;222yyy 16 880.00&lt;BR /&gt;222xxx 10 850.00&lt;BR /&gt;222www 5 754.95&lt;BR /&gt;222www 1 150.99&lt;BR /&gt;222www 1 150.99&lt;BR /&gt;222xxx 3 255.00&lt;BR /&gt;222uuu 2 3993.98&lt;BR /&gt;222vvv 17 5456.15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The actual question I have been asked is:&lt;/P&gt;&lt;P&gt;Create a VIEW called ‘top_three’ that shows the stock levels for the top&lt;BR /&gt;three selling products (by volume, not value). The VIEW should contain the following&lt;BR /&gt;fields:&lt;BR /&gt;- name (from the product table)&lt;BR /&gt;- A field totalling the volume of sales called ‘number_of_sales’&lt;BR /&gt;- The four stock level fields: eu, us, uk and amea&lt;/P&gt;&lt;P&gt;then&lt;BR /&gt;alter the script to add a field to the view for that shows the total sales as a&lt;BR /&gt;percentage of total stock. Call this field ‘sale_stock_percentage’&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code that I have at the moment:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
create view top_three as
select a.name, 
b.quatity,
c.uk, 
c.eu, 
c.us, 
c.amea
from work.product a
full join work.stock c
on a.product_id = c.product_id
full join work.order_product b
on a.product_id = b.product_id
;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 Oct 2021 08:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777555#M247398</guid>
      <dc:creator>don_dadda</dc:creator>
      <dc:date>2021-10-31T08:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with a view, a total and an alter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777557#M247400</link>
      <description>&lt;P&gt;The tables were imported from excel so below are the tables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;product&lt;/P&gt;&lt;P&gt;product_id name unit_cost&lt;BR /&gt;222uuu laptop 1996.99&lt;BR /&gt;222vvv headphones 320.95&lt;BR /&gt;222www speakers 150.99&lt;BR /&gt;222xxx keyboard 85.00&lt;BR /&gt;222yyy mouse 55.00&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Stock&lt;/P&gt;&lt;P&gt;product_id uk eu us amea&lt;BR /&gt;222uuu 219 80 150 353&lt;BR /&gt;222vvv 532 243 299 256&lt;BR /&gt;222www 34 90 15 148&lt;BR /&gt;222xxx 277 309 265 321&lt;BR /&gt;222yyy 56 17 54 234&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Orders&lt;/P&gt;&lt;P&gt;product_id quantity total_gbp&lt;/P&gt;&lt;P&gt;222uuu 1 1996.99&lt;BR /&gt;222yyy 2 110.00&lt;BR /&gt;222vvv 7 2246.65&lt;BR /&gt;222www 4 603.96&lt;BR /&gt;222vvv 16 5135.20&lt;BR /&gt;222vvv 13 4172.35&lt;BR /&gt;222yyy 16 880.00&lt;BR /&gt;222xxx 10 850.00&lt;BR /&gt;222www 5 754.95&lt;BR /&gt;222www 1 150.99&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;The code I have written:&lt;/P&gt;&lt;PRE&gt;proc sql;
create view top_three as
select a.name, 
b.quantity,
c.uk, 
c.eu, 
c.us, 
c.amea
from work.product a
full join work.stock c
on a.product_id = c.product_id
full join work.orders b
on a.product_id = b.product_id
;&lt;/PRE&gt;&lt;P&gt;And this is the actual question I have been asked:&lt;/P&gt;&lt;P&gt;Create a VIEW called ‘top_three’ that shows the stock levels for the top&lt;BR /&gt;three selling products (by volume, not value). The VIEW should contain the following&lt;BR /&gt;fields:&lt;BR /&gt;- name (from the product table)&lt;BR /&gt;- A field totalling the volume of sales called ‘number_of_sales’&lt;BR /&gt;- The four stock level fields: eu, us, uk and amea&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;(b) alter the script to add a field to the view for that shows the total sales as a&lt;BR /&gt;percentage of total stock. Call this field ‘sale_stock_percentage’&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 09:06:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777557#M247400</guid>
      <dc:creator>don_dadda</dc:creator>
      <dc:date>2021-10-31T09:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with a view, a total and an alter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777601#M247415</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;This should be close to what you want :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.product;
informat name $10.;
input product_id $ name $ unit_cost;
datalines;
222uuu laptop     1996.99
222vvv headphones 320.95
222www speakers   150.99
222xxx keyboard   85.00
222yyy mouse      55.00
;
run;

data work.Stock;
input product_id $ uk eu us amea;
datalines;
222uuu 219  80 150 353
222vvv 532 243 299 256
222www  34  90  15 148
222xxx 277 309 265 321
222yyy  56  17  54 234
;
run;

data work.Orders;
input product_id $ quantity total_gbp;
datalines;
222uuu 1  1996.99
222yyy 2   110.00
222vvv 7  2246.65
222www 4   603.96
222vvv 16 5135.20
222vvv 13 4172.35
222yyy 16  880.00
222xxx 10  850.00
222www 5   754.95
222www 1   150.99
;
run;

proc means data=work.Orders noprint nway;
   class product_id;
   var   quantity;
   output out=work.toplist(drop=_TYPE_ rename=(_freq_=NumberOrderLines)) sum= / autolabel autoname;
run;

proc sort data=work.toplist out=work.top3list;
 by descending quantity_sum;
run;

PROC SQL noprint;
 create view work.top_three as
 select   t1.product_id , t1.name  
        , t2.NumberOrderLines , t2.quantity_sum 
        , t3.uk , t3.eu , t3.us , t3.amea
		, (t2.quantity_sum / SUM(t3.uk,t3.eu,t3.us,t3.amea)) as sale_stock_percentage
		  format = percent7.2
 from   work.product         as t1
      , work.top3list(obs=3) as t2
      , work.Stock           as t3
 where t1.product_id = t2.product_id = t3.product_id
 order by t2.quantity_sum desc
; QUIT;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 19:22:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-a-view-a-total-and-an-alter/m-p/777601#M247415</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-10-31T19:22:35Z</dc:date>
    </item>
  </channel>
</rss>

