Hi! I have question about counting specific values using array, I don't know how to do it, could anyone help me with it? Thank you!
I have lots of variables like below, and I wanted to do two things, first is to identify if each observation has the specific values that I am looking for, I used below array codes by using 'find=0', and if SAS find ''a', 'c', or 'e' in the variables, then 'find=1'.
data one;
input X1 $ X2 $ .. ....... X30 $;
cards;
a b .......ef
c d ..... dg
d c . .... rg
;
find=0;
array X X1-X30;
do over X;
if X in ('a','c','e') then find=1;
end;
run;
Now I have a question about the second thing that I want to do, I need to know how many specific values in each observation. Like above code, I want to know how many 'a', 'c', and 'e' in total in each row for all 30X variables. Is there any way to achieve that using array or something else? Thank you!
When you say " if each observation has the specific values that I am looking for" does that mean any of the variables hold any of the values? Some combination of variables holds all of the values? Some combination of variable has some minimum subset of the values?
What do you expect the output to look like? I'm not sure what the result should look like.
Also, are your values actually just 'a' 'c' 'e' or are you substituting short values in place of your actual ones. This is important for solutions because there is a function COUNTC that counts occurrences of single characters in a string which makes part of this pretty easy if the question is single character values but won't work if these are short hand.
Untested as complete example data not provided
data want; set have; array x(*) x1-x30; /*if you need separate counts of a c e*/ num_a = countc(cats(of x(*)),'a'); num_c = countc(cats(of x(*)),'c'); num_e = countc(cats(of x(*)),'e'); Num_ace= sum(num_a,num_c,num_e); /* if only need total count of a c e*/ Num_ace2= countc(cats(of x(*)),'ace'); /* if using this then the DO loop isn't needed*/ find= (num_ace > 0); run;
The CATS function combines all of the present values of the X variables into one long string to search, the Countc finds the number of times the character appears in the combined string.
The find= at the end uses SAS behavior of a comparison result returns 1 for true and 0 for false.
You can get the count of "a", "c" or "e" by using the compress function.
data one; input X1 $ X2 $ X3 $ X4 $ X5 $; cards; a b c d e f c d e h t f d c h i h e a r c e t j m h b v f p f j e g a c ; data two; set one; array X(*) X1-X5; do i=1 to dim(x); if X(i) in ('a','c','e') then find=1; end; words_len = length(compress(cat(of x:),compress(cat(of x:),'^ace'))); run;
Simply add a count:
find = 0;
count = 0;
array X X1-X30;
do over X;
if X in ('a','c','e')
then do;
find = 1;
count + 1;
end;
end;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.