Solved
Contributor
Posts: 28

# Count of a text variable

Hi Friends,

I want help in counting a variable. I have list of firms against whom cases are registered. I want to count how many issues are involve in each  case. For instance, my subset of data looks as follows:

Firm     Date           Case ID      ISSUES

A          1/1/2001       111     Issue1|Issue2|Issue3

A          5/21/2005     123    xxx|yyy|zzz|dddddd

B          2/10/2004     119     aaaaa|ssss

ISSUE is in character form. Each issue is separated by a straight line "|". The issue can start with a capital letter, a number, or a small letter. In the above example, I am trying to get an output as follows:

Firm     Date           Case ID      ISSUES                     Count

A          1/1/2001       111     Issue1|Issue2|Issue3          3

A          5/21/2005     123    xxx|yyy|zzz|dddddd             4

B          2/10/2004     119     aaaaa|ssss                        2

In other words, I want to count number of straight lines + 1.Can someone please provide me with a code?

Accepted Solutions
Solution
‎02-25-2013 06:17 PM
Super Contributor
Posts: 644

## Re: Count of a text variable

You could try

count = 1 + Length (Compress (issues, '|' , 'k') ) ;

The compress function with the optional K (keep) modifier will remove all characters except |

Richard

All Replies
Solution
‎02-25-2013 06:17 PM
Super Contributor
Posts: 644

## Re: Count of a text variable

You could try

count = 1 + Length (Compress (issues, '|' , 'k') ) ;

The compress function with the optional K (keep) modifier will remove all characters except |

Richard

PROC Star
Posts: 8,163

## Re: Count of a text variable

Couldn't you just use the countc function?  see, e.g.: SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Super User
Posts: 8,078

## Re: Count of a text variable

Use the COUNTW function, telling it that | delineates the words.

count = countw(issues,'|');

Frequent Contributor
Posts: 81

## Re: Count of a text variable

Hi,

data new;

informat Date MMDDYY10.;

input Firm \$ Date Case_ID ISSUES \$30.;

FORMAT DATE date9. ;

datalines;

A 1/1/2001 111 Issue1|Issue2|Issue3

A 5/21/2005 123 xxx|yyy|zzz|dddddd

B 2/10/2004 119 aaaaa|ssss

;

RUN;

data new1;

set new;

count=countw(ISSUES,'|');

run;

Output:;-

==================

/Daman

🔒 This topic is solved and locked.