Identifying Subjects with “n” Observations Each

Using the grouping capability of PROC SQL and the COUNT function, you can list all patients that do not have exactly “n” visits or observations in a data set, just as you did in Programs 5-11 and 5-12. Here is the program with an explanation following.

Program 8-10. Using SQL to List Patients Who Do Not Have Two Visits
TITLE "Listing of Patients Who Do Not Have Two Visits";
PROC SQL;
   SELECT PATNO,
          VISIT
      FROM CLEAN.PATIENTS2
      GROUP BY PATNO
      HAVING COUNT(PATNO) NE 2;
QUIT;

By first grouping the observations by patient number, you can then use the COUNT function, which returns the number of observations in a group. Here is the output from Program 8-10.

 Listing of Patients Who Do Not Have Two Visits ...

Get Cody’s Data Cleaning Techniques Using SAS® Software now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.