<?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: sum of the first two transactions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877472#M346645</link>
    <description>For example when i run my code, on cust_no=1 my rn column gives me 1,2,3,4,5 for all those five rows in the order they appear on my data want.</description>
    <pubDate>Thu, 25 May 2023 09:35:27 GMT</pubDate>
    <dc:creator>Solly7</dc:creator>
    <dc:date>2023-05-25T09:35:27Z</dc:date>
    <item>
      <title>sum of the first two transactions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877457#M346641</link>
      <description>&lt;P&gt;Hi , i need to sum the amount of the first two transactions per cust_no on the below data&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;DATA HAVE&amp;nbsp; (TRNS_TABLE)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;cust_no&amp;nbsp; &amp;nbsp;prod_code&amp;nbsp; &amp;nbsp; &amp;nbsp;yearmonth&amp;nbsp; &amp;nbsp; &amp;nbsp; TRNS_AMT&amp;nbsp; &amp;nbsp;TXN_TIME&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; EFF_DATE&amp;nbsp;&amp;nbsp;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WX&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 202212&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;500.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9:09:40.000000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 17DEC2022&amp;nbsp;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WX&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;202212&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12:36:19.000000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;19DEC2022&amp;nbsp;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WX&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;202301&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;500.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 11:14:00.000000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30JAN2023&amp;nbsp;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WX&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;202301&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;500.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15:42:32.000000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30JAN2023&amp;nbsp;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WX&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;202301&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 450.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12:18:57.000000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31JAN2023&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AB&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 202302&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1000.00&amp;nbsp; &amp;nbsp; &amp;nbsp; 12:22:45.00000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01FEB2023&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data is already orderd by eff_date and txn_time. The &lt;STRONG&gt;eff_date is date9&lt;/STRONG&gt;. format, &lt;STRONG&gt;txn_time is time15.6 format&lt;/STRONG&gt;. I need to sum first two transactions per cust_no and summarize my data per cust_no and yearmonth.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DATA WANT&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;cust_no&amp;nbsp; &amp;nbsp; &amp;nbsp;yearmonth&amp;nbsp; &amp;nbsp; sum_first_two&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;202212&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 700&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;202301&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 202302&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1000&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried below code didnt work&lt;/P&gt;
&lt;PRE&gt;data temp;
set trns_table;
by cust_no eff_date;
retain rn;
if first.cust_no then rn = 1;
else rn + 1;
run;

proc sql;
create table summary as
select
  cust_no,
  yearmonth,
  sum(
    case when rn &amp;lt;= 2 then TRNS_AMT else 0 end
  ) as first_two_trns
from temp
group by cust_no, yearmonth;
quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 May 2023 08:16:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877457#M346641</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2023-05-25T08:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: sum of the first two transactions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877469#M346643</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 09:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877469#M346643</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2023-05-25T09:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: sum of the first two transactions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877471#M346644</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I tried below code didnt work&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Never (that's NEVER) tell us something didn't work, without giving more explanation. We don't know what problems you are seeing. Please explain why you say it "didn't work", what is the evidence? Be specific and detailed.&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 09:25:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877471#M346644</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-25T09:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: sum of the first two transactions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877472#M346645</link>
      <description>For example when i run my code, on cust_no=1 my rn column gives me 1,2,3,4,5 for all those five rows in the order they appear on my data want.</description>
      <pubDate>Thu, 25 May 2023 09:35:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877472#M346645</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2023-05-25T09:35:27Z</dc:date>
    </item>
    <item>
      <title>Re: sum of the first two transactions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877474#M346646</link>
      <description>&lt;P&gt;Please provide the data as &lt;FONT color="#FF0000"&gt;working&lt;/FONT&gt; SAS data step code, which you can type in yourself or follow these &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 09:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877474#M346646</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-25T09:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: sum of the first two transactions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877478#M346647</link>
      <description>&lt;PRE&gt;data TRNS_TABLE;

informat EFF_DATE date9.;
informat TXN_TIME time15.6;

format EFF_DATE date9.;
format TXN_TIME time15.6;

input cust_no prod_code $ yearmonth TRNS_AMT TXN_TIME EFF_DATE;

datalines;
1 WX 202212 500.00 09:09:40.000000 17DEC2022
1 WX 202212 200.00 12:36:19.000000 19DEC2022
1 WX 202301 500.00 11:14:00.000000 30JAN2023
1 WX 202301 500.00 15:42:32.000000 30JAN2023
1 WX 202301 450.00 12:18:57.000000 31JAN2023
2 AB 202302 1000.00 12:22:45.000000 01FEB2023
;

run;
&lt;/PRE&gt;
&lt;P&gt;Hi see above working dataset&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 09:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877478#M346647</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2023-05-25T09:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: sum of the first two transactions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877482#M346648</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I need to sum first two transactions per cust_no and summarize my data per cust_no and yearmonth.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then your code says:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;by cust_no eff_date;
retain rn;
if first.cust_no then rn = 1;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You haven't programmed what you said. You programmed first two transactions per cust_no and eff_date. What change should you make so that you get first two transactions by cust_no and yearmonth?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In addition, when you use &lt;FONT face="courier new,courier"&gt;first.cust_no&lt;/FONT&gt;, you are re-starting the counter RN every time you have a new cust_no. But again, that's not what you said you want, you want the counter to re-start every time there is a new yearmonth. What change would you need?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lastly, doing this sum in SQL is a lot of typing. Much less typing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=temp(where=(rn&amp;lt;=2)) nway;
    class cust_no yearmonth;
    var trns_amt;
    output out=want sum=sum_trns_amt;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 May 2023 10:10:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877482#M346648</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-25T10:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: sum of the first two transactions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877488#M346649</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;, your proposed solution gives me desired results. Thanks a lot, always learning from you!&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 11:05:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-the-first-two-transactions/m-p/877488#M346649</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2023-05-25T11:05:41Z</dc:date>
    </item>
  </channel>
</rss>

