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"
The COUNT function is intended for character strings. Perhaps you are thinking of the N function? (If not, what are you trying to accomplish?)
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?
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
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)
What if I want to count non missing numeric values withing the array?
Thank you for prompt response. I will start a new thread.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.