<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How many within a one year period... in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859287#M339509</link>
    <description>&lt;P&gt;I have a dataset with the following variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Inspection ID&lt;/LI&gt;
&lt;LI&gt;Date&lt;/LI&gt;
&lt;LI&gt;Employer ID&lt;/LI&gt;
&lt;LI&gt;Count of violations&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;There are&amp;nbsp;five years worth of data, one row per inspection, and an employer can have multiple inspections.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question is: How many times did the same employer receive three or more violations (not necessarily in the same inspection) within a one-year period?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In other words, for every inspection record in the original dataset, I also need to count back to any inspections within the previous year (12 months/365 days) of the date of that original inspection and count the sum total of violations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thoughts on how to do this?&amp;nbsp; SAS code or Proc SQL are preferred, but I'll work with anything you can come up with...&amp;nbsp; I was thinking maybe a correlated subquery, but my initial attempt at the code didn't work and I kinda broke my brain. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Feb 2023 23:24:59 GMT</pubDate>
    <dc:creator>TashaChapWUSS</dc:creator>
    <dc:date>2023-02-16T23:24:59Z</dc:date>
    <item>
      <title>How many within a one year period...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859287#M339509</link>
      <description>&lt;P&gt;I have a dataset with the following variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Inspection ID&lt;/LI&gt;
&lt;LI&gt;Date&lt;/LI&gt;
&lt;LI&gt;Employer ID&lt;/LI&gt;
&lt;LI&gt;Count of violations&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;There are&amp;nbsp;five years worth of data, one row per inspection, and an employer can have multiple inspections.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question is: How many times did the same employer receive three or more violations (not necessarily in the same inspection) within a one-year period?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In other words, for every inspection record in the original dataset, I also need to count back to any inspections within the previous year (12 months/365 days) of the date of that original inspection and count the sum total of violations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thoughts on how to do this?&amp;nbsp; SAS code or Proc SQL are preferred, but I'll work with anything you can come up with...&amp;nbsp; I was thinking maybe a correlated subquery, but my initial attempt at the code didn't work and I kinda broke my brain. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Feb 2023 23:24:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859287#M339509</guid>
      <dc:creator>TashaChapWUSS</dc:creator>
      <dc:date>2023-02-16T23:24:59Z</dc:date>
    </item>
    <item>
      <title>Re: How many within a one year period...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859294#M339513</link>
      <description>&lt;P&gt;If might help to show us your attempt at the subquery.&lt;/P&gt;
&lt;P&gt;At a minimum it will show variable names and data set name. It might be that your subquery was just in the wrong place or a condition was not quite matching your description.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For one thing were you using the INTCK or INTNX functions for interval determination?&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2023 00:08:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859294#M339513</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-17T00:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: How many within a one year period...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859296#M339514</link>
      <description>&lt;P&gt;Here's my initial attempt.&amp;nbsp; I've worked with subqueries a lot, but not correlated subqueries before, so I'm stumped.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

proc sql;
create table empcount as 
select c.inspection_no,
		c.open_date,
		c.employer_no,
        c.v_count,
		y.v_past
from cleanvios as c left join 
		(select employer_no, sum(v_count) as w_past
			from cleanvios as p
		where c.employer_no = p.employer_no and intnx('YEAR', c.open_date, 1, 'SAME') le p.open_date lt c.open_date
		group by employer_no) as y
		on c.employer_no = y.employer_no;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Feb 2023 00:22:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859296#M339514</guid>
      <dc:creator>TashaChapWUSS</dc:creator>
      <dc:date>2023-02-17T00:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: How many within a one year period...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859297#M339515</link>
      <description>From there I was going to add the V_Count and V_Past together.  But basically I just don't know how to do that look-back properly.</description>
      <pubDate>Fri, 17 Feb 2023 00:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859297#M339515</guid>
      <dc:creator>TashaChapWUSS</dc:creator>
      <dc:date>2023-02-17T00:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: How many within a one year period...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859323#M339523</link>
      <description>&lt;P&gt;You can just make a subquery to get the one calculated column:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table empcount as 
select c.inspection_no,
		c.open_date,
		c.employer_no,
        c.v_count,
		(select sum(v_count) from cleanvios as p
		  where p.employer_no=c.employer_no
		    and intnx('YEAR', c.open_date, 1, 'SAME') le p.open_date lt c.open_date)
		 as v_past
from cleanvios as c;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(code not tested, as you did not provide any example data)&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2023 07:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-within-a-one-year-period/m-p/859323#M339523</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-02-17T07:30:22Z</dc:date>
    </item>
  </channel>
</rss>

