<?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: Calculating Monthly YTD for the number of unique customers with Proc CAS -How? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939387#M368835</link>
    <description>&lt;P&gt;If YTD is what is being asked for, no looping needed, no macro needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Enterprise-Guide/Last-month-and-YTD-data/m-p/829569#M41275" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Enterprise-Guide/Last-month-and-YTD-data/m-p/829569#M41275&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In that thread, the input data was three letter month names (Jan, Feb, &lt;EM&gt;etc&lt;/EM&gt;.) so that's what the code uses. Its even simpler if you have actual SAS date or date/time values.&lt;/P&gt;</description>
    <pubDate>Thu, 15 Aug 2024 11:01:45 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-08-15T11:01:45Z</dc:date>
    <item>
      <title>Calculating Monthly YTD for the number of unique customers with Proc CAS -How?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939331#M368815</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;This is my first post (I think?) asking for help in the forum.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a situation where I need to calculate a monthly year-to-date (YTD) distinct count of the number of customers benefiting from a particular insurance. As an example, for January 2023, the number should simply be the number of distinct customers that benefited during that month. For February 2023, it should be the number of distinct customers that benefited from the isurance at least once durin the period 2023-01-01 to 2023-02-28 and so on, all the way to December. This needs to be done for three years and is a repeated process.&lt;/P&gt;&lt;P&gt;Currently, the organization I am working for uses a PROC SQL Macro loop to achieve this and to calculate rolling 12-month values of the same statistic. However, this process is very slow, sometimes taking upwards of 2 hours with the data we have. When not working with values that need to be distinct, I have gradually transitioned to using a multilevel format and PROC SUMMARY to achieve this instead. (Up until recently, PROC EXPAND has not been an option.) However, when distinct counts are required, this method does not work.&lt;/P&gt;&lt;P&gt;In order to try and solve this problem, I decided to explore PROC CAS and the aggregation.aggregate action, as it is capable of doing distinct calculations. Luckily, I think I have figured out how to do the rolling twelve-month calculations that way, but I do not yet grasp the procedure well enough to do YTD. Does anyone have any ideas on how PROC CAS could be leveraged to achieve this?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;I will provde the code i used to calculate R12 as soon as I can access it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAMPLE DATA (abridged):&lt;BR /&gt;Variable names and datat structure are acureate, Content is nonsense.&lt;BR /&gt;A lagers saple dataset is attaced as a csv.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA WORK.TESTDATA_CAS;
    LENGTH
        ID               $ 3
        DELFORMAN        $ 12
        AVDELNING        $ 6
        KON                8
        DATE               8 ;
    LABEL
        AVDELNING        = "AVDELNING"
        DATE             = "DATUM" ;
    FORMAT
        ID               $CHAR3.
        DELFORMAN        $CHAR12.
        AVDELNING        $CHAR6.
        KON              BEST12.
        DATE             DATE9. ;
    INFORMAT
        ID               $CHAR3.
        DELFORMAN        $CHAR12.
        AVDELNING        $CHAR6.
        KON              BEST12.
        DATE             DATE9. ;
    INFILE DATALINES4
        DLM='7F'x
        MISSOVER
        DSD ;
    INPUT
        ID               : $CHAR3.
        DELFORMAN        : $CHAR12.
        AVDELNING        : $CHAR6.
        KON              : BEST32.
        DATE             : BEST32. ;
DATALINES4;
F68&amp;#127;Mint&amp;#127;Sweet&amp;#127;2&amp;#127;22763
F51&amp;#127;Cookie Dough&amp;#127;Sour&amp;#127;2&amp;#127;22840
F97&amp;#127;Strawberry&amp;#127;Bitter&amp;#127;2&amp;#127;21974
F23&amp;#127;Cookie Dough&amp;#127;Bitter&amp;#127;1&amp;#127;23335
F73&amp;#127;Cookie Dough&amp;#127;Sour&amp;#127;2&amp;#127;22171
F73&amp;#127;Chocolate&amp;#127;Sweet&amp;#127;1&amp;#127;22491
F0&amp;#127;Strawberry&amp;#127;Salty&amp;#127;1&amp;#127;22153
F47&amp;#127;Strawberry&amp;#127;Sweet&amp;#127;2&amp;#127;23739
F38&amp;#127;Strawberry&amp;#127;Sweet&amp;#127;1&amp;#127;21952
F83&amp;#127;Cookie Dough&amp;#127;Salty&amp;#127;2&amp;#127;21939
F31&amp;#127;Mint&amp;#127;Sour&amp;#127;1&amp;#127;22324
F90&amp;#127;Strawberry&amp;#127;Bitter&amp;#127;2&amp;#127;22375
F38&amp;#127;Pistachio&amp;#127;Sour&amp;#127;2&amp;#127;23509
F30&amp;#127;Cookie Dough&amp;#127;Sour&amp;#127;1&amp;#127;22902
F72&amp;#127;Chocolate&amp;#127;Sweet&amp;#127;2&amp;#127;23355
F80&amp;#127;Mint&amp;#127;Bitter&amp;#127;2&amp;#127;22271
F65&amp;#127;Pistachio&amp;#127;Bitter&amp;#127;2&amp;#127;22297
F61&amp;#127;Pistachio&amp;#127;Sour&amp;#127;2&amp;#127;23096
F83&amp;#127;Chocolate&amp;#127;Salty&amp;#127;1&amp;#127;22483
F60&amp;#127;Mint&amp;#127;Sour&amp;#127;1&amp;#127;23371
F66&amp;#127;Cookie Dough&amp;#127;Bitter&amp;#127;1&amp;#127;22770
F90&amp;#127;Pistachio&amp;#127;Bitter&amp;#127;2&amp;#127;22793
F82&amp;#127;Cookie Dough&amp;#127;Sour&amp;#127;1&amp;#127;23460
F19&amp;#127;Pistachio&amp;#127;Bitter&amp;#127;2&amp;#127;22837
F20&amp;#127;Chocolate&amp;#127;Salty&amp;#127;1&amp;#127;21926
F27&amp;#127;Chocolate&amp;#127;Bitter&amp;#127;2&amp;#127;23201
F3&amp;#127;Vanilla&amp;#127;Salty&amp;#127;2&amp;#127;23595
F57&amp;#127;Cookie Dough&amp;#127;Salty&amp;#127;2&amp;#127;22363
F7&amp;#127;Strawberry&amp;#127;Sour&amp;#127;2&amp;#127;23130
F87&amp;#127;Cookie Dough&amp;#127;Sweet&amp;#127;2&amp;#127;22676
F78&amp;#127;Strawberry&amp;#127;Sweet&amp;#127;1&amp;#127;23052
F79&amp;#127;Mint&amp;#127;Sour&amp;#127;1&amp;#127;21940
F45&amp;#127;Cookie Dough&amp;#127;Bitter&amp;#127;2&amp;#127;23721
F10&amp;#127;Mint&amp;#127;Sour&amp;#127;1&amp;#127;23437
F48&amp;#127;Chocolate&amp;#127;Salty&amp;#127;1&amp;#127;22422
F98&amp;#127;Chocolate&amp;#127;Sweet&amp;#127;1&amp;#127;23483
F72&amp;#127;Vanilla&amp;#127;Sour&amp;#127;1&amp;#127;21976
F49&amp;#127;Cookie Dough&amp;#127;Bitter&amp;#127;1&amp;#127;22143
F99&amp;#127;Mint&amp;#127;Sour&amp;#127;1&amp;#127;22521
F26&amp;#127;Strawberry&amp;#127;Sweet&amp;#127;1&amp;#127;22973
F83&amp;#127;Chocolate&amp;#127;Sour&amp;#127;2&amp;#127;23643
F44&amp;#127;Chocolate&amp;#127;Sour&amp;#127;1&amp;#127;23394
F96&amp;#127;Strawberry&amp;#127;Sweet&amp;#127;1&amp;#127;23526
F96&amp;#127;Mint&amp;#127;Salty&amp;#127;1&amp;#127;22805
F56&amp;#127;Chocolate&amp;#127;Sour&amp;#127;1&amp;#127;23204
F35&amp;#127;Chocolate&amp;#127;Bitter&amp;#127;2&amp;#127;22243
F36&amp;#127;Strawberry&amp;#127;Salty&amp;#127;2&amp;#127;23101
F64&amp;#127;Vanilla&amp;#127;Bitter&amp;#127;1&amp;#127;22352
F7&amp;#127;Mint&amp;#127;Sweet&amp;#127;2&amp;#127;23281
F53&amp;#127;Vanilla&amp;#127;Sweet&amp;#127;1&amp;#127;23305
F97&amp;#127;Vanilla&amp;#127;Sour&amp;#127;2&amp;#127;23403
F72&amp;#127;Vanilla&amp;#127;Bitter&amp;#127;1&amp;#127;23688
F30&amp;#127;Chocolate&amp;#127;Salty&amp;#127;1&amp;#127;23137
F8&amp;#127;Cookie Dough&amp;#127;Sour&amp;#127;1&amp;#127;23438
F56&amp;#127;Chocolate&amp;#127;Bitter&amp;#127;1&amp;#127;21930
F49&amp;#127;Chocolate&amp;#127;Sour&amp;#127;2&amp;#127;23234
F53&amp;#127;Pistachio&amp;#127;Salty&amp;#127;1&amp;#127;22782
F67&amp;#127;Pistachio&amp;#127;Sour&amp;#127;2&amp;#127;22425
F53&amp;#127;Vanilla&amp;#127;Sour&amp;#127;1&amp;#127;23726
F26&amp;#127;Chocolate&amp;#127;Sour&amp;#127;2&amp;#127;22898
F45&amp;#127;Vanilla&amp;#127;Bitter&amp;#127;1&amp;#127;23177
F45&amp;#127;Pistachio&amp;#127;Salty&amp;#127;2&amp;#127;22495
F38&amp;#127;Cookie Dough&amp;#127;Sweet&amp;#127;2&amp;#127;23239
F30&amp;#127;Mint&amp;#127;Bitter&amp;#127;1&amp;#127;21920
F33&amp;#127;Vanilla&amp;#127;Sweet&amp;#127;1&amp;#127;23158
F23&amp;#127;Cookie Dough&amp;#127;Sweet&amp;#127;1&amp;#127;22933
F7&amp;#127;Mint&amp;#127;Bitter&amp;#127;2&amp;#127;23704
F8&amp;#127;Strawberry&amp;#127;Bitter&amp;#127;2&amp;#127;23391
F46&amp;#127;Vanilla&amp;#127;Salty&amp;#127;2&amp;#127;23371
F0&amp;#127;Vanilla&amp;#127;Salty&amp;#127;2&amp;#127;22721
F62&amp;#127;Strawberry&amp;#127;Sour&amp;#127;1&amp;#127;23324
F64&amp;#127;Strawberry&amp;#127;Sweet&amp;#127;1&amp;#127;22533
F6&amp;#127;Vanilla&amp;#127;Salty&amp;#127;1&amp;#127;23233
F8&amp;#127;Strawberry&amp;#127;Sour&amp;#127;1&amp;#127;23056
F75&amp;#127;Chocolate&amp;#127;Salty&amp;#127;2&amp;#127;22319
F41&amp;#127;Mint&amp;#127;Bitter&amp;#127;2&amp;#127;23618
F16&amp;#127;Mint&amp;#127;Salty&amp;#127;1&amp;#127;22911
F68&amp;#127;Mint&amp;#127;Sweet&amp;#127;2&amp;#127;23701
F37&amp;#127;Chocolate&amp;#127;Salty&amp;#127;1&amp;#127;22508
F56&amp;#127;Strawberry&amp;#127;Sweet&amp;#127;2&amp;#127;22480
F59&amp;#127;Cookie Dough&amp;#127;Bitter&amp;#127;2&amp;#127;22666
F91&amp;#127;Strawberry&amp;#127;Sweet&amp;#127;2&amp;#127;21926
F33&amp;#127;Vanilla&amp;#127;Salty&amp;#127;2&amp;#127;23147
F78&amp;#127;Pistachio&amp;#127;Sour&amp;#127;2&amp;#127;22079
F25&amp;#127;Mint&amp;#127;Bitter&amp;#127;2&amp;#127;22792
F53&amp;#127;Strawberry&amp;#127;Salty&amp;#127;1&amp;#127;23232
F97&amp;#127;Cookie Dough&amp;#127;Bitter&amp;#127;2&amp;#127;23457
F13&amp;#127;Vanilla&amp;#127;Bitter&amp;#127;1&amp;#127;21992
F15&amp;#127;Chocolate&amp;#127;Salty&amp;#127;1&amp;#127;23372
F27&amp;#127;Chocolate&amp;#127;Salty&amp;#127;1&amp;#127;23515
F5&amp;#127;Cookie Dough&amp;#127;Sour&amp;#127;1&amp;#127;22528
F87&amp;#127;Vanilla&amp;#127;Bitter&amp;#127;1&amp;#127;22783
F94&amp;#127;Mint&amp;#127;Bitter&amp;#127;1&amp;#127;22726
F63&amp;#127;Strawberry&amp;#127;Salty&amp;#127;2&amp;#127;23737
F82&amp;#127;Vanilla&amp;#127;Sweet&amp;#127;2&amp;#127;23621
F34&amp;#127;Chocolate&amp;#127;Salty&amp;#127;2&amp;#127;23015
F32&amp;#127;Cookie Dough&amp;#127;Bitter&amp;#127;1&amp;#127;22986
F87&amp;#127;Cookie Dough&amp;#127;Salty&amp;#127;1&amp;#127;22681
F62&amp;#127;Strawberry&amp;#127;Sour&amp;#127;1&amp;#127;21996
F3&amp;#127;Cookie Dough&amp;#127;Sweet&amp;#127;2&amp;#127;23659
F89&amp;#127;Cookie Dough&amp;#127;Salty&amp;#127;2&amp;#127;22123
F93&amp;#127;Vanilla&amp;#127;Sour&amp;#127;2&amp;#127;23383
F25&amp;#127;Mint&amp;#127;Bitter&amp;#127;1&amp;#127;23534
F25&amp;#127;Strawberry&amp;#127;Salty&amp;#127;1&amp;#127;22436
F76&amp;#127;Vanilla&amp;#127;Bitter&amp;#127;1&amp;#127;22967
F25&amp;#127;Mint&amp;#127;Sour&amp;#127;2&amp;#127;23242
F0&amp;#127;Vanilla&amp;#127;Bitter&amp;#127;1&amp;#127;22908
F38&amp;#127;Pistachio&amp;#127;Sour&amp;#127;1&amp;#127;22379
F37&amp;#127;Vanilla&amp;#127;Sour&amp;#127;1&amp;#127;23440
;;;;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Thank you in advance.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2024 21:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939331#M368815</guid>
      <dc:creator>Henrik_P</dc:creator>
      <dc:date>2024-08-14T21:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly YTD for the number of unique customers with Proc CAS -How?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939335#M368818</link>
      <description>&lt;P&gt;You problem description includes "benefiting from a particular insurance."&amp;nbsp; Which variable in the example data holds the values of insurance? Which particular value are we looking for in the example data? Or is this actually a "for each type of insurance"?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you keeping the results from a previous month and adding to that or redoing the whole process from scratch?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2024 22:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939335#M368818</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-08-14T22:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly YTD for the number of unique customers with Proc CAS -How?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939361#M368825</link>
      <description>&lt;P&gt;At one point you state the objective is to "&lt;SPAN&gt;calculate rolling 12-month values of the same statistic.", but you started as stating you want YTD cumulative unique ID counts for each calendar year, i.e. not a rolling 12-month value.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;But either way, why bother with proc sql in a loop?&amp;nbsp; In the YTD case, you can use PROC SQL a single time to create a view with the earliest month in each calendar year for each ID, for every calendar year.&amp;nbsp; Then a PROC FREQ followed by BY YEAR will generate monthly cumulative counts of ID's for each year.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create view vneed as
  select year(date) as year, id, min(month(date)) as earliest_month
  from testdata_cas 
  group by calculated year,id;
quit;
proc freq data=vneed;
  by year;
  table earliest_month;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course you might need a where clause in the proc sql to select a "particular insurance".&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit note:&amp;nbsp; If the cumulative freq column of proc freq doesn't satisfy, skip the proc freq and run a DATA step to produce cumulative counts for each year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data counts (keep=year month newid_count cumulative_count);
  set vneed;
  by year;
  array counts {12} _temporary_;
  if first.year then call missing(of counts{*});
  counts{earliest_month}+1;
  if last.year;
  length month newid_count 8;
  call missing(cumulative_count);
  do month=1 to 12;
    newid_count=coalesce(counts{month},0);
    cumulative_count+newid_count;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s note on using the MLF feature in the proc summary class statement suggests this improvement to my suggestion:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value fmnth  (multilabel) 
    1   ='Jan'       1-2 ='Jan-Feb'   1-3 ='Jan-Mar'   
    1-4 ='Jan-Apr'   1-5 ='Jan-May'   1-6 ='Jan-Jun'
    1-7 ='Jan-Jul'   1-8 ='Jan-Aug'   1-9 ='Jan-Sep'
    1-10='Jan-Oct'   1-11='Jan-Nov'   1-12='Jan-Dec'
    ;
run;

proc summary data=vneed nway;
  class year;
  class earliest_month/mlf;
  output out=want (drop=_type_ rename=(_freq_=cum_freq));
  format earliest_month fmnth.;
run;
proc sort;
  by year cum_freq;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2024 04:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939361#M368825</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-08-16T04:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly YTD for the number of unique customers with Proc CAS -How?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939387#M368835</link>
      <description>&lt;P&gt;If YTD is what is being asked for, no looping needed, no macro needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Enterprise-Guide/Last-month-and-YTD-data/m-p/829569#M41275" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Enterprise-Guide/Last-month-and-YTD-data/m-p/829569#M41275&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In that thread, the input data was three letter month names (Jan, Feb, &lt;EM&gt;etc&lt;/EM&gt;.) so that's what the code uses. Its even simpler if you have actual SAS date or date/time values.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 11:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Monthly-YTD-for-the-number-of-unique-customers-with/m-p/939387#M368835</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-08-15T11:01:45Z</dc:date>
    </item>
  </channel>
</rss>

