<?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: Distinct Cumulative totals year to date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605140#M175550</link>
    <description>&lt;P&gt;You likely need to also include a bit of what your starting data looks like.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Nov 2019 18:32:59 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-11-18T18:32:59Z</dc:date>
    <item>
      <title>Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605135#M175546</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to get the total distinct values by month and year to date. The logic in my code does the sum and I can't think through how to get the distinct values. I have attached a worksheet with the sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.BOOK4;
  infile datalines dsd truncover;
  input ACTUAL:DOLLAR20.2 COUNTRY:$6. PRODTYPE:$9. PRODUCT:$20. YEAR:F12. MONTH:$3.;
datalines4;
$107.00,CANADA,FURNITURE,SOFA,1994,Jan
$354.00,CANADA,FURNITURE,SOFA,1994,Feb
$101.00,CANADA,FURNITURE,OTTOMAN,1994,Mar
$553.00,CANADA,FURNITURE,SOFA,1994,Apr
$877.00,CANADA,FURNITURE,SOFA,1994,May
$431.00,CANADA,FURNITURE,SOFA,1994,Jun
$511.00,CANADA,FURNITURE,CHAIR,1994,Jul
$157.00,CANADA,FURNITURE,STOOL,1994,Aug
$520.00,CANADA,FURNITURE,SOFA,1994,Sep
$114.00,CANADA,FURNITURE,SOFA,1994,Oct
$277.00,CANADA,FURNITURE,SOFA,1994,Nov
$561.00,CANADA,FURNITURE,CUPBOARD,1994,Dec
$267.00,CANADA,FURNITURE,BED,1994,Jan
$347.00,CANADA,FURNITURE,BED,1994,Feb
$991.00,CANADA,FURNITURE,BED,1994,Mar
$923.00,CANADA,FURNITURE,BED,1994,Apr
$437.00,CANADA,FURNITURE,DESK,1994,May
$737.00,CANADA,FURNITURE,BED,1994,Jun
$104.00,CANADA,FURNITURE,BED,1994,Jul
$840.00,CANADA,FURNITURE,BED,1994,Aug
$704.00,CANADA,FURNITURE,BED,1994,Sep
$889.00,CANADA,FURNITURE,BED,1994,Oct
$107.00,CANADA,FURNITURE,LOVESEAT,1994,Nov
$571.00,CANADA,FURNITURE,BED,1994,Dec
$355.00,CANADA,FURNITURE,TABLE,1994,Jan
$506.00,CANADA,FURNITURE,TABLE,1994,Feb
$585.00,CANADA,FURNITURE,DRESSER,1994,Mar
$634.00,CANADA,FURNITURE,TABLE,1994,Apr
$662.00,CANADA,FURNITURE,TABLE,1994,May
$783.00,CANADA,FURNITURE,TABLE,1994,Jun
$786.00,CANADA,FURNITURE,TABLE,1994,Jul
$710.00,CANADA,FURNITURE,CHAIR,1994,Aug
$950.00,CANADA,FURNITURE,TABLE,1994,Sep
$274.00,CANADA,FURNITURE,SOFA,1994,Oct
$406.00,CANADA,FURNITURE,TABLE,1994,Nov
$515.00,CANADA,FURNITURE,TABLE,1994,Dec
$877.00,CANADA,FURNITURE,BED,1994,Jan
$845.00,CANADA,FURNITURE,CHAIR,1994,Feb
$425.00,CANADA,FURNITURE,CHAIR,1994,Mar
$899.00,CANADA,FURNITURE,BENCH,1994,Apr
$987.00,CANADA,FURNITURE,DESK,1994,May
$641.00,CANADA,FURNITURE,BED,1994,Jun
$448.00,CANADA,FURNITURE,CHAIR,1994,Jul
$341.00,CANADA,FURNITURE,CHAIR,1994,Aug
$137.00,CANADA,FURNITURE,CHAIR,1994,Sep
$235.00,CANADA,FURNITURE,CHAIR,1994,Oct
$482.00,CANADA,FURNITURE,CHAIR,1994,Nov
$678.00,CANADA,FURNITURE,CHAIR,1994,Dec
$942.00,CANADA,FURNITURE,DESK,1994,Jan
$912.00,CANADA,FURNITURE,DESK,1994,Feb
$768.00,CANADA,FURNITURE,CHAIR,1994,Mar
$951.00,CANADA,FURNITURE,DESK,1994,Apr
$768.00,CANADA,FURNITURE,NIGHTSTAND,1994,May
$978.00,CANADA,FURNITURE,DESK,1994,Jun
$20.00,CANADA,FURNITURE,DESK,1994,Jul
$298.00,CANADA,FURNITURE,DESK,1994,Aug
$193.00,CANADA,FURNITURE,TABLE,1994,Sep
$336.00,CANADA,FURNITURE,DESK,1994,Oct
$617.00,CANADA,FURNITURE,DESK,1994,Nov
$709.00,CANADA,FURNITURE,SOFA,1994,Dec
;;;;

proc sql;
create table cur_month as
select
Country,
prodtype,
Month,
"Current Month" as Type,
sum(actual) as Actual,
count(distinct product) as Prod_Type
from book4
group by country,  prodtype, month
order  by country,  prodtype, month;
quit;

data ytd (rename=(ytd_Actual = Actual ytd_type = Prod_Type));
set cur_month;
by  country prodtype;

if first.prodtype then ytd_actual = 0;
ytd_actual+ actual;

if first.prodtype then ytd_type = 0;
ytd_type + prod_type;

Type = "YTD";

drop  actual prod_type;
run;

data combined;
	set cur_month ytd;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT style="background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/FONT&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;P&gt;This is the output I get, The issues is on the Prod_Type column&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;COUNTRY	PRODTYPE	MONTH	Type		Actual	Prod_Type
CANADA	FURNITURE	Jan	Current Month	2548	4
CANADA	FURNITURE	Feb	Current Month	2964	5
CANADA	FURNITURE	Mar	Current Month	2870	4
CANADA	FURNITURE	Apr	Current Month	3960	5
CANADA	FURNITURE	May	Current Month	3731	4
CANADA	FURNITURE	Jun	Current Month	3570	4
CANADA	FURNITURE	Jul	Current Month	1869	4
CANADA	FURNITURE	Aug	Current Month	2346	4
CANADA	FURNITURE	Sep	Current Month	2504	4
CANADA	FURNITURE	Oct	Current Month	1848	4
CANADA	FURNITURE	Nov	Current Month	1889	5
CANADA	FURNITURE	Dec	Current Month	3034	5
CANADA	FURNITURE	Jan	YTD		2548	4
CANADA	FURNITURE	Feb	YTD		5512	9
CANADA	FURNITURE	Mar	YTD		8382	13
CANADA	FURNITURE	Apr	YTD		12342	18
CANADA	FURNITURE	May	YTD		16073	22
CANADA	FURNITURE	Jun	YTD		19643	26
CANADA	FURNITURE	Jul	YTD		21512	30
CANADA	FURNITURE	Aug	YTD		23858	34
CANADA	FURNITURE	Sep	YTD		26362	38
CANADA	FURNITURE	Oct	YTD		28210	42
CANADA	FURNITURE	Nov	YTD		30099	47
CANADA	FURNITURE	Dec	YTD		33133	52&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However this is my desired output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;COUNTRY PRODTYPE MONTH Type 	   	Actual Prod_Type 
CANADA FURNITURE Jan Current Month 	2548 	4 
CANADA FURNITURE Feb Current Month 	2964 	5 
CANADA FURNITURE Mar Current Month 	2870 	4 
CANADA FURNITURE Apr Current Month 	3960 	5 
CANADA FURNITURE May Current Month 	3731 	4 
CANADA FURNITURE Jun Current Month 	3570 	4 
CANADA FURNITURE Jul Current Month 	1869	4 
CANADA FURNITURE Aug Current Month 	2346 	4 
CANADA FURNITURE Sep Current Month 	2504 	4 
CANADA FURNITURE Oct Current Month 	1848 	4 
CANADA FURNITURE Nov Current Month	1889 	5 
CANADA FURNITURE Dec Current Month 	3034 	5 
CANADA FURNITURE Jan YTD 		2548 	4 sofa, bed table, desk
CANADA FURNITURE Feb YTD 		5512 	5 chair
CANADA FURNITURE Mar YTD	        8382 	7 dresser, ottoman
CANADA FURNITURE Apr YTD 		12342 	8 bench
CANADA FURNITURE May YTD 		16073 	9 nightstand
CANADA FURNITURE Jun YTD 		19643 	9 
CANADA FURNITURE Jul YTD 		21512 	9 
CANADA FURNITURE Aug YTD 		23858 	10 stool
CANADA FURNITURE Sep YTD 		26362 	10 
CANADA FURNITURE Oct YTD 		28210 	10 
CANADA FURNITURE Nov YTD 		30099 	11 loveseat
CANADA FURNITURE Dec YTD 		33133 	12 cupboard&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any assistance I can get will be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 20:17:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605135#M175546</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-18T20:17:27Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605139#M175549</link>
      <description>&lt;P&gt;Please explain how March YTD in the desired output is 7. How is this computed?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, where does the "dresser, ottoman" come from?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 18:30:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605139#M175549</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-18T18:30:58Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605140#M175550</link>
      <description>&lt;P&gt;You likely need to also include a bit of what your starting data looks like.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 18:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605140#M175550</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-18T18:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605152#M175556</link>
      <description>&lt;P&gt;Any attachment of data apparently didn't make it.&lt;/P&gt;
&lt;P&gt;Better is to provide data step code of data and paste as text in a code box:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 18:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605152#M175556</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-18T18:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605160#M175563</link>
      <description>&lt;P&gt;I just added the data (book4.xlsx)&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 19:09:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605160#M175563</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-18T19:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605162#M175565</link>
      <description>I have updated the post with the data. Please advise if you can see it. Thank you.</description>
      <pubDate>Mon, 18 Nov 2019 19:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605162#M175565</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-18T19:08:54Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605173#M175572</link>
      <description>&lt;P&gt;Please read &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;'s post again. Excel spreadsheets are&amp;nbsp;&lt;EM&gt;not&lt;/EM&gt; SAS datasets.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 19:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605173#M175572</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-18T19:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605180#M175573</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/29035"&gt;@NewSASPerson&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I have updated the post with the data. Please advise if you can see it. Thank you.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Many of us will not open Microsoft Office documents because they are a security threat. Please provide the data as described in the link from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also please explain how, in the desired output, March YTD is equal to 7. What are the calculations?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 19:55:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605180#M175573</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-18T19:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605182#M175575</link>
      <description>&lt;P&gt;Instead of adding a spreadsheet, you'll probably get a lot faster response, and more responses, if you utilize the advice from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P style="box-sizing: inherit; color: #333333; font-family: &amp;amp;quot; helevticaneue-light&amp;amp;quot;,&amp;amp;quot;helvetica neue&amp;amp;quot;,helvetica,arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; padding: 0px; margin: 0px;"&gt;Better is to provide data step code of data and paste as text in a code box:&lt;/P&gt;
&lt;P style="box-sizing: inherit; color: #333333; font-family: &amp;amp;quot; helevticaneue-light&amp;amp;quot;,&amp;amp;quot;helvetica neue&amp;amp;quot;,helvetica,arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="box-sizing: inherit; color: #333333; font-family: &amp;amp;quot; helevticaneue-light&amp;amp;quot;,&amp;amp;quot;helvetica neue&amp;amp;quot;,helvetica,arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; padding: 0px; margin: 0px;"&gt;Instructions here: &lt;A style="background-attachment: scroll; background-clip: border-box; background-color: transparent; background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: repeat; background-size: auto; box-sizing: inherit; color: #007dc3; text-decoration: none;" href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know that I am always more ready to provide actual code when the person asking for help creates a sas data step appropriate to her question.&amp;nbsp; Help us help you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 20:01:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605182#M175575</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-18T20:01:28Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605187#M175578</link>
      <description>&lt;P&gt;I have update the code. It should have the data step now. Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 20:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605187#M175578</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-18T20:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605192#M175580</link>
      <description>I have update the code. It should have the data step now. Thank you.</description>
      <pubDate>Mon, 18 Nov 2019 20:35:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605192#M175580</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-18T20:35:16Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605238#M175616</link>
      <description>&lt;P&gt;I don't duplicate your output.&lt;/P&gt;
&lt;P&gt;The data from the first proc SQL creating WORK.CUR_MONTH has the output in Alphabetical Month Order: Apr, Aug, Dec etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are doing something such that MONTH is numeric 1 to 12 it is not shown in the code.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 22:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605238#M175616</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-18T22:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605289#M175647</link>
      <description>&lt;P&gt;Thank you for setting up the DATA step for this problem.&amp;nbsp; As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;said, your program orders the months alphabetically instead of in calendar order.&amp;nbsp; So I modified your data step to add a MONTHNUM variable, which is then added to the group by and sort by clauses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.BOOK4;
  infile datalines dsd truncover;
  input ACTUAL:DOLLAR20.2 COUNTRY:$6. PRODTYPE:$9. PRODUCT:$20. YEAR:F12. MONTH:$3.;
  monthnum=findw('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec',month,' ','e');
datalines4;
$107.00,CANADA,FURNITURE,SOFA,1994,Jan
$354.00,CANADA,FURNITURE,SOFA,1994,Feb
$101.00,CANADA,FURNITURE,OTTOMAN,1994,Mar
$553.00,CANADA,FURNITURE,SOFA,1994,Apr
$877.00,CANADA,FURNITURE,SOFA,1994,May
$431.00,CANADA,FURNITURE,SOFA,1994,Jun
$511.00,CANADA,FURNITURE,CHAIR,1994,Jul
$157.00,CANADA,FURNITURE,STOOL,1994,Aug
$520.00,CANADA,FURNITURE,SOFA,1994,Sep
$114.00,CANADA,FURNITURE,SOFA,1994,Oct
$277.00,CANADA,FURNITURE,SOFA,1994,Nov
$561.00,CANADA,FURNITURE,CUPBOARD,1994,Dec
$267.00,CANADA,FURNITURE,BED,1994,Jan
$347.00,CANADA,FURNITURE,BED,1994,Feb
$991.00,CANADA,FURNITURE,BED,1994,Mar
$923.00,CANADA,FURNITURE,BED,1994,Apr
$437.00,CANADA,FURNITURE,DESK,1994,May
$737.00,CANADA,FURNITURE,BED,1994,Jun
$104.00,CANADA,FURNITURE,BED,1994,Jul
$840.00,CANADA,FURNITURE,BED,1994,Aug
$704.00,CANADA,FURNITURE,BED,1994,Sep
$889.00,CANADA,FURNITURE,BED,1994,Oct
$107.00,CANADA,FURNITURE,LOVESEAT,1994,Nov
$571.00,CANADA,FURNITURE,BED,1994,Dec
$355.00,CANADA,FURNITURE,TABLE,1994,Jan
$506.00,CANADA,FURNITURE,TABLE,1994,Feb
$585.00,CANADA,FURNITURE,DRESSER,1994,Mar
$634.00,CANADA,FURNITURE,TABLE,1994,Apr
$662.00,CANADA,FURNITURE,TABLE,1994,May
$783.00,CANADA,FURNITURE,TABLE,1994,Jun
$786.00,CANADA,FURNITURE,TABLE,1994,Jul
$710.00,CANADA,FURNITURE,CHAIR,1994,Aug
$950.00,CANADA,FURNITURE,TABLE,1994,Sep
$274.00,CANADA,FURNITURE,SOFA,1994,Oct
$406.00,CANADA,FURNITURE,TABLE,1994,Nov
$515.00,CANADA,FURNITURE,TABLE,1994,Dec
$877.00,CANADA,FURNITURE,BED,1994,Jan
$845.00,CANADA,FURNITURE,CHAIR,1994,Feb
$425.00,CANADA,FURNITURE,CHAIR,1994,Mar
$899.00,CANADA,FURNITURE,BENCH,1994,Apr
$987.00,CANADA,FURNITURE,DESK,1994,May
$641.00,CANADA,FURNITURE,BED,1994,Jun
$448.00,CANADA,FURNITURE,CHAIR,1994,Jul
$341.00,CANADA,FURNITURE,CHAIR,1994,Aug
$137.00,CANADA,FURNITURE,CHAIR,1994,Sep
$235.00,CANADA,FURNITURE,CHAIR,1994,Oct
$482.00,CANADA,FURNITURE,CHAIR,1994,Nov
$678.00,CANADA,FURNITURE,CHAIR,1994,Dec
$942.00,CANADA,FURNITURE,DESK,1994,Jan
$912.00,CANADA,FURNITURE,DESK,1994,Feb
$768.00,CANADA,FURNITURE,CHAIR,1994,Mar
$951.00,CANADA,FURNITURE,DESK,1994,Apr
$768.00,CANADA,FURNITURE,NIGHTSTAND,1994,May
$978.00,CANADA,FURNITURE,DESK,1994,Jun
$20.00,CANADA,FURNITURE,DESK,1994,Jul
$298.00,CANADA,FURNITURE,DESK,1994,Aug
$193.00,CANADA,FURNITURE,TABLE,1994,Sep
$336.00,CANADA,FURNITURE,DESK,1994,Oct
$617.00,CANADA,FURNITURE,DESK,1994,Nov
$709.00,CANADA,FURNITURE,SOFA,1994,Dec
;;;;

proc sql;
create table cur_month as
select
Country,
prodtype,
Month,
"Current Month" as Type,
sum(actual) as Actual,
count(distinct product) as Prod_Type
from book4
group by country,  prodtype, monthnum, month
order  by country,  prodtype, monthnum, month;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then, to follow the twelve months of current values, by the same twelve months year with YTD values, you can read the CUR_MONTH data set twice, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want (drop=cum_:);
  do until (last.prodtype);
    set cur_month ;
	by country prodtype;
	output;
  end;
  do until (last.prodtype);
    set cur_month ;
	by country prodtype;
	cum_actual+actual;
	cum_pt+prod_type;
	type='YTD';
	actual=cum_actual;
	prod_type=cum_pt;
	output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, if you have multiple years, and your want to do this for each year, you need to add YEAR to the group by and sort by clauses, between PRODTYPE and MONTHNUM.&amp;nbsp; And you would have to change the data step to use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do until (last.year);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by country prodtype year;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2019 04:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605289#M175647</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-19T04:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605326#M175668</link>
      <description>Thank you for your help in fixing the data step. However, this does not give me the desire result. It gives the same result I already have.&lt;BR /&gt;</description>
      <pubDate>Tue, 19 Nov 2019 07:49:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605326#M175668</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-19T07:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605454#M175727</link>
      <description>&lt;P&gt;Ah, it's the list of newly-encountered products you want included in the YTD records.&amp;nbsp; To do this in the data step, you can use a sorted version of your original dataset (by country prodtype&amp;nbsp;month).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Such a dataset can be interleaved with the results of your sql procedure&amp;nbsp; (by country prodtype monthnum), such that for each month, all the book4 products (sorted below) will be read, followed by the single record from the sql data set.&amp;nbsp; This program should do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.BOOK4;
  infile datalines dsd truncover;
  input ACTUAL:DOLLAR20.2 COUNTRY:$6. PRODTYPE:$9. PRODUCT:$20. YEAR:F12. MONTH:$3.;
  monthnum=findw('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec',month,' ','e');
datalines4;
$107.00,CANADA,FURNITURE,SOFA,1994,Jan
$354.00,CANADA,FURNITURE,SOFA,1994,Feb
$101.00,CANADA,FURNITURE,OTTOMAN,1994,Mar
$553.00,CANADA,FURNITURE,SOFA,1994,Apr
$877.00,CANADA,FURNITURE,SOFA,1994,May
$431.00,CANADA,FURNITURE,SOFA,1994,Jun
$511.00,CANADA,FURNITURE,CHAIR,1994,Jul
$157.00,CANADA,FURNITURE,STOOL,1994,Aug
$520.00,CANADA,FURNITURE,SOFA,1994,Sep
$114.00,CANADA,FURNITURE,SOFA,1994,Oct
$277.00,CANADA,FURNITURE,SOFA,1994,Nov
$561.00,CANADA,FURNITURE,CUPBOARD,1994,Dec
$267.00,CANADA,FURNITURE,BED,1994,Jan
$347.00,CANADA,FURNITURE,BED,1994,Feb
$991.00,CANADA,FURNITURE,BED,1994,Mar
$923.00,CANADA,FURNITURE,BED,1994,Apr
$437.00,CANADA,FURNITURE,DESK,1994,May
$737.00,CANADA,FURNITURE,BED,1994,Jun
$104.00,CANADA,FURNITURE,BED,1994,Jul
$840.00,CANADA,FURNITURE,BED,1994,Aug
$704.00,CANADA,FURNITURE,BED,1994,Sep
$889.00,CANADA,FURNITURE,BED,1994,Oct
$107.00,CANADA,FURNITURE,LOVESEAT,1994,Nov
$571.00,CANADA,FURNITURE,BED,1994,Dec
$355.00,CANADA,FURNITURE,TABLE,1994,Jan
$506.00,CANADA,FURNITURE,TABLE,1994,Feb
$585.00,CANADA,FURNITURE,DRESSER,1994,Mar
$634.00,CANADA,FURNITURE,TABLE,1994,Apr
$662.00,CANADA,FURNITURE,TABLE,1994,May
$783.00,CANADA,FURNITURE,TABLE,1994,Jun
$786.00,CANADA,FURNITURE,TABLE,1994,Jul
$710.00,CANADA,FURNITURE,CHAIR,1994,Aug
$950.00,CANADA,FURNITURE,TABLE,1994,Sep
$274.00,CANADA,FURNITURE,SOFA,1994,Oct
$406.00,CANADA,FURNITURE,TABLE,1994,Nov
$515.00,CANADA,FURNITURE,TABLE,1994,Dec
$877.00,CANADA,FURNITURE,BED,1994,Jan
$845.00,CANADA,FURNITURE,CHAIR,1994,Feb
$425.00,CANADA,FURNITURE,CHAIR,1994,Mar
$899.00,CANADA,FURNITURE,BENCH,1994,Apr
$987.00,CANADA,FURNITURE,DESK,1994,May
$641.00,CANADA,FURNITURE,BED,1994,Jun
$448.00,CANADA,FURNITURE,CHAIR,1994,Jul
$341.00,CANADA,FURNITURE,CHAIR,1994,Aug
$137.00,CANADA,FURNITURE,CHAIR,1994,Sep
$235.00,CANADA,FURNITURE,CHAIR,1994,Oct
$482.00,CANADA,FURNITURE,CHAIR,1994,Nov
$678.00,CANADA,FURNITURE,CHAIR,1994,Dec
$942.00,CANADA,FURNITURE,DESK,1994,Jan
$912.00,CANADA,FURNITURE,DESK,1994,Feb
$768.00,CANADA,FURNITURE,CHAIR,1994,Mar
$951.00,CANADA,FURNITURE,DESK,1994,Apr
$768.00,CANADA,FURNITURE,NIGHTSTAND,1994,May
$978.00,CANADA,FURNITURE,DESK,1994,Jun
$20.00,CANADA,FURNITURE,DESK,1994,Jul
$298.00,CANADA,FURNITURE,DESK,1994,Aug
$193.00,CANADA,FURNITURE,TABLE,1994,Sep
$336.00,CANADA,FURNITURE,DESK,1994,Oct
$617.00,CANADA,FURNITURE,DESK,1994,Nov
$709.00,CANADA,FURNITURE,SOFA,1994,Dec
;;;;

proc sql;
  create table cur_month as
  select  Country,prodtype,Month,monthnum,"Current Month" as Type,
    sum(actual) as Actual,
    count(distinct product) as Prod_Type
  from book4
  group by country,  prodtype, monthnum,  month
  order  by country,  prodtype, monthnum, month;
quit;

proc sort data=book4 out=book4s;
  by country prodtype year monthnum;
run;

data want (drop=cum_: product);
  do until (last.prodtype);
    set cur_month ;
    by country prodtype;
    output;
  end;

  length cum_list $300 prod_list $50;
  do until (last.prodtype);
    set book4s (keep=country prodtype monthnum product in=inprod) 
        cur_month ;
    by country prodtype monthnum;
	if first.monthnum then call missing(prod_list);
    if inprod then do;
      if findw(cum_list,trim(product),' ','E')=0 then do;
        prod_list=catx(' ',prod_list,product);
        cum_list=catx(' ',cum_list,product);
      end;
      continue;  /* Go back to top of loop */
    end;
    cum_actual+actual;
    cum_pt+prod_type;
    type='YTD';
    actual=cum_actual;
    prod_type=cum_pt;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How this works:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;In the DATA step, the first do loop writes out the results taken from the sql procedure.&lt;/LI&gt;
&lt;LI&gt;In the second do loop,
&lt;OL&gt;
&lt;LI&gt;the SET statement reads, for each country/prodtype/monthnum&amp;nbsp; all records from BOOK4S, followed by the single corresponding record from CUR_MONTH.&lt;/LI&gt;
&lt;LI&gt;The "if inprod" tests for whether the incoming record is from BOOK4S.&amp;nbsp; For such records the product tests whether the incoming PRODUCT has already been encountered and has been stored in CUM_LIST.&amp;nbsp; If it is not then it is added to CUM_LIST and also to PROD_LIST - the variable that will be output in the YTD record.&lt;/LI&gt;
&lt;LI&gt;The CONTINUE statement tell SAS to back to the beginning of the DO loop, which is what is needed for incoming BOOK4S records.&lt;/LI&gt;
&lt;LI&gt;Otherwise, if the incoming record is not from BOOK4S (inprod not equal to 1) it is from the sql data set, where ytd values are calculated and output.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Tue, 19 Nov 2019 16:57:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605454#M175727</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-19T16:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605490#M175743</link>
      <description>&lt;P&gt;My apologies that this is not clearer.&amp;nbsp; It is the values on Prod_Type that is different. For instance, For : Feb, YTD . the Prod_Type value should be 5 but the total is 9 instead. Mar, YTD should be 7 instead of 13 etc. I had put the furniture type next to the numbers to clarify how it should be being added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2019 18:58:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605490#M175743</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-19T18:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605520#M175757</link>
      <description>&lt;P&gt;So you want prod_type to be a YTD count of distinct PRODUCT values encountered.&amp;nbsp; Then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.BOOK4;
  infile datalines dsd truncover;
  input ACTUAL:DOLLAR20.2 COUNTRY:$6. PRODTYPE:$9. PRODUCT:$20. YEAR:F12. MONTH:$3.;
  monthnum=findw('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec',month,' ','e');
datalines4;
$107.00,CANADA,FURNITURE,SOFA,1994,Jan
$354.00,CANADA,FURNITURE,SOFA,1994,Feb
$101.00,CANADA,FURNITURE,OTTOMAN,1994,Mar
$553.00,CANADA,FURNITURE,SOFA,1994,Apr
$877.00,CANADA,FURNITURE,SOFA,1994,May
$431.00,CANADA,FURNITURE,SOFA,1994,Jun
$511.00,CANADA,FURNITURE,CHAIR,1994,Jul
$157.00,CANADA,FURNITURE,STOOL,1994,Aug
$520.00,CANADA,FURNITURE,SOFA,1994,Sep
$114.00,CANADA,FURNITURE,SOFA,1994,Oct
$277.00,CANADA,FURNITURE,SOFA,1994,Nov
$561.00,CANADA,FURNITURE,CUPBOARD,1994,Dec
$267.00,CANADA,FURNITURE,BED,1994,Jan
$347.00,CANADA,FURNITURE,BED,1994,Feb
$991.00,CANADA,FURNITURE,BED,1994,Mar
$923.00,CANADA,FURNITURE,BED,1994,Apr
$437.00,CANADA,FURNITURE,DESK,1994,May
$737.00,CANADA,FURNITURE,BED,1994,Jun
$104.00,CANADA,FURNITURE,BED,1994,Jul
$840.00,CANADA,FURNITURE,BED,1994,Aug
$704.00,CANADA,FURNITURE,BED,1994,Sep
$889.00,CANADA,FURNITURE,BED,1994,Oct
$107.00,CANADA,FURNITURE,LOVESEAT,1994,Nov
$571.00,CANADA,FURNITURE,BED,1994,Dec
$355.00,CANADA,FURNITURE,TABLE,1994,Jan
$506.00,CANADA,FURNITURE,TABLE,1994,Feb
$585.00,CANADA,FURNITURE,DRESSER,1994,Mar
$634.00,CANADA,FURNITURE,TABLE,1994,Apr
$662.00,CANADA,FURNITURE,TABLE,1994,May
$783.00,CANADA,FURNITURE,TABLE,1994,Jun
$786.00,CANADA,FURNITURE,TABLE,1994,Jul
$710.00,CANADA,FURNITURE,CHAIR,1994,Aug
$950.00,CANADA,FURNITURE,TABLE,1994,Sep
$274.00,CANADA,FURNITURE,SOFA,1994,Oct
$406.00,CANADA,FURNITURE,TABLE,1994,Nov
$515.00,CANADA,FURNITURE,TABLE,1994,Dec
$877.00,CANADA,FURNITURE,BED,1994,Jan
$845.00,CANADA,FURNITURE,CHAIR,1994,Feb
$425.00,CANADA,FURNITURE,CHAIR,1994,Mar
$899.00,CANADA,FURNITURE,BENCH,1994,Apr
$987.00,CANADA,FURNITURE,DESK,1994,May
$641.00,CANADA,FURNITURE,BED,1994,Jun
$448.00,CANADA,FURNITURE,CHAIR,1994,Jul
$341.00,CANADA,FURNITURE,CHAIR,1994,Aug
$137.00,CANADA,FURNITURE,CHAIR,1994,Sep
$235.00,CANADA,FURNITURE,CHAIR,1994,Oct
$482.00,CANADA,FURNITURE,CHAIR,1994,Nov
$678.00,CANADA,FURNITURE,CHAIR,1994,Dec
$942.00,CANADA,FURNITURE,DESK,1994,Jan
$912.00,CANADA,FURNITURE,DESK,1994,Feb
$768.00,CANADA,FURNITURE,CHAIR,1994,Mar
$951.00,CANADA,FURNITURE,DESK,1994,Apr
$768.00,CANADA,FURNITURE,NIGHTSTAND,1994,May
$978.00,CANADA,FURNITURE,DESK,1994,Jun
$20.00,CANADA,FURNITURE,DESK,1994,Jul
$298.00,CANADA,FURNITURE,DESK,1994,Aug
$193.00,CANADA,FURNITURE,TABLE,1994,Sep
$336.00,CANADA,FURNITURE,DESK,1994,Oct
$617.00,CANADA,FURNITURE,DESK,1994,Nov
$709.00,CANADA,FURNITURE,SOFA,1994,Dec
;;;;

proc sql;
create table cur_month as
  select  Country,prodtype,Month,monthnum,
          "Current Month" as Type,
          sum(actual) as Actual
  from book4
  group  by country,  prodtype, monthnum
  order  by country,  prodtype, monthnum;
quit;

proc sort data=book4 out=book4s  equals;
  by country prodtype monthnum;
run;

data prod_type_counts (keep=country prodtype monthnum prod_type);
  length cum_type_list $300;/*List of distinct products encountered YTD*/
  do until (last.prodtype);
    set book4s;
    by country prodtype monthnum;
	if findw(cum_type_list,trim(product),'','E')=0 then do;
       cum_type_list=catx(' ',cum_type_list,product);
       prod_type=sum(prod_type,1);
    end;
    if last.monthnum=1 then output;
  end;
run;

data want (drop=cum_: monthnum);
  do until (last.prodtype);
    set cur_month ;
    by country prodtype;
    output;
  end;

  do until (last.prodtype);
    merge cur_month prod_type_counts;
    by country prodtype monthnum;
    cum_actual+actual;
    type='YTD';
    actual=cum_actual;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2019 20:32:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605520#M175757</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-19T20:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605605#M175779</link>
      <description>&lt;P&gt;The&amp;nbsp;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: black; direction: ltr; font-family: Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace; font-size: 1em; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 1.5; -ms-hyphens: none; orphans: 2; overflow-wrap: normal; text-align: left; text-decoration: none; text-indent: 0px; text-shadow: 0px 1px white; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-break: normal; word-spacing: normal;"&gt;prod_type_counts&lt;/SPAN&gt; data step is exactly what I need. Thank you! However, the length of&amp;nbsp;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: black; direction: ltr; font-family: Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace; font-size: 1em; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 1.5; -ms-hyphens: none; orphans: 2; overflow-wrap: normal; text-align: left; text-decoration: none; text-indent: 0px; text-shadow: 0px 1px white; text-transform: none; -webkit-text-stroke-width: 0px; white-space: pre; word-break: normal; word-spacing: normal;"&gt;cum_type_list&lt;/SPAN&gt; in my actual data set can exceed 32767. And it is producing incorrect totals for length of cum_type_list that exceed 32767. Is there an alternative method?&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 02:30:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605605#M175779</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-20T02:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605610#M175780</link>
      <description>&lt;P&gt;YOu need more the $32767 to hold all the product values?&amp;nbsp; Then you can replace use of the CUM_TYPE_LIST variable with a hash object, which can be check for the presence of each product encountered, and can be dynamically grown by adding each each product's first encounter:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.BOOK4;
  infile datalines dsd truncover;
  input ACTUAL:DOLLAR20.2 COUNTRY:$6. PRODTYPE:$9. PRODUCT:$20. YEAR:F12. MONTH:$3.;
  monthnum=findw('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec',month,' ','e');
datalines4;
$107.00,CANADA,FURNITURE,SOFA,1994,Jan
$354.00,CANADA,FURNITURE,SOFA,1994,Feb
$101.00,CANADA,FURNITURE,OTTOMAN,1994,Mar
$553.00,CANADA,FURNITURE,SOFA,1994,Apr
$877.00,CANADA,FURNITURE,SOFA,1994,May
$431.00,CANADA,FURNITURE,SOFA,1994,Jun
$511.00,CANADA,FURNITURE,CHAIR,1994,Jul
$157.00,CANADA,FURNITURE,STOOL,1994,Aug
$520.00,CANADA,FURNITURE,SOFA,1994,Sep
$114.00,CANADA,FURNITURE,SOFA,1994,Oct
$277.00,CANADA,FURNITURE,SOFA,1994,Nov
$561.00,CANADA,FURNITURE,CUPBOARD,1994,Dec
$267.00,CANADA,FURNITURE,BED,1994,Jan
$347.00,CANADA,FURNITURE,BED,1994,Feb
$991.00,CANADA,FURNITURE,BED,1994,Mar
$923.00,CANADA,FURNITURE,BED,1994,Apr
$437.00,CANADA,FURNITURE,DESK,1994,May
$737.00,CANADA,FURNITURE,BED,1994,Jun
$104.00,CANADA,FURNITURE,BED,1994,Jul
$840.00,CANADA,FURNITURE,BED,1994,Aug
$704.00,CANADA,FURNITURE,BED,1994,Sep
$889.00,CANADA,FURNITURE,BED,1994,Oct
$107.00,CANADA,FURNITURE,LOVESEAT,1994,Nov
$571.00,CANADA,FURNITURE,BED,1994,Dec
$355.00,CANADA,FURNITURE,TABLE,1994,Jan
$506.00,CANADA,FURNITURE,TABLE,1994,Feb
$585.00,CANADA,FURNITURE,DRESSER,1994,Mar
$634.00,CANADA,FURNITURE,TABLE,1994,Apr
$662.00,CANADA,FURNITURE,TABLE,1994,May
$783.00,CANADA,FURNITURE,TABLE,1994,Jun
$786.00,CANADA,FURNITURE,TABLE,1994,Jul
$710.00,CANADA,FURNITURE,CHAIR,1994,Aug
$950.00,CANADA,FURNITURE,TABLE,1994,Sep
$274.00,CANADA,FURNITURE,SOFA,1994,Oct
$406.00,CANADA,FURNITURE,TABLE,1994,Nov
$515.00,CANADA,FURNITURE,TABLE,1994,Dec
$877.00,CANADA,FURNITURE,BED,1994,Jan
$845.00,CANADA,FURNITURE,CHAIR,1994,Feb
$425.00,CANADA,FURNITURE,CHAIR,1994,Mar
$899.00,CANADA,FURNITURE,BENCH,1994,Apr
$987.00,CANADA,FURNITURE,DESK,1994,May
$641.00,CANADA,FURNITURE,BED,1994,Jun
$448.00,CANADA,FURNITURE,CHAIR,1994,Jul
$341.00,CANADA,FURNITURE,CHAIR,1994,Aug
$137.00,CANADA,FURNITURE,CHAIR,1994,Sep
$235.00,CANADA,FURNITURE,CHAIR,1994,Oct
$482.00,CANADA,FURNITURE,CHAIR,1994,Nov
$678.00,CANADA,FURNITURE,CHAIR,1994,Dec
$942.00,CANADA,FURNITURE,DESK,1994,Jan
$912.00,CANADA,FURNITURE,DESK,1994,Feb
$768.00,CANADA,FURNITURE,CHAIR,1994,Mar
$951.00,CANADA,FURNITURE,DESK,1994,Apr
$768.00,CANADA,FURNITURE,NIGHTSTAND,1994,May
$978.00,CANADA,FURNITURE,DESK,1994,Jun
$20.00,CANADA,FURNITURE,DESK,1994,Jul
$298.00,CANADA,FURNITURE,DESK,1994,Aug
$193.00,CANADA,FURNITURE,TABLE,1994,Sep
$336.00,CANADA,FURNITURE,DESK,1994,Oct
$617.00,CANADA,FURNITURE,DESK,1994,Nov
$709.00,CANADA,FURNITURE,SOFA,1994,Dec
;;;;

proc sql;
create table cur_month as
  select  Country,prodtype,Month,monthnum,
          "Current Month" as Type,
          sum(actual) as Actual
  from book4
  group  by country,  prodtype, monthnum
  order  by country,  prodtype, monthnum;
quit;

proc sort data=book4 out=book4s  equals;
  by country prodtype monthnum;
run;

data prod_type_counts (keep=country prodtype monthnum prod_type);
  if 0 then set book4s;
  if _n_=1 then do;
    declare hash cum_prod_types();
	  cum_prod_types.definekey('product');
	  cum_prod_types.definedone();
  end;
  cum_prod_types.clear();
  do until (last.prodtype);
    set book4s;
    by country prodtype monthnum;
	if cum_prod_types.check()^=0 then do;
      cum_prod_types.add();
	  prod_type=sum(prod_type,1);
    end;
    if last.monthnum=1 then output;
  end;
run;

data want (drop=cum_: monthnum);
  do until (last.prodtype);
    set cur_month ;
    by country prodtype;
    output;
  end;

  do until (last.prodtype);
    merge cur_month prod_type_counts;
    by country prodtype monthnum;
    cum_actual+actual;
    type='YTD';
    actual=cum_actual;
    output;
  end;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I clear the hash object before starting each prodtype. So none of the cumulative counts cross PRODTYPEs.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 03:32:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605610#M175780</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-20T03:32:54Z</dc:date>
    </item>
    <item>
      <title>Re: Distinct Cumulative totals year to date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605744#M175810</link>
      <description>Thank you very much! This is exactly what I needed.</description>
      <pubDate>Wed, 20 Nov 2019 14:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Distinct-Cumulative-totals-year-to-date/m-p/605744#M175810</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2019-11-20T14:23:57Z</dc:date>
    </item>
  </channel>
</rss>

