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;
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;
Oh, not sure why I choose these number, they are so missleading. I just update the info.
Thank you.
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;
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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.