BookmarkSubscribeRSS Feed
Fluorite | Level 6



I am interested in counting the average number of contacts per episode (also the number of episodes) based on the patientid and program_code by activation year. An episode is a period between the activation date and the deactivation date. 

Date format as DD/MM/YYYY

Diamond | Level 26

Can there be a contact date that is not in an episode (not between activation_date and deactivation_date)? For ID2, how can the deactivation_date be before the activation_date?


Please provide data as working SAS data step code (instructions) and not as Excel files or screen captures.

Paige Miller
Fluorite | Level 6

Thank you for looking into this. I have corrected the episode date for patient ID2.  Apologies - I don't have SAS on my personal computer and so can't provide the data as a working SAS data step code now. 

Diamond | Level 26

You didn't answer my first question: "Can there be a contact date that is not in an episode (not between activation_date and deactivation_date)?"


I will wait until you can provide the data in the requested format.

Paige Miller
Fluorite | Level 6
No it can’t. The contact date need to be between the activation date and deactivation date.
Diamond | Level 26

@Sofia2022 wrote:
No it can’t. The contact date need to be between the activation date and deactivation date.

Then isn't ID2 in violation of that rule?

Paige Miller
Fluorite | Level 6
Not now, as I have corrected the deactivation date. The contact dates for ID2 is now equal or between the activation and deactivation date.
Diamond | Level 26

As a side note ... we all get confused by dates of the form 02/03/2022, is that March 2 or February 3? Better is to use unambiguous formatting of dates such as 03FEB2022.

Paige Miller
Fluorite | Level 6
March 2. Date format as DD/MM/YYYY
Meteorite | Level 14

As @PaigeMiller remarked, you should provide example data as a data step, e.g.:


data have;
  length PatientID $3 Contact_date Program_code activation_date Deactivation_date 8;
  informat Contact_date activation_date Deactivation_date ddmmyy.;
  format Contact_date activation_date Deactivation_date ddmmyy.;
  input PatientID Contact_date Program_code activation_date Deactivation_date;
ID1 12/1/22 1 12/1/22 12/6/22
ID1 12/2/22 1 12/1/22 12/6/22
ID1 12/3/22 1 12/1/22 12/6/22
ID1 12/6/22 1 12/1/22 12/6/22
ID1 12/1/22 2 12/1/22 12/6/22
ID1 12/2/22 2 12/1/22 12/6/22
ID1 12/3/22 2 12/1/22 12/6/22
ID1 12/6/22 2 12/1/22 12/6/22
ID2 12/2/22 1 1/2/22 2/3/22
ID2 1/2/22 1 1/2/22 2/3/22
ID3 3/3/21 3 1/3/21 18/6/21
ID3 16/6/21 3 1/3/21 18/6/21



I would start by getting the counts for each patient and program_code. SQL can be used here:


proc sql;
  create table counts as select
    PatientID,Program_code,count(*) as count
  from have
  where Contact_date between activation_date and Deactivation_date
  group by PatientID,Program_code;

Then you can use PROC SUMMARY to get the average number of contacts:



proc summary data=counts;
  class PatientID;
  var count;
  output out=averages mean=avg_count;

This will (as we did not use the NWAY option) create a table like this:



The first row (with missing PatientID and _TYPE_=0) is the grand total, the average for all patients. The _FREQ_ variable shows the number of input rows used to calculate the average. The next rows are for each patient. If you need the averages for the Program_code and not the PatientID, use that variable in the CLASS statement instead, and if you just need the grand total, drop the CLASS statement.


Of course, PROC SUMMARY could also have been used to create the first table as well. And PROC SQL can also calculate averages, but not on different levels at once (the _TYPE_).


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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 3 in conversation