Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting a number of observations according to critera 1

Status
Not open for further replies.

iren

Technical User
Mar 8, 2005
106
US
Hi everybody,

I wonder how can I count observations according to certain criteria?

I have fields (variables) Condition1 Condition2 Condition3 Condition4
And I have 60 observations (members)

I want to to count how many members have Condition4=”Y” and anyone of Condition 1-3 =”N”

If Condition4=”Y” AND
(Condition1=”N” OR
(Condition2=”N” OR
(Condition3=”N”) THEN
.....................
.....................
I have no idea how to count members however...

Could You please give me a hand?

Thank You!

Iren
 
Set a flag according to the different criteria, then use Proc Freq to tabulate the results.
i.e.
Code:
data dset2;
  set dset1;
  if Condition4=”Y” AND
    (Condition1=”N” OR
     Condition2=”N” OR
     Condition3=”N”) THEN condition_flag = 'A';
run;

proc freq data=dset2;
  table condition_flag /missing;
run;
This'll give you a table showing a count of how many matched your criteria. You can obviously also set up a different flag value for each possible combination of your conditions to count all of them, format the condition_flag variable to give a nice text explanation, and even use Proc Tabulate instead of Proc Freq to give nicer reports.
If you need to actually use the number elsewhere in the code, you can specify an "out=dset3" on the proc freq to write the results out to a dataset. Look up details on Proc freq for other options like norow, nopercent etc.

An alternative, simpler method is to use "retain" statement in your datastep to retain a counter, then increment it each time you match your criteria, then at the end of the dataset, that'll be your count.
Code:
* write out one record which contains the number of records meeting criteria *;
data dset2(keep=counter);
  set dset1 end=eof;

  retain counter 0;

  if Condition4=”Y” AND
    (Condition1=”N” OR
     Condition2=”N” OR
     Condition3=”N”) THEN counter + 1;

  if eof then output dset2;
run;

Also proc summary can be used to acheive something similar.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top