<?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 How to sum columns based on date ranges. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-columns-based-on-date-ranges/m-p/871770#M344433</link>
    <description>&lt;P&gt;I have to get pre and post sums based on install date. The date is in the variable name which i capture using the vname. I am having trouble creating an index on the variables to sum each one based on their install date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;here is my code so far.&amp;nbsp; I am missing something to capture the index of months. I saw a similar post about this but cannot find it anymore.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set test;&lt;BR /&gt;array KWH(*) USAGE_202001 USAGE_202002 USAGE_202003 USAGE_202004&lt;BR /&gt;USAGE_202005 USAGE_202006 USAGE_202007 USAGE_202008 USAGE_202009&lt;BR /&gt;USAGE_202010 USAGE_202011 USAGE_202012 USAGE_202101 USAGE_202102&lt;BR /&gt;USAGE_202103 USAGE_202104 USAGE_202105 USAGE_202106 USAGE_202107&lt;BR /&gt;USAGE_202108 USAGE_202109 USAGE_202110 USAGE_202111 USAGE_202112&lt;BR /&gt;USAGE_202201 USAGE_202202 USAGE_202203 USAGE_202204 USAGE_202205&lt;BR /&gt;USAGE_202206 USAGE_202207 USAGE_202208 USAGE_202209 USAGE_202210&lt;BR /&gt;USAGE_202211 USAGE_202012 USAGE_202301 USAGE_202302 USAGE_202303;&lt;/P&gt;&lt;P&gt;array months(*) month1 - month39;&lt;/P&gt;&lt;P&gt;do i = 1 to 39;&lt;BR /&gt;months(i) = input(substr(vname(KWH(i)),7,6),6.);&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;PRE_strt_indx = whichn(PRE_stArt_dt, of months(*));&lt;BR /&gt;PRE_end_indx = whichn(PRE_end_dt, of months(*));&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;* sum the monthly pays in the range;&lt;/P&gt;&lt;P&gt;do i = pre_strt_indx to pre_end_indx;&lt;BR /&gt;sum_usage =sum(sum_usage, kwh(i));&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Tue, 25 Apr 2023 03:32:59 GMT</pubDate>
    <dc:creator>bibbnd</dc:creator>
    <dc:date>2023-04-25T03:32:59Z</dc:date>
    <item>
      <title>How to sum columns based on date ranges.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-columns-based-on-date-ranges/m-p/871770#M344433</link>
      <description>&lt;P&gt;I have to get pre and post sums based on install date. The date is in the variable name which i capture using the vname. I am having trouble creating an index on the variables to sum each one based on their install date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;here is my code so far.&amp;nbsp; I am missing something to capture the index of months. I saw a similar post about this but cannot find it anymore.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set test;&lt;BR /&gt;array KWH(*) USAGE_202001 USAGE_202002 USAGE_202003 USAGE_202004&lt;BR /&gt;USAGE_202005 USAGE_202006 USAGE_202007 USAGE_202008 USAGE_202009&lt;BR /&gt;USAGE_202010 USAGE_202011 USAGE_202012 USAGE_202101 USAGE_202102&lt;BR /&gt;USAGE_202103 USAGE_202104 USAGE_202105 USAGE_202106 USAGE_202107&lt;BR /&gt;USAGE_202108 USAGE_202109 USAGE_202110 USAGE_202111 USAGE_202112&lt;BR /&gt;USAGE_202201 USAGE_202202 USAGE_202203 USAGE_202204 USAGE_202205&lt;BR /&gt;USAGE_202206 USAGE_202207 USAGE_202208 USAGE_202209 USAGE_202210&lt;BR /&gt;USAGE_202211 USAGE_202012 USAGE_202301 USAGE_202302 USAGE_202303;&lt;/P&gt;&lt;P&gt;array months(*) month1 - month39;&lt;/P&gt;&lt;P&gt;do i = 1 to 39;&lt;BR /&gt;months(i) = input(substr(vname(KWH(i)),7,6),6.);&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;PRE_strt_indx = whichn(PRE_stArt_dt, of months(*));&lt;BR /&gt;PRE_end_indx = whichn(PRE_end_dt, of months(*));&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;* sum the monthly pays in the range;&lt;/P&gt;&lt;P&gt;do i = pre_strt_indx to pre_end_indx;&lt;BR /&gt;sum_usage =sum(sum_usage, kwh(i));&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 03:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-columns-based-on-date-ranges/m-p/871770#M344433</guid>
      <dc:creator>bibbnd</dc:creator>
      <dc:date>2023-04-25T03:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum columns based on date ranges.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-columns-based-on-date-ranges/m-p/871778#M344439</link>
      <description>&lt;P&gt;Transpose your usage_* variables to a long dataset, and extract the month as a date from _NAME_.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose
  data=test
  out=long (rename=(col1=usage) where=(usage not in (0,.)))
;
by /* insert your identifying key here */;
var usage_:;
run;

data long_2;
set long;
month = input(scan(_name_,2,'_'),yymmn6.);
format month yymmn6.;
drop _name_;
run;

data want;
merge
  test
  long_2
;
by /* insert key here */;
if first./*key*/
then sum_usage = 0;
if intnx('month',pre_start_dt,0,'b') le month le pre_end_dt then sum_usage + usage;
if last./*key*/;
drop month usage;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Apr 2023 04:48:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-columns-based-on-date-ranges/m-p/871778#M344439</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-25T04:48:21Z</dc:date>
    </item>
  </channel>
</rss>

