<?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: Datastep / SQL in one step(more efficient code) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415835#M102051</link>
    <description>&lt;P&gt;In the context of your query the other variables like subaccno don't make much sense, if you take a distinct acc_no, and sum amount then that is the data row, the other variables could be any of the records as they don't get summed.&amp;nbsp; If you just want the last row in the group and a sum, then:&lt;/P&gt;
&lt;PRE&gt;data want (drop=amt);
  set have;
  retain count_amt;
  by acc_no;
  if first.acc_no then count_amt=amt;&lt;BR /&gt;  else count_amt=sum(count_amt,amt);&lt;BR /&gt;  if last.acc_no;&lt;BR /&gt;run;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Nov 2017 14:11:46 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-11-23T14:11:46Z</dc:date>
    <item>
      <title>Datastep / SQL in one step(more efficient code)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415828#M102047</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset and SQL below, this is the best i can do, I know it can be done in one step of Datastep or SQL. Need advice how to do it in one step(Datastep or SQL) instead of mine by 2 step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm='|';
input ACC_NO SUB_ACCNO PRODUCT$ ACC_PLUS_PRODUCT :$20. CATEGORY $ AMT;
cards;
11111 | 12345 | GOLD | 11111GOLD | B | 50
11111 | 24895 | BLACK | 11111BLACK | B | 25
11111 | 24445 | BLACK | 11111BLACK | B | 75
11111 | 24445 | SILVER | 11111SILVER | B | 140
11111 | 23456 | SILVER | 11111SILVER | S | 70
11111 | 86475 | GOLD | 11111GOLD | S | 130
22222 | 34567 | SILVER | 22222SILVER | B | 100
22222 | 12458 | GOLD | 22222GOLD | B | 50
22222 | 12557 | GOLD | 22222GOLD | S | 100
33333 | 23785 | GOLD | 33333GOLD | B | 75
33333 | 12968 | SILVER | 33333SILVER | B | 110
33333 | 12968 | SILVER | 33333SILVER | S | 110
33333 | 54368 | SILVER | 33333SILVER | S | 50
33333 | 94256 | SILVER | 33333SILVER | S | 175
;

proc sql;
	create table flow_list_remove_duplicate(drop=AMT) as
	select *, 
	SUM(AMT) as count_AMT
	from have
	group by ACC_NO ;
quit;

proc sort data=work.flow_list_remove_duplicate;
	by ACC_NO;
run;

DATA FINAL;
	SET WORK.flow_list_remove_duplicate;
	by ACC_NO;
	if first.ACC_NO then output;
run;
	&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank in advance&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2017 13:52:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415828#M102047</guid>
      <dc:creator>sagulolo</dc:creator>
      <dc:date>2017-11-23T13:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: Datastep / SQL in one step(more efficient code)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415830#M102048</link>
      <description>&lt;P&gt;Expected result as below:-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Obs&lt;/TD&gt;&lt;TD&gt;ACC_NO&lt;/TD&gt;&lt;TD&gt;SUB_ACCNO&lt;/TD&gt;&lt;TD&gt;PRODUCT&lt;/TD&gt;&lt;TD&gt;ACC_PLUS_PRODUCT&lt;/TD&gt;&lt;TD&gt;CATEGORY&lt;/TD&gt;&lt;TD&gt;count_AMT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;11111&lt;/TD&gt;&lt;TD&gt;86475&lt;/TD&gt;&lt;TD&gt;GOLD&lt;/TD&gt;&lt;TD&gt;11111GOLD&lt;/TD&gt;&lt;TD&gt;S&lt;/TD&gt;&lt;TD&gt;490&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;22222&lt;/TD&gt;&lt;TD&gt;34567&lt;/TD&gt;&lt;TD&gt;SILVER&lt;/TD&gt;&lt;TD&gt;22222SILVER&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;250&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;33333&lt;/TD&gt;&lt;TD&gt;94256&lt;/TD&gt;&lt;TD&gt;SILVER&lt;/TD&gt;&lt;TD&gt;33333SILVER&lt;/TD&gt;&lt;TD&gt;S&lt;/TD&gt;&lt;TD&gt;520&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 23 Nov 2017 13:57:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415830#M102048</guid>
      <dc:creator>sagulolo</dc:creator>
      <dc:date>2017-11-23T13:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Datastep / SQL in one step(more efficient code)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415835#M102051</link>
      <description>&lt;P&gt;In the context of your query the other variables like subaccno don't make much sense, if you take a distinct acc_no, and sum amount then that is the data row, the other variables could be any of the records as they don't get summed.&amp;nbsp; If you just want the last row in the group and a sum, then:&lt;/P&gt;
&lt;PRE&gt;data want (drop=amt);
  set have;
  retain count_amt;
  by acc_no;
  if first.acc_no then count_amt=amt;&lt;BR /&gt;  else count_amt=sum(count_amt,amt);&lt;BR /&gt;  if last.acc_no;&lt;BR /&gt;run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Nov 2017 14:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415835#M102051</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-23T14:11:46Z</dc:date>
    </item>
    <item>
      <title>Re: Datastep / SQL in one step(more efficient code)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415848#M102060</link>
      <description>&lt;P&gt;Hi RW9,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank for the code. Would appreciated if you could give guidance on the Proc SQL as well(if you're convenience).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2017 15:25:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415848#M102060</guid>
      <dc:creator>sagulolo</dc:creator>
      <dc:date>2017-11-23T15:25:27Z</dc:date>
    </item>
    <item>
      <title>Re: Datastep / SQL in one step(more efficient code)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415851#M102062</link>
      <description>&lt;P&gt;In what sense sorry?&amp;nbsp; For the summation, SQL looks at the group by columns, so only takes ACC_NO and AMT, as these are the only items which relate to the summary statistic.&amp;nbsp; As for trying to get the last record out within SQL, this tends to be trickier.&amp;nbsp; SQL does not have the concept of a series of observations, it only looks at logical blocks of observations.&amp;nbsp; So you would need to have a logical way of finding the last record, maybe looking at your test data: where SUB_ACCNO=max(SUB_ACCNO).&amp;nbsp; Anyways, for this it is far simpler to do in datastep.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2017 15:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415851#M102062</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-23T15:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: Datastep / SQL in one step(more efficient code)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415857#M102067</link>
      <description>&lt;P&gt;Hi RW9,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You're absolute right, this is actually a test data(apologize for the poor test data). At the moment, there is no any criteria need to select which data after the summation done. I believe next will be asking criteria like last record, latest date(which i didn't put in), max/min or etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank again for your guidance, with sample code of this "where SUB_ACCNO=max(SUB_ACCNO)", at least I have idea how to do it in&amp;nbsp; Datastep and SQL as well.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2017 15:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Datastep-SQL-in-one-step-more-efficient-code/m-p/415857#M102067</guid>
      <dc:creator>sagulolo</dc:creator>
      <dc:date>2017-11-23T15:50:57Z</dc:date>
    </item>
  </channel>
</rss>

