Obsidian | Level 7

## Counting with Condition #2

Dear SAS community,

Edit: My Apologies, I have amended the question.

Your help would be much appreciated.

Suppose that I have data below;

Subject / Year / Condition

A 1991 1

A 1992 1

A 1993 2

A 1994 2

A 1995 3

A 1996 1

A 1997 1

B 1991 2

B 1992 1

B 1993 2

B 1994 1

B 1995 3

B 1996 1

B 1997 2

I cannot change the order of this, and I would like to start counting

when "1" appears and continue counting without counting '2' or '3' so that I get

Subject / Year / Condition / Count

A 1991 1    1

A 1992 1    2

A 1993 2    2

A 1994 2    2

A 1995 3    2

A 1996 1    3

A 1997 1    4

B 1991 2    0

B 1992 1    1

B 1993 2    1

B 1994 1    2

B 1995 3    2

B 1996 1    3

B 1997 2    3

How can I achieve this?

Basically the end goal is to then find the number of each 1, 2, 3 and find the percentage of them to select which one I would choose based on the highest probability. But I will need to find a solution to this first.

Thank you!

Regards,

T

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Counting with Condition #2

The expected result seems to require resetting count when "subject" changes. Right? This requires sorted or at least properly grouped data. This is an extended version of @PeterClemmensen code:

``````data want;
set have;
by Subject notsorted;
if first.Subject then Count=0;
if Condition=1 then Count+1;
run;``````
3 REPLIES 3
Tourmaline | Level 20

## Re: Counting with Condition #2

Why do you not count the last 1?

You can do something like this

``````data have;
input Subject \$ Year Condition;
datalines;
A 1991 1
A 1992 1
A 1993 2
A 1994 2
A 1995 3
A 1996 1
A 1997 1
;

data want;
set have;
if Condition=1 then Count+1;
run;``````

## Re: Counting with Condition #2

The expected result seems to require resetting count when "subject" changes. Right? This requires sorted or at least properly grouped data. This is an extended version of @PeterClemmensen code:

``````data want;
set have;
by Subject notsorted;
if first.Subject then Count=0;
if Condition=1 then Count+1;
run;``````
Obsidian | Level 7

## Re: Counting with Condition #2

Thank you! Yes, this is correct. It was so simple and I was trying such complicated things. Have a good weekend!

Discussion stats
• 3 replies
• 498 views
• 0 likes
• 3 in conversation