Obsidian | Level 7

## Count Values

How do I count the number of times the value A or T appears for each record. I want to enter the count into either column TSA_Count_A or TSA_Count_T as applicable?

 ID TSA_1 TSA_2 TSA_3 TSA_4 TSA_COUNT_ A TSA_COUNT_T 123 A T A T 456 T T N A 789 T N A T
1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Count Values

``````data have;
infile cards truncover;
input (ID	T1-T4) (\$);
cards;
123	A	T	A	T
456	T	T	N	A
789	T	N	A	T
;

data want;
set have;
TSA_COUNT_A=countc(cats(of t1-t4),'A');
TSA_COUNT_T=countc(cats(of t1-t4),'T');
run;``````
4 REPLIES 4
Tourmaline | Level 20

## Re: Count Values

``````data have;
infile cards truncover;
input (ID	T1-T4) (\$);
cards;
123	A	T	A	T
456	T	T	N	A
789	T	N	A	T
;

data want;
set have;
TSA_COUNT_A=countc(cats(of t1-t4),'A');
TSA_COUNT_T=countc(cats(of t1-t4),'T');
run;``````
Obsidian | Level 7

## Re: Count Values

Thank you!

I created an array and it worked.

ARRAY TSA_RESULT {4} TSA_1-TSA_4;

TSA_COUNT_A = COUNTC(CATS(OF TSA_1-TSA_4), 'A');

TSA_COUNT_T = COUNTC(CATS(OF TSA_1-TSA_4), 'T');

Super User

## Re: Count Values

@AP101 wrote:

Thank you!

I created an array and it worked.

ARRAY TSA_RESULT {4} TSA_1-TSA_4;

TSA_COUNT_A = COUNTC(CATS(OF TSA_1-TSA_4), 'A');

TSA_COUNT_T = COUNTC(CATS(OF TSA_1-TSA_4), 'T');

The only case I think were is would make a difference is if the input dataset had only some of the variables.

Then the ARRAY statement will insure that the added variables are of the same type as those that were present.

Obsidian | Level 7

## Re: Count Values

That's good to know. I thought I had to add it for the 1-4. I ran it without and still the same. Thanks!

Discussion stats
• 4 replies
• 885 views
• 3 likes
• 3 in conversation