BookmarkSubscribeRSS Feed
jbird
Calcite | Level 5

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. 

 

6 REPLIES 6
Patrick
Opal | Level 21

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;
SASKiwi
PROC Star

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; 
Patrick
Opal | Level 21

@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 = .;

yabwon
Onyx | Level 15

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;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

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;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 400 views
  • 3 likes
  • 5 in conversation