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