Example 9.16 Counting the Occurrences of a String

Goal

Count the number of occurrences of a set of character strings in the value of a character variable.

Example Features

Featured StepDATA step
Featured Step Options and StatementsCOUNT, COUNTC, and COUNTW functions

Input Data Set

Data set QUIZ_GRADES has quiz grades for four students. The grades are stored in one variable, QUIZ_STRING, and the grades are separated by commas.

          QUIZ_GRADES

Obs    student      quiz_string
 1       431       B+,B-,A,I,A+
 2       501       A,A,B,P,C,U,F
 3       289       A,A,B,A,A-,U
 4       130       C+,B-,I,?,W

Resulting Data Set

Output 9.16 QUIZ_SUMMARY Data Set

 Example 9.16 QUIZ_SUMMARY Obs student quiz_string n_quizzes super good fair bad no_grades invalid 1 431 B+,B-,A,I,A+ 5 1 3 0 0 1 0 2 501 A,A,B,P,C,U,F ...

Get Combining and Modifying SAS® Data Sets: Examples Second Edition 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.