BookmarkSubscribeRSS Feed
khalidamin
Obsidian | Level 7

Hi,

 

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

 

Thank you.

 

K

8 REPLIES 8
Reeza
Super User

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. 

khalidamin
Obsidian | Level 7

@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

ballardw
Super User

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(*));

 

Reeza
Super User

@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. 

mkeintz
PROC Star

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');

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Rick_SAS
SAS Super FREQ

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

art297
Opal | Level 21

@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

 

mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1640 views
  • 0 likes
  • 6 in conversation