Hello,
I have a data set with 6 variables: ID, IR00, IR10, IR20, IR30, IR40. ID is an 8 digit numeric ID. IR00 through IR40 are each continous variable from 0 to 1. I need to find the min and max for each ID across the IR00 to IR40 variables. The min has to be above 0.
I have found the max using:
data new;
set old;
MAX_IR = max(of IR00 - IR40) ;
output;
keep ID IR00 IR10 IR20 IR30 IR40 MAX_IR;
run;
But I cannot figure out how to find the min value above zero. Any help would be appreciated.
Can't think of anything "shorthand". Below should work.
data new;
set old;
array vars{*} IR00 - IR40;
do i=1 to dim(vars);
if vars[i]>0 then min_ir=min(min_ir,vars[i]);
end;
MAX_IR = max(of IR00 - IR40);
output;
keep ID IR00 IR10 IR20 IR30 IR40 MAX_IR min_ir;
run;
Try this. The SMALLEST function ignores missing values:
data new;
set old;
MAX_IR = max(of IR00 - IR40);
if IR00 = 0 then IR00 = .;
if IR10 = 0 then IR10 = .;
if IR20 = 0 then IR20 = .;
if IR30 = 0 then IR30 = .;
if IR40 = 0 then IR40 = .;
MIN_IR = smallest(of IR00 - IR40);
run;
@SASKiwi Also the min() function skips missings but I believe the real "challenge" is: "The min has to be above 0. "
if IR00 <= 0 then IR00 = .;
With SAS Arrays?
data old;
call streaminit(42);
do id = 1 to 1e2;
array ir IR00 IR10 IR20 IR30 IR40;
do over IR;
IR = rand("integer",0,9)/10;
end;
output;
end;
run;
data new;
set old;
array IR IR:;
do over IR;
MAX_IR = max(MAX_IR,IR);
if IR then MIN_IR = min(MIN_IR,IR);
end;
keep ID IR: M:;
run;
proc print;
run;
Bart
P.S. Just for fun
data new;
set old;
array IR IR:;
do over IR;
MAX_IR = MAX_IR<>IR;
MIN_IR = min(MIN_IR,IR+(0=IR));
end;
keep ID IR: M:;
run;
One more just for fun:
data new;
set old;
array IR IR:; /* make 0 to . */
do over IR;
IR = ifn(IR,IR,.);
end;
MAX_IR = max(of IR[*]);
MIN_IR = min(of IR[*]);
set old;
keep ID IR: M:;
run;
proc print;
run;
You may have to protect against all missing IR values or all zeroes. If so, then:
data want (drop=_:);
set have ;
if n(of IR:)>0 then max=max(of IR:);
if max=0 then max=.;
if max>0 then do _r=1 to n(of IR:) until(min>0);
min=smallest(_r,of IR:);
end;
run;
The "do ... UNTIL" using smallest is a way to avoid min=0.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.