BookmarkSubscribeRSS Feed
TashaChapWUSS
Obsidian | Level 7

I have a dataset with the following variables:

 

  • Inspection ID
  • Date
  • Employer ID
  • Count of violations

There are five years worth of data, one row per inspection, and an employer can have multiple inspections.

 

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?

 

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.

 

Thoughts on how to do this?  SAS code or Proc SQL are preferred, but I'll work with anything you can come up with...  I was thinking maybe a correlated subquery, but my initial attempt at the code didn't work and I kinda broke my brain. 🙂

 

4 REPLIES 4
ballardw
Super User

If might help to show us your attempt at the subquery.

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.

 

For one thing were you using the INTCK or INTNX functions for interval determination?

TashaChapWUSS
Obsidian | Level 7

Here's my initial attempt.  I've worked with subqueries a lot, but not correlated subqueries before, so I'm stumped.

 



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;
TashaChapWUSS
Obsidian | Level 7
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.
s_lassen
Meteorite | Level 14

You can just make a subquery to get the one calculated column:

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;

(code not tested, as you did not provide any example data)

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 336 views
  • 1 like
  • 3 in conversation