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

 

 

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
novinosrin
Tourmaline | Level 20
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;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20
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;
AP101
Obsidian | Level 7

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

Tom
Super User Tom
Super User

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


Adding the array statement shouldn't have made any difference.

 

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.

AP101
Obsidian | Level 7

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!

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
  • 4 replies
  • 1029 views
  • 3 likes
  • 3 in conversation