Count of a text variable

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

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?

Thank you for your time.


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

View solution in original post


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: 7,363

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
Super User
Posts: 6,499

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,

Please find below the code for your problem:-

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:;-

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

Capture.JPG

/Daman

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 235 views
  • 6 likes
  • 5 in conversation