<?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: PROC SQL help to Group By Count of Records in increment of  Week, Month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585962#M167208</link>
    <description>&lt;P&gt;Hi. Any idea what the INTNX function syntax would look like for my example in Proc Sql? Say for the Week example.&amp;nbsp; I need to somehow use both INTNX and the COUNT function I think because in the end I need a dataset with three variables; ID, WEEK_NUM (or MONTH_NAME), and the actual Count.&lt;/P&gt;</description>
    <pubDate>Tue, 03 Sep 2019 22:26:01 GMT</pubDate>
    <dc:creator>buechler66</dc:creator>
    <dc:date>2019-09-03T22:26:01Z</dc:date>
    <item>
      <title>PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585959#M167206</link>
      <description>&lt;P&gt;Hi.&amp;nbsp; I have a dataset I'm reading in for testing purposes.&amp;nbsp; I hope you can help me with this problem.&amp;nbsp; It has three variables ID, FROM_DT, and CODE.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each ID I need to be able to count the number of records for a given increment of time using the FROM_DT variable (so by Week or Month for example) from between my start date (&amp;amp;BGN_DT) and my end date (&amp;amp;END_DT).&amp;nbsp;&amp;nbsp;A requirement is that I need to use Proc SQL for this.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using PROC SQL, is there a way to Group By ID and count the number of records by a particular time increment.&amp;nbsp; Again, like for Week (Sunday to Saturday for Weeks 1 thru 52) and/or for Month (Jan to Dec)?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help here would be greatly appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET BGN_DT='01JAN2019'D;
%LET BGN_DT='31JAN2019'D;

DATA HAVE;
INPUT ID $ FROM_DT : DATE9. CODE $;
FORMAT FROM_DT DATE9.;
DATALINES;
F23 01JAN2019 T234
F23 30JAN2019 1234
F23 21FEB2019 1234
F23 25FEB2019 Z234
F23 23FEB2019 X234
F23 18MAR2019 1234
F23 16MAR2019 1234
F23 21MAR2019 1234
F23 31MAR2019 1234
F23 21APR2019 1234
F23 17APR2019 1234
F23 29APR2019 1234
F23 13MAY2019 1234
F23 19MAY2019 1234
F23 04MAY2019 1234
F23 01JUN2019 T234
F23 30JUN2019 1234
F23 21AUG2019 1234
F23 25AUG2019 Z234
F23 23AUG2019 X234
F23 18SEP2019 1234
F23 16SEP2019 1234
F23 21SEP2019 1234
F23 30SEP2019 1234
F23 21NOV2019 1234
F23 17NOV2019 1234
F23 29NOV2019 1234
F23 13DEC2019 1234
F23 19DEC2019 1234
F23 04DEC2019 1234
S34 01JAN2019 A344
S34 30JAN2019 A344
S34 21FEB2019 2344
S34 25FEB2019 E344
S34 23FEB2019 2344
S34 18MAR2019 2344
S34 16MAR2019 B344
S34 21MAR2019 B344
S34 31MAR2019 B344
S34 21APR2019 2344
S34 17APR2019 2344
S34 29APR2019 C344
S34 13MAY2019 D344
S34 19MAY2019 2344
S34 04MAY2019 2344
S34 01JUN2019 A344
S34 30JUN2019 A344
S34 21JUL2019 2344
S34 25JUL2019 E344
S34 23JUL2019 2344
S34 18AUG2019 2344
S34 16AUG2019 B344
S34 21AUG2019 B344
S34 30AUG2019 B344
S34 21NOV2019 2344
S34 17NOV2019 2344
S34 29NOV2019 C344
S34 13DEC2019 D344
S34 19DEC2019 2344
S34 04DEC2019 2344
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 22:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585959#M167206</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-09-03T22:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585960#M167207</link>
      <description>&lt;P&gt;You could use the INTNX function in PROC SQL to count the number of weeks/months since &amp;amp;BGN_DT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also very easy to do in PROC FREQ or PROC SUMMARY&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 22:17:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585960#M167207</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-03T22:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585962#M167208</link>
      <description>&lt;P&gt;Hi. Any idea what the INTNX function syntax would look like for my example in Proc Sql? Say for the Week example.&amp;nbsp; I need to somehow use both INTNX and the COUNT function I think because in the end I need a dataset with three variables; ID, WEEK_NUM (or MONTH_NAME), and the actual Count.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 22:26:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585962#M167208</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-09-03T22:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585966#M167209</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;A requirement is that I need to use Proc SQL for this.&amp;nbsp;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;This seems like a silly requirement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;gt; is there a way to Group By ID and count the number of records&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
select ID, sum( FROM_DT between &amp;amp;BGN_DT and &amp;amp;END_DT) as count
from HAVE
group by ID                                         ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Sep 2019 22:24:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585966#M167209</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-09-03T22:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585967#M167210</link>
      <description>&lt;P&gt;I should have said the INTCK function. Plenty of examples here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1md4mx2crzfaqn14va8kt7qvfhr.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1md4mx2crzfaqn14va8kt7qvfhr.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 22:26:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585967#M167210</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-03T22:26:17Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585969#M167211</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;EM&gt;&amp;gt;A requirement is that I need to use Proc SQL for this.&amp;nbsp;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;This seems like a silly requirement&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Agreed.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 22:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585969#M167211</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-03T22:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585972#M167213</link>
      <description>Hmm, I would use the SUM() function?</description>
      <pubDate>Tue, 03 Sep 2019 22:27:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585972#M167213</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-09-03T22:27:29Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585973#M167214</link>
      <description>&lt;P&gt;Try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select 
    id,
    intnx("week", from_dt, 0) as week "Week starting on" format=yymmdd10.,
    count(*) as count
from have
where from_dt between &amp;amp;bgn_dt. and &amp;amp;end_dt.
group by id, calculated week;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;replace &lt;EM&gt;week&lt;/EM&gt; by &lt;EM&gt;month&lt;/EM&gt; for monthly counts.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 22:33:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585973#M167214</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-03T22:33:07Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585974#M167215</link>
      <description>Thank you so much for the example.  Now I can see exactly what you mean.  I appreciate your help and time so much!</description>
      <pubDate>Tue, 03 Sep 2019 22:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585974#M167215</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-09-03T22:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585976#M167216</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/79805"&gt;@buechler66&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Yes, there is. In fact, with SQL it's simpler than with other tools. For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input id :$3. from_dt :date. ;                                                                                                        
  format from_dt yymmdd10. ;                                                                                                            
  cards ;                                                                                                                               
f23 01jan2019                                                                                                                           
f23 30jan2019                                                                                                                           
f23 21feb2019                                                                                                                           
f23 25feb2019                                                                                                                           
f23 23feb2019                                                                                                                           
f23 18mar2019                                                                                                                           
f23 16mar2019                                                                                                                           
f23 21mar2019                                                                                                                           
f23 31mar2019                                                                                                                           
f23 21apr2019                                                                                                                           
f23 17apr2019                                                                                                                           
f23 29apr2019                                                                                                                           
f23 13may2019                                                                                                                           
f23 19may2019                                                                                                                           
f23 04may2019                                                                                                                           
f23 01jun2019                                                                                                                           
f23 30jun2019                                                                                                                           
f23 21aug2019                                                                                                                           
f23 25aug2019                                                                                                                           
f23 23aug2019                                                                                                                           
f23 18sep2019                                                                                                                           
f23 16sep2019                                                                                                                           
f23 21sep2019                                                                                                                           
f23 30sep2019                                                                                                                           
f23 21nov2019                                                                                                                           
f23 17nov2019                                                                                                                           
f23 29nov2019                                                                                                                           
f23 13dec2019                                                                                                                           
f23 19dec2019                                                                                                                           
f23 04dec2019                                                                                                                           
s34 01jan2019                                                                                                                           
s34 30jan2019                                                                                                                           
s34 21feb2019                                                                                                                           
s34 25feb2019                                                                                                                           
s34 23feb2019                                                                                                                           
s34 18mar2019                                                                                                                           
s34 16mar2019                                                                                                                           
s34 21mar2019                                                                                                                           
s34 31mar2019                                                                                                                           
s34 21apr2019                                                                                                                           
s34 17apr2019                                                                                                                           
s34 29apr2019                                                                                                                           
s34 13may2019                                                                                                                           
s34 19may2019                                                                                                                           
s34 04may2019                                                                                                                           
s34 01jun2019                                                                                                                           
s34 30jun2019                                                                                                                           
s34 21jul2019                                                                                                                           
s34 25jul2019                                                                                                                           
s34 23jul2019                                                                                                                           
s34 18aug2019                                                                                                                           
s34 16aug2019                                                                                                                           
s34 21aug2019                                                                                                                           
s34 30aug2019                                                                                                                           
s34 21nov2019                                                                                                                           
s34 17nov2019                                                                                                                           
s34 29nov2019                                                                                                                           
s34 13dec2019                                                                                                                           
s34 19dec2019                                                                                                                           
s34 04dec2019                                                                                                                           
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
%let bgn_dt='01jan2019'd ;                                                                                                              
%let dt_int = week ;                                                                                                                    
                                                                                                                                        
proc sql ;                                                                                                                              
  create table want as                                                                                                                  
  select id                                                                                                                             
       , 1 + intck ("&amp;amp;dt_int", &amp;amp;bgn_dt, from_dt) as &amp;amp;dt_int._number                                                                     
       , count (*) as count                                                                                                             
  from   have                                                                                                                           
  group  id, &amp;amp;dt_int._number                                                                                                            
  ;                                                                                                                                     
quit ;                                                            
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want a different date interval, for example, MONTH, just plug it into the macro parameter DT_INT instead of WEEK. Ditto for any other legitimate SAS date interval, such as QUARTER, SEMIYEAR, etc. Try then to see the effect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 22:49:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/585976#M167216</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-03T22:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL help to Group By Count of Records in increment of  Week, Month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/586146#M167295</link>
      <description>This code is also awesome. Love how you made it work for any incremental date value!</description>
      <pubDate>Wed, 04 Sep 2019 14:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-help-to-Group-By-Count-of-Records-in-increment-of-Week/m-p/586146#M167295</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-09-04T14:48:52Z</dc:date>
    </item>
  </channel>
</rss>

