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

Dear SAS community,

 

Edit: My Apologies, I have amended the question.

 

This is yet another counting with the condition that I have been struggling with. Please help!

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
andreas_lds
Jade | Level 19

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;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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;
andreas_lds
Jade | Level 19

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;
tonoplast
Obsidian | Level 7

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