Help using Base SAS procedures

issue with Count function

Accepted Solution Solved
Reply
Occasional Contributor dio
Occasional Contributor
Posts: 8
Accepted Solution

issue with Count function

Hi,

i have a problem with the Count function. I would like to specify the substring argument as an element of an array, since i have a list of words and I am looking at the occurences of the words in a list of text. However, it seems like the function fails and only counts when the word appears on the last position in the text. Do you have any idea regarding what I am doing wrong?

Thanks.

/*Example*/

DATA Example;

INPUT Texts $CHAR60.;

DATALINES;

The number of times "the" appears is the question

THE the

None on this line

There is the map

;

DATA Example;

set Example;

/* Load Words into a one-dimensional array*/

length words1-words3 $20;

array words{3} words1-words3;

retain words1-words3;

/* Load Occurences into a one-dimensional array*/

length occur1-occur3 6;

array occur{3} occur1-occur3;

retain occur1-occur3;

words1="the";

words2="number";

words3="line";

do i=1 to 3;

   occur{i}=count(texts,words{i},'i');

end;

/*Define validation variable that uses the typical format of the Count function*/

m=count(texts,"the",'i');

keep texts occur1 m;

run;


Accepted Solutions
Solution
‎02-28-2012 08:06 AM
Respected Advisor
Posts: 3,124

Re: issue with Count function

Hi,

Since you set the length $20 to your words, they are actually not what they appeared to be, instead, they are the characters+padded blanks all the way to 20.

In this case, you want to ignore blanks by adding modifier 't':

  occur{i}=count(texts,words{i},'ti');

And then everything falls back to their place.

BTW, I don't understand why you use length statement at all, since you assign individual values to your words one by one anyway.

Regards,

Haikuo

View solution in original post


All Replies
Solution
‎02-28-2012 08:06 AM
Respected Advisor
Posts: 3,124

Re: issue with Count function

Hi,

Since you set the length $20 to your words, they are actually not what they appeared to be, instead, they are the characters+padded blanks all the way to 20.

In this case, you want to ignore blanks by adding modifier 't':

  occur{i}=count(texts,words{i},'ti');

And then everything falls back to their place.

BTW, I don't understand why you use length statement at all, since you assign individual values to your words one by one anyway.

Regards,

Haikuo

Occasional Contributor dio
Occasional Contributor
Posts: 8

issue with Count function

Works like a charm, thanks!

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 147 views
  • 0 likes
  • 2 in conversation