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: Call for Content

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!

Submit your idea!

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