<?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 Total between start and end in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697516#M213169</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer ID there are columns with information of debt.&lt;/P&gt;
&lt;P&gt;The name of the fields is with structure tYYMM.&lt;/P&gt;
&lt;P&gt;There is also information of start and end periods .&lt;/P&gt;
&lt;P&gt;There are&amp;nbsp; 2 required tasks:&lt;/P&gt;
&lt;P&gt;1- Calculate for each customer the total of debt between start and end months&lt;/P&gt;
&lt;P&gt;For example:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID=999 , start=1904 ,end=1906 so Total=sum(t1904,t1905,t1906)&lt;/P&gt;
&lt;P&gt;ID=888 , start=1906 ,end=1908 so Total=sum(t1906,t1907,t1908)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2- Create a new data set called "New" with new fields called L1,L2,L3&lt;/P&gt;
&lt;P&gt;Each field will contain information of tYYMM between start and end&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;ID=999 , start=1904 ,end=1906 so&lt;/P&gt;
&lt;P&gt;L1=value in t1904&lt;/P&gt;
&lt;P&gt;L2=value in t1905&lt;/P&gt;
&lt;P&gt;L3=value in t1906&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Rawtb;
input ID start end t1901 t1902 t1903 t1904 t1905 t1906 t1907 t1908 t1909 t1910 t1911 t1912;
cards;
999 1904 1906 1 3 5 7 9 2 4 6 2 7 9 4
888 1906 1908 3 5 2 1 6 1 2 8 9 1 8 7
444 1901 1903 8 5 9 1 4 1 2 6 9 1 2 3
;
run;

/*Task1:Add a new column called "Total" in data set "Rawtb" .
The new field "Total" will be calculated by sum of tYYMM columns  between start and end */
For ID=999 Total=sum(t1904,t1905,t1906);
For ID=888 Total=sum(t1906,t1907,t1908);
For ID=444 Total=sum(t1901,t1902,t1903);

/*Task2:Create a new data set with columns L1 L2 L3 with information of tYYMM between start and End*/
Data Rawtb;
input ID start end L1 L2 L3;
cards;
999 1904 1906 7 9 2
888 1906 1908 1 2 8
444 1901 1903 8 5 9
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;</description>
    <pubDate>Mon, 09 Nov 2020 07:01:59 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2020-11-09T07:01:59Z</dc:date>
    <item>
      <title>Total between start and end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697516#M213169</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer ID there are columns with information of debt.&lt;/P&gt;
&lt;P&gt;The name of the fields is with structure tYYMM.&lt;/P&gt;
&lt;P&gt;There is also information of start and end periods .&lt;/P&gt;
&lt;P&gt;There are&amp;nbsp; 2 required tasks:&lt;/P&gt;
&lt;P&gt;1- Calculate for each customer the total of debt between start and end months&lt;/P&gt;
&lt;P&gt;For example:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID=999 , start=1904 ,end=1906 so Total=sum(t1904,t1905,t1906)&lt;/P&gt;
&lt;P&gt;ID=888 , start=1906 ,end=1908 so Total=sum(t1906,t1907,t1908)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2- Create a new data set called "New" with new fields called L1,L2,L3&lt;/P&gt;
&lt;P&gt;Each field will contain information of tYYMM between start and end&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;ID=999 , start=1904 ,end=1906 so&lt;/P&gt;
&lt;P&gt;L1=value in t1904&lt;/P&gt;
&lt;P&gt;L2=value in t1905&lt;/P&gt;
&lt;P&gt;L3=value in t1906&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Rawtb;
input ID start end t1901 t1902 t1903 t1904 t1905 t1906 t1907 t1908 t1909 t1910 t1911 t1912;
cards;
999 1904 1906 1 3 5 7 9 2 4 6 2 7 9 4
888 1906 1908 3 5 2 1 6 1 2 8 9 1 8 7
444 1901 1903 8 5 9 1 4 1 2 6 9 1 2 3
;
run;

/*Task1:Add a new column called "Total" in data set "Rawtb" .
The new field "Total" will be calculated by sum of tYYMM columns  between start and end */
For ID=999 Total=sum(t1904,t1905,t1906);
For ID=888 Total=sum(t1906,t1907,t1908);
For ID=444 Total=sum(t1901,t1902,t1903);

/*Task2:Create a new data set with columns L1 L2 L3 with information of tYYMM between start and End*/
Data Rawtb;
input ID start end L1 L2 L3;
cards;
999 1904 1906 7 9 2
888 1906 1908 1 2 8
444 1901 1903 8 5 9
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Nov 2020 07:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697516#M213169</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-11-09T07:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: Total between start and end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697521#M213172</link>
      <description>&lt;P&gt;Maxims 19 &amp;amp; 33.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 07:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697521#M213172</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-09T07:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Total between start and end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697524#M213174</link>
      <description>The structure of data is in wide because this the the war data that I receive and I need to work in this situation...</description>
      <pubDate>Mon, 09 Nov 2020 07:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697524#M213174</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-11-09T07:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: Total between start and end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697528#M213176</link>
      <description>&lt;P&gt;If you want a solution that works even if the t-variables change, you have to transpose the data or write ugly code.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 07:56:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697528#M213176</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-11-09T07:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: Total between start and end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697529#M213177</link>
      <description>&lt;P&gt;When you receive the data in a sub-optimal format/layout, your FIRST step has always to be to bring the data into usable shape. This is the "T" in ETL (Extract, Transform, Load).&lt;/P&gt;
&lt;P&gt;This involves working on the dataset structure, but also transforming variable attributes to what makes later work easier. Any &lt;EM&gt;minute&lt;/EM&gt; you spend doing this saves &lt;EM&gt;hours&lt;/EM&gt; of wasted time later. This advice comes from 4 decades in IT and 2+ decades of working with SAS, so you might contemplate heeding it.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 08:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697529#M213177</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-09T08:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: Total between start and end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697532#M213180</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Rawtb;
input ID start end t1901 t1902 t1903 t1904 t1905 t1906 t1907 t1908 t1909 t1910 t1911 t1912;
cards;
999 1904 1906 1 3 5 7 9 2 4 6 2 7 9 4
888 1906 1908 3 5 2 1 6 1 2 8 9 1 8 7
444 1901 1903 8 5 9 1 4 1 2 6 9 1 2 3
;
run;

data want1 (keep = ID start end t:) 
     want2 (keep = ID start end L1-l3);
   set Rawtb;
   array t {*} t:;
   array L L1 - L3;
   _I_ = 1;
   do i = mod(start, 10) by 1 while (_I_ &amp;lt; 4);
      Total + t [i];
      L = t [i];
      _I_ + 1;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 09 Nov 2020 08:32:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697532#M213180</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-09T08:32:14Z</dc:date>
    </item>
    <item>
      <title>Re: Total between start and end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697535#M213183</link>
      <description>&lt;P&gt;And here's the whole shebang:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Extract */
Data Rawtb;
input ID start end t1901 t1902 t1903 t1904 t1905 t1906 t1907 t1908 t1909 t1910 t1911 t1912;
cards;
999 1904 1906 1 3 5 7 9 2 4 6 2 7 9 4
888 1906 1908 3 5 2 1 6 1 2 8 9 1 8 7
444 1901 1903 8 5 9 1 4 1 2 6 9 1 2 3
;

/* Transform */
proc transpose
  data=rawtb
  out=trans
;
by id notsorted;
var t:;
run;

data transaction;
set trans;
period = input(cats("20",substr(_name_,2)),yymmn6.);
format period yymmd7.;
drop _name_;
rename col1=value;
run;

data dimension;
set rawtb (rename=(start=_start end=_end));
start = input(cats("20",_start),yymmn6.);
end = input(cats("20",_end),yymmn6.);
format start end yymmd7.;
keep id start end;
run;

/* Load (done concurrently with sort) */
proc sort data=transaction;
by id period;
run;

proc sort data=dimension;
by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With this data (made intelligent by using a superior layout and proper data types (dates!)), the two wanted tables can be created in one step with easy code that will adapt itself seamlessly to the input data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data
  want1 (keep=id start end total)
  want2 (keep=id start end l value)
;
merge
  dimension
  transaction
;
by id;
if first.id
then do;
  total = 0;
  l = 0;
end;
if start le period le end
then do;
  total + value;
  l + 1;
  output want2;
end;
if last.id then output want1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I kept a long layout for want2; a wide report for human consumption including export to Excel can easily be done with PROC REPORT:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report data=want2;
column id start end l,value;
define id / group;
define start / group;
define end / group;
define l / "" across;
define value / sum;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Nov 2020 08:34:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697535#M213183</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-09T08:34:37Z</dc:date>
    </item>
    <item>
      <title>Re: Total between start and end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697536#M213184</link>
      <description>&lt;P&gt;Note that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s code will fail as soon as start and end are more than 2 periods apart. The code I posted will be completely independent from this and need no modification. This is what you want to achieve, or you'll spend close to 100% of your future time adapting existing code instead of solving new issues.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 08:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-between-start-and-end/m-p/697536#M213184</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-09T08:38:56Z</dc:date>
    </item>
  </channel>
</rss>

