BookmarkSubscribeRSS Feed
hhchenfx
Rhodochrosite | Level 12

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;

5 REPLIES 5
stat_sas
Ammonite | Level 13

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;

hhchenfx
Rhodochrosite | Level 12

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

Thank you.

stat_sas
Ammonite | Level 13

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;

user24feb
Barite | Level 11

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3156 views
  • 0 likes
  • 4 in conversation