BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PhillipSherlock
Obsidian | Level 7

Hello everyone,

 

I have several variables and I would like to count the number of variables for which each person has a value of 2.

 

My initial thought (below) was to use 'count' and 'in', but I'm missing something or way off the mark. 

 

count(in(var1, var2, var3)=2) as var_total

 

Thank you for your help,

Phil

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Provide example data.

 

IN isn't likely to be helpful because that will return a numeric 1 for "yes the value is in the list" or 0 for "no the value is not in the list". So comparing anything as IN (<list>) = 2 always returns 0 as the result of IN is never 2.

 

A simple COUNT function works with strings and substrings. So you would want to create a string of all the values before counting. HOWEVER if you are searching for "2" it would be found as part of a value of "12" or "21" or anything else that has a 2. So is it possible for 2 to be part of another valid value?

 

Counttwo = count(cats( of var1-varx),'2');

might work for you if you don't have any values like the "12" mentioned earlier appearing in the values.

This creates a character value converting var1 through varx (use your number if sequentially numbered variables or list them with commas separating the names) to one string and then counts the occurrences of 2.

If you have values of 12 and such that might appear then you will need to examine each one separately.

Something like:

data want;
   set have;
   array s{*}  var1 var2 var3;
   do i= 1 to dim(s);
      Count2 = sum(count2,(s[I]=2));
   end;
run;

Which assumes all of the variables you are searching are numeric. If they are character then use S[i]='2'.

SAS returns 1 for true, so you can use a comparison like s[i]=<value> and accumulate them as shown to get a total count.

 

If you have your values in mix of numeric and character values then you need to reevaluate why. A combination can be processed but you need two different arrays, one for the character and one for the numeric.

View solution in original post

2 REPLIES 2
ballardw
Super User

Provide example data.

 

IN isn't likely to be helpful because that will return a numeric 1 for "yes the value is in the list" or 0 for "no the value is not in the list". So comparing anything as IN (<list>) = 2 always returns 0 as the result of IN is never 2.

 

A simple COUNT function works with strings and substrings. So you would want to create a string of all the values before counting. HOWEVER if you are searching for "2" it would be found as part of a value of "12" or "21" or anything else that has a 2. So is it possible for 2 to be part of another valid value?

 

Counttwo = count(cats( of var1-varx),'2');

might work for you if you don't have any values like the "12" mentioned earlier appearing in the values.

This creates a character value converting var1 through varx (use your number if sequentially numbered variables or list them with commas separating the names) to one string and then counts the occurrences of 2.

If you have values of 12 and such that might appear then you will need to examine each one separately.

Something like:

data want;
   set have;
   array s{*}  var1 var2 var3;
   do i= 1 to dim(s);
      Count2 = sum(count2,(s[I]=2));
   end;
run;

Which assumes all of the variables you are searching are numeric. If they are character then use S[i]='2'.

SAS returns 1 for true, so you can use a comparison like s[i]=<value> and accumulate them as shown to get a total count.

 

If you have your values in mix of numeric and character values then you need to reevaluate why. A combination can be processed but you need two different arrays, one for the character and one for the numeric.

FreelanceReinh
Jade | Level 19

Hello @PhillipSherlock,

 

The risk of finding "2" as a substring of "12" etc. can be avoided by using delimiters (e.g. '#' or whatever does not occur in the variables to be concatenated).

 

Example:

data have;
input a b $ c;
cards;
2  2 30
3  2 22
5 12  7
2  2  2
;

data want;
set have;
var_total=count(catx('##','|',of a--c,'|'),'#2#');
run;

I've arbitrarily defined b as character to demonstrate that mixed variable types are no problem. The purpose of the added '|' characters is to get the delimiters around the first and last variable. Modify the variable list (a--c) as appropriate.

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1057 views
  • 0 likes
  • 3 in conversation