BookmarkSubscribeRSS Feed
pablorodriguez1
Calcite | Level 5

Hi,

In a data step in which I have defined an array I can use the sum function, but the count function doesn't work. How can I count the number of values that are not zero within an array?

    SUM_ARRAY = sum(of A1-A20); - Works
    COUNT_ARRAY = count(of A1-A20);  Yields the following error: "The COUNT function call has too many arguments"

7 REPLIES 7
Astounding
PROC Star

The COUNT function is intended for character strings.  Perhaps you are thinking of the N function?  (If not, what are you trying to accomplish?)

ballardw
Super User

I don't even see the array mentioned in the question.

 

I would likely start with something like this:

array values a1-a20;
sum_array = sum(of values(*));
do i= 1 to dim(values);
   Count_array= sum(count_array,(values[i] ne 0));
end;

However this will count values that are missing as well. Is that the desired behavior?

 

rogerjdeangelis
Barite | Level 11
I don't think it is possible?

Here is what I consider minimal code:

data tst;
array values a1-a5 (10 0 23 15 43);
do over values;
 cnt + (values ne 0);
end;
run;quit;

Up to 40 obs WORK.TST total obs=1

                                      SUM_
Obs    A1    A2    A3    A4    A5    ARRAY    CNT

 1     10     0    23    15    43      91      4

snoopy369
Barite | Level 11

COUNT can be coerced to do this, if your data is agreeable. I'm not sure it's better than the loop operation timewise or structurally, but it's at least an interesting solution.

Basically we delimit the data by ; including starting and ending it with the delimiter, then count the number of ;0;, and subtract from the total.

data _null_;
  call streaminit(7);
  array a[20];
  do _i = 1 to 20;
    a[_i] = max(0,rand('Uniform')-0.3);
  end;
  put a[*]=;
  nonzero = dim(a) - count(';'||catx(';',of a[*])||';',';0;');
  put nonzero=;
run;

 

(This was cross-posted to/from Stack Overflow: http://stackoverflow.com/questions/43184156/sas-count-within-arrays)

Emet
Fluorite | Level 6

What if I want to count non missing numeric values withing the array?

PaigeMiller
Diamond | Level 26

@Emet wrote:

What if I want to count non missing numeric values withing the array?


@Emet 

It would really be much better if you started a new thread and described your problem completely, from the beginning. I would be happy to help you if you did this.

--
Paige Miller
Emet
Fluorite | Level 6

Thank you for prompt response. I will start a new thread.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 5917 views
  • 1 like
  • 7 in conversation