<?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: SAS Code fix in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296934#M62300</link>
    <description>&lt;P&gt;Hello Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. For level P1, it should get the sum from all the below levels where ever status is Y (Irrespective of the levels), so in the given example there is a Y for P3 level, so it should pick the sum from all 'Y'.&lt;/P&gt;&lt;P&gt;2. For Level P2, it should pick the value from Level P3 and Status is 'Y'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Wed, 07 Sep 2016 11:21:43 GMT</pubDate>
    <dc:creator>sascodequestion</dc:creator>
    <dc:date>2016-09-07T11:21:43Z</dc:date>
    <item>
      <title>How to create a custom aggregation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296908#M62291</link>
      <description>&lt;P&gt;I have a requirement like this and i need your help in solving this.&lt;/P&gt;
&lt;P&gt;I have a dataset called test which is like below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here we have a column called P_Level and we have three values for this (P1, P2, and P3). SO my requirement is like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;For P1 level- I should get the sum of all Observations which are having status as ‘Y’. So If I assume the new variable as SUM it should be the sum of (3+4,3+3,3+6) =22&lt;/LI&gt;
&lt;LI&gt;For P2 level- My SUM should be equal to the sum of P3 level- where my PL1 and PL2 variable values match and Status =Y.&lt;/LI&gt;
&lt;LI&gt;Please find the screenshot of input and output for your reference.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;data test;&lt;BR /&gt;input P_Level$ PL1$ PL2$ PL3$ QTY1 QTY2 STATUS$;&lt;BR /&gt;cards;&lt;BR /&gt;P1 A . . 3 4 N&lt;BR /&gt;P2 A PL11 . 10 20 N&lt;BR /&gt;P3 A PL11 PL21 1 2 N&lt;BR /&gt;P3 A PL11 PL22 3 4 Y&lt;BR /&gt;P3 A PL11 PL23 1 3 N&lt;BR /&gt;P2 A PL12 . 30 40 N&lt;BR /&gt;P3 A PL12 PL21 1 3 N&lt;BR /&gt;P3 A PL12 PL22 3 3 Y&lt;BR /&gt;P3 A PL12 PL23 1 5 N&lt;BR /&gt;P3 A PL13 . 33 43 N&lt;BR /&gt;P3 A PL13 PL21 1 6 N&lt;BR /&gt;P3 A PL13 PL22 3 6 Y&lt;BR /&gt;P3 A PL13 PL23 1 6 N&lt;BR /&gt;;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12939iEEF9EDED5FE800FB/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Output.PNG" title="Output.PNG" /&gt;</description>
      <pubDate>Wed, 07 Sep 2016 10:56:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296908#M62291</guid>
      <dc:creator>sascodequestion</dc:creator>
      <dc:date>2016-09-07T10:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Code fix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296921#M62295</link>
      <description>&lt;P&gt;Below should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql feedback;
  create table p1_sum as
    select pl1, sum(sum(qty1,qty2)) as p_sum
    from test
    where status='Y'
    group by pl1
    ;
  create table p2_sum as
    select pl2, sum(sum(qty1,qty2)) as p_sum
    from test
    where status='Y'
    group by pl2
    ;
  create table want as
    select t.*, coalesce(p1.p_sum, p2.p_sum) as p_sum
    from
      test t
      left join p1_sum p1
        on missing(t.pl2) and t.pl1=p1.pl1
      left join p2_sum p2
        on missing(t.pl3) and t.pl2=p2.pl2
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or as a variation of above:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=test(where=(status='Y')) noprint;
  class pl1 pl2;
  ways 1 2;
  var qty1 qty2;
  output out=inter (where=(_type_ in (2,3))) sum=hugo1 hugo2;
run;quit;


proc sql feedback;
  create table want as
    select t.*, sum(p.hugo1,p.hugo2) as p_sum
    from
      test t
      left join inter p
        on t.pl1=p.pl1 and t.pl2=p.pl2 and missing(t.pl3)
    ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Sep 2016 09:52:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296921#M62295</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-09-07T09:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Code fix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296926#M62296</link>
      <description>&lt;P&gt;When I need to solve a compound or complicated situation, I prefer to split it into few parts.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let start with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data tmp1 tmp2 tmp3;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;input P_Level &amp;nbsp;$ &amp;nbsp; PL1 &amp;nbsp;$ PL2 $ PL3 $ &amp;nbsp; QTY1 &amp;nbsp; QTY2 &amp;nbsp; STATUS &amp;nbsp;$;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;select (P_level);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; when ("P1") output tmp1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; when ("P2") output tmp2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; when ("P3") output tmp3;&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;&lt;SPAN&gt;cards;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; ..... &amp;nbsp;/* your input is here */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Now you can deal with each P_level seperately:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data sum_P1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; set tmp1 end=eof;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; retain sum 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if status = 'Y' then sum = sum(of sum qty1 qty2); &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if eof then output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; keep P_level sum;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;RUN;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;For levels P2 and P3 I didn't understand what you need to do,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;neither for P1 with status = 'N'.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2016 09:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296926#M62296</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-09-07T09:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Code fix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296931#M62298</link>
      <description>&lt;P&gt;Thanks for the response shmuel.&lt;/P&gt;&lt;P&gt;Sorry for not describing my requirement clear.&lt;/P&gt;&lt;P&gt;1. For level P1, it should get the sum from all the below levels where ever status is Y (Irrespective of the levels), so in the given example there is a Y for P3 level, so it should pick the sum from all 'Y'.&lt;/P&gt;&lt;P&gt;2. For Level P2, it should pick the value from Level P3 and Status is 'Y'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see the ouput attached for your reference&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12941i2B67816398758281/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Output.PNG" title="Output.PNG" /&gt;</description>
      <pubDate>Wed, 07 Sep 2016 10:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296931#M62298</guid>
      <dc:creator>sascodequestion</dc:creator>
      <dc:date>2016-09-07T10:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Code fix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296933#M62299</link>
      <description>&lt;P&gt;How do you know which records to include?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please detail ail your logic in as much detail as possible.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2016 10:55:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296933#M62299</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-09-07T10:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Code fix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296934#M62300</link>
      <description>&lt;P&gt;Hello Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. For level P1, it should get the sum from all the below levels where ever status is Y (Irrespective of the levels), so in the given example there is a Y for P3 level, so it should pick the sum from all 'Y'.&lt;/P&gt;&lt;P&gt;2. For Level P2, it should pick the value from Level P3 and Status is 'Y'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2016 11:21:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296934#M62300</guid>
      <dc:creator>sascodequestion</dc:creator>
      <dc:date>2016-09-07T11:21:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Code fix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296958#M62305</link>
      <description>&lt;P&gt;What do mean by "below level"? &amp;nbsp;Are P2 and P3 below level P1 ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For &lt;STRONG&gt;level P1&lt;/STRONG&gt; would you sum &lt;STRONG&gt;all&lt;/STRONG&gt; QTY1 and QTY2 from &lt;STRONG&gt;all levels&lt;/STRONG&gt; where status = "Y" ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For &lt;STRONG&gt;level P2&lt;/STRONG&gt; is summing QTY1 and QTY2 from &lt;STRONG&gt;level P3 only&lt;/STRONG&gt; where status = 'Y" &amp;nbsp;&lt;STRONG&gt;or&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;sum from &lt;STRONG&gt;both P2 and P3 levels&lt;/STRONG&gt; ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2016 13:27:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296958#M62305</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-09-07T13:27:48Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Code fix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296960#M62306</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What do mean by "below level"? &amp;nbsp;Are P2 and P3 below level P1 &amp;nbsp;ANs: Yes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For &lt;STRONG&gt;level P1&lt;/STRONG&gt; would you sum &lt;STRONG&gt;all&lt;/STRONG&gt; QTY1 and QTY2 from &lt;STRONG&gt;all levels&lt;/STRONG&gt; where status = "Y" ? ANs : Yes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For &lt;STRONG&gt;level P2&lt;/STRONG&gt; is summing QTY1 and QTY2 from &lt;STRONG&gt;level P3 only&lt;/STRONG&gt; where status = 'Y" &amp;nbsp;&lt;STRONG&gt;or&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;sum from &lt;STRONG&gt;both P2 and P3 levels&lt;/STRONG&gt; ? ANs:&amp;nbsp;&lt;STRONG&gt;level P3 only&lt;/STRONG&gt;&lt;SPAN&gt; where status = 'Y" .&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2016 13:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/296960#M62306</guid>
      <dc:creator>sascodequestion</dc:creator>
      <dc:date>2016-09-07T13:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Code fix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/297005#M62321</link>
      <description>&lt;P&gt;Concerning your answer and output you expected and uploaded,&lt;/P&gt;&lt;P&gt;You may try the code attached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I assumed that every group of PL2 value is preceded by P_level = 'P2'&lt;/P&gt;&lt;P&gt;which seems to be not correct. You miss 'P2' preceding PL13.&lt;/P&gt;&lt;P&gt;I'm leaving this one to you to resolve using RETAIN previous PL2 and testing for change;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2016 16:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/297005#M62321</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-09-07T16:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom aggregation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/297157#M62373</link>
      <description>&lt;PRE&gt;
Assuming I understand what you mean:





data test;
input P_Level$ PL1$ PL2$ PL3$ QTY1 QTY2 STATUS$;
cards;
P1 A . . 3 4 N
P2 A PL11 . 10 20 N
P3 A PL11 PL21 1 2 N
P3 A PL11 PL22 3 4 Y
P3 A PL11 PL23 1 3 N
P2 A PL12 . 30 40 N
P3 A PL12 PL21 1 3 N
P3 A PL12 PL22 3 3 Y
P3 A PL12 PL23 1 5 N
P2 A PL13 . 33 43 N
P3 A PL13 PL21 1 6 N
P3 A PL13 PL22 3 6 Y
P3 A PL13 PL23 1 6 N
;
run;
data test;
 set test;
 if p_level in ('P1' 'P2') then group+1;
run;
proc sql;
create table want as
 select *,case 
 when(p_level='P1') then 
 (select sum(sum(qty1,qty2)) from test where status='Y')
 when(p_level='P2') then 
 (select sum(sum(qty1,qty2)) from test where group=a.group and status='Y')
 else . end as sum
  from test as a;
quit;

&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Sep 2016 11:50:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-custom-aggregation/m-p/297157#M62373</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-09-08T11:50:49Z</dc:date>
    </item>
  </channel>
</rss>

