Counting number of record meet conditions

Reply
Super Contributor
Posts: 371

Counting number of record meet conditions

Hi Everyone,

I have following data with 3 variables.

I want to count the number of record that meet a condition say:

5<var1 and 4<var2 and 3<var3

5<var1 and 4<var2 and 6<var3

(always "<" less than)

in the general case, I want to count the number of record that meet each of the combination of condition  like:

do v1=5 to 20 by 5;

do v2=4 to 30 by 4;

do v3=0 to 30 by 6;

The output file should report the number of record meeting each combination.

(I would prefer the Do Loop method)

Please help me with that.

Thank you so much.

HHC

data have;

input var1 var2 var3 ;

datalines;

10 20 15

11 2 34

5 25 40

;

run;

Trusted Advisor
Posts: 1,204

Re: Counting number of record meet conditions

Something like this...

data want(drop=i);

set have;

count=0;

array v{3} var1-var3;

do i=1 to dim(v);

if 5<v{i} then count+1;

end;

if count=dim(v);

run;

Super Contributor
Posts: 371

Re: Counting number of record meet conditions

Oh, not sure why I choose these number, they are so missleading. I just update the info.

Thank you.

Trusted Advisor
Posts: 1,204

Re: Counting number of record meet conditions

data want(drop=i);

set have;

count=0;

array v{3} var1-var3;

do i=1 to 1;

if 5<v{i} and 4<v{i+1} and 6<v{i+2} then count+1;

end;

if count=dim(v);

run;

Super Contributor
Posts: 336

Re: Counting number of record meet conditions

If you would like to filter records according to one (?) condition and keep those records:

data have;
input var1 var2 var3 ;
datalines;
10 20 15
11 2 34
5 25 40
;
run;

Data Want (Keep=Var: Count);
  Do i=1 By 1 Until (Eof);
     Set Have End=Eof;
  If (Var1 ge 5 & Var2 ge 4 & Var3 ge 6) Then Count=Sum(Count,1);
  End;
  Do j=1 To i;
    Set Have;
If (Var1 ge 5 & Var2 ge 4 & Var3 ge 6) Then Output;
  End;
Run;

If you only need the total count, leave out the 2nd loop.

Super User
Super User
Posts: 7,401

Re: Counting number of record meet conditions

Well, I would assign a group to each valid so you can count:

data have;
  input var1 var2 var3 ;
datalines;
10 20 15
11 2 34
5 25 40
;
run;

/* This step generates another dataset with all the conditions outputting only rows which fulfill the criteria and assign a category to each */
data _null_;
  call execute('data want; set have; length condition $2000;');
  do i=5 to 20 by 5;
    do j=4 to 30 by 4;
      do k=0 to 30 by 6;
        call execute(' if '||strip(put(i,best.))||' < var1 and '||strip(put(j,best.))||' < var2 and '||strip(put(k,best.))||' < var3 then do;
                        condition="'||strip(put(i,best.))||' < var1 and '||strip(put(j,best.))||' < var2 and '||strip(put(k,best.))||'";
                        output;
                       end;');
      end;
    end;
  end;
  call execute('run;');
run;

/* Then count the categories */

proc sql;
  create table TOTALS as
  select  distinct
          CONDITION,
          COUNT(CONDITION) as RESULT
  from    WORK.WANT
  group by CONDITION;
quit;

Ask a Question
Discussion stats
  • 5 replies
  • 373 views
  • 0 likes
  • 4 in conversation