DATA Step, Macro, Functions and more

COUNT

Reply
Occasional Contributor
Posts: 14

COUNT

Hi,

 

Wondering if there is any function in SAS which is equivalent to the COUNT function in SPSS.

 

Thank you.

 

K

Super User
Posts: 17,829

Re: COUNT

Not specifically, AFAIK. It's actually a bit annoying, because this is a common question!

 

That being said, you can easily use an array or other methods to get counts. 

If you post sample data and what you expect as output and we can illustrate ways this can be accomplished. 

Occasional Contributor
Posts: 14

Re: COUNT

[ Edited ]

@Reeza I was trying to compute 'count_v' using v1-v3.

a.jpg

I followed LinLin's solution using array (https://communities.sas.com/t5/SAS-Procedures/Horizontal-count-of-number-of-variables-that-meet-crit...). 

 

Thank you.

K

Super User
Posts: 10,500

Re: COUNT

If the arguments are numeric then you want the N function.

 

v_count= n(v1,v2,v3);

or if you can use a list

v_count = n(of v1-v3);

or array defined as:

Array v v1-v3;

v_count= n(of v(*));

 

Super User
Posts: 17,829

Re: COUNT

@khalidamin Your post is still ambiguous. That could be the sum of the columns, or the count of 1s. The assumption that their all 0/1 isn't explicit. 

Valued Guide
Posts: 797

Re: COUNT

If the set of values you wish to count are all single characters, then you can use the COUNTC and CATS functions as here. 

 

data grades;
  input id 1. (grade_eng grade_hist grade_bio grade_math grade_civics) ($1.);
  npass=countc(cats(of grade_eng--grade_civics),'ABCD');
  put (_all_) (=);
datalines;
1ABCDE
2ABCEE
3ABEEE
4AEEEE
5EEEEE
run;

 

 

I think it would also work if you were counting a set of single digit numeric values in numeric vars:

 

   n_ones_and_twos =countc(cats(num1,num2,num3,num4,'12');

SAS Super FREQ
Posts: 3,477

Re: COUNT

For those of use who are not familiar with SPSS, what are you trying to accomplish?

PROC Star
Posts: 7,363

Re: COUNT

@Rick_SAS: an SPSS example: COUNT NREAD=MONDE,NZZ,TEMPS,FAZ,BZ,TAZ,MATIN(1,2,3). Counts all occurences of values 1,2,3 across all the specified variables

 

@khalidamin@mkeintz's solution works for both character and numeric, as well as arrays. Take a look at the following:

 

data grades;
  input id 1. (v1-v5) ($1.);
  npass=countc(cats(of v:),'1');
  datalines;
111110
211100
311000
410000
500000
run;

data grades2;
  input id v1-v5;
  npass=countc(cats(of v:),'1');
  datalines;
1 1 1 1 1 0
2 1 1 1 0 0
3 1 1 0 0 0
4 1 0 0 0 0
5 0 0 0 0 0
run;

data grades3;
  input id 1. (testa testb testc testd teste) ($1.);
  array v(*) $ testa--teste;
  npass=countc(cats(of v(*)),'1');
  datalines;
111110
211100
311000
410000
500000
run;

data grades4;
  input id testa testb testc testd teste;
  array v(*) testa--teste;
  npass=countc(cats(of v(*)),'1');
  datalines;
1 1 1 1 1 0
2 1 1 1 0 0
3 1 1 0 0 0
4 1 0 0 0 0
5 0 0 0 0 0
run;

HTH,

Art, CEO, AnalystFinder.com

 

Valued Guide
Posts: 797

Re: COUNT

And if you are trying to count multi-character values (or multidigit numerics), it can be emulated like this:

 

data authors;
  input (author1-author5) ($4. +1);
datalines;
mark art  ruth sam  pete
joe  rick jane john jean
pete art  rick elen jack
run;

data want;
  set authors;
  txt=catx('!',of author:,'!');
  do value='mark','art','jane';
    txt=tranwrd(txt,trim(value)||'!','1!');
  end;
  nauthor=countc(txt,'1');
  put (author:) (=)  / nauthor=;
run;

It can probably be made into a relatively useful macro spss_count such as this:

 

options mprint;
%macro spss_count(vars=,values=,countvar=)
  /des="replicate spss count function";
  %local 
     VARS     /*a variable list to be examined for desired values*/
     VALUES   /*a space-separated list of values to count*/
     COUNTVAR /*Name of variable to receive the count */
		;
  _txt=catx('!',&vars,'!');
  %do v=1 %to %sysfunc(countw(&values));
    %let value=%scan(&values,&v)!;
    _txt=tranwrd(_txt,trim("&value"),'1!');
  %end;
  &countvar=countc(_txt,'1');
  drop _txt;
%mend spss_count;


data want2;
  set have;
  %spss_count(vars=of author:,values=mark art jane,countvar=nauthors);
  put (author:) (=) / nauthors=;
run;

I leave it to those more facile with proc fcmp to make an efficient user-defined function.

Ask a Question
Discussion stats
  • 8 replies
  • 210 views
  • 0 likes
  • 6 in conversation