DATA Step, Macro, Functions and more

How to - Find Minimum Positive Variable in An Array?

Reply
Super Contributor
Posts: 395

How to - Find Minimum Positive Variable in An Array?

Hello everyone,

 

I would like to get smallest positive value in an array? Is there a way to do this?

 

Data Have;
Length Variable1 8 Variable2 8 Variable3 8 Variable4 8 Variable5 8 Variable6 8;
Infile Datalines Missover;
Input  Variable1 
Variable2 Variable3 Variable4 Variable5 Variable6;
Datalines;
5 3 1 -1 -3 -5
Run;

Data Want;
Set Have;
Array theArray {*} Variable:;
Do i =1 to 6;
MinVariable=Min(theArray[i]);
End;
Run;

Thanks

Super Contributor
Posts: 395

Re: How to - Find Minimum Positive Variable in An Array?

@SAS_inquisitiveMin(of theArray[i]); is still returning "-5", actually, I expect "1" instead of "-5", am I missing something?

 

Thank you

Super Contributor
Posts: 298

Re: How to - Find Minimum Positive Variable in An Array?

turcay:

 

This should work. For me in SUE, the firstsmall turns to be a missing value. Secondsmall is OK. Why firstsmall is missing?. It may be working for you. Check it and tell.

Data have;
Length Var1 8 Var2 8 Var3 8 Var4 8 Var5 8 Var6 8;
Infile Datalines Missover;
Input  Var1 - Var6;
Datalines;
5 3 1 -1 -3 -5
Run;

Data _null_;
   Set Have;
   firstsmall = .; secondsmall = .;
   Array k[*] Var:;
   firstsamll  = smallest(1, of k[*]);
   secondsmall = smallest(2, of k[*]);
   put firstsmall = ;
   put secondsmall = ;
stop;
Run;
Super Contributor
Posts: 395

Re: How to - Find Minimum Positive Variable in An Array?

Thanks a lot @datasp,

 

But it returns missing either for my environment.

Frequent Contributor
Posts: 95

Re: How to - Find Minimum Positive Variable in An Array?

A similar question was asked here:

 

https://communities.sas.com/t5/SAS-Procedures/The-smallest-positive-value-for-each-variable/td-p/520...

 

You might be able to make use of that solution.

Norman.
SAS 9.4 (TS1M4) X64_7PRO WIN 6.1.7601 Service Pack 1 Workstation

Super User
Posts: 5,516

Re: How to - Find Minimum Positive Variable in An Array?

Trying not to overcomplicate this too much:

 

do I=1 to 6;

   if theArray{I} > 0 then do;

      if minVal = . or theArray{I} < minVal then minVal = theArray{I};

   end;

end;

Super User
Posts: 10,044

Re: How to - Find Minimum Positive Variable in An Array?

Add one more condition.

 

Data Have;
Length Variable1 8 Variable2 8 Variable3 8 Variable4 8 Variable5 8 Variable6 8;
Infile Datalines Missover;
Input  Variable1 
Variable2 Variable3 Variable4 Variable5 Variable6;
Datalines;
5 3 1 -1 -3 -5
Run;

Data Want;
Set Have;
Array theArray {*} Variable:;
Do i =1 to 6;
 if sign(theArray[i]) ne -1 then MinVariable=Min(MinVariable,theArray[i]);
End;
Run;
Super Contributor
Posts: 395

Re: How to - Find Minimum Positive Variable in An Array?

Thank you all of them, I have many solution to find smallest positive value in an array. But I want to ask related question about this to you @Ksharp @datasp @Astounding @Norman21.

 

In User Defined Logic, when I try to use Min function for my array, it returns this -> ERROR: The function MIN requires at least 2 arguments. There are too few arguments for the function MIN.  MinFraction=Min(FractionOfQuarter[i]); works for SAS Base platform, but not for UDL, I want to get minimum value for between whole array values, how can I get it? Any suggestion?

 

 

Do i=1 To 9;     
FractionOfQuarter[i]=Datdif(_cashflow_.Balance.matdate[i],_date_, 'ACT/360')/90; /*It works*/ 
MinFraction=Min(FractionOfQuarter[i]);/*It returns -> ERROR: The function MIN requires at least 2 arguments. There are too few arguments for the function MIN.*/
End;

 

Super Contributor
Posts: 395

Re: How to - Find Minimum Positive Variable in An Array?

Okay,

 

It seems it works when I type the following;

 

MinFraction=Min(of FractionOfQuarter[*]);

Thanks Smiley Happy

Super Contributor
Posts: 298

Re: How to - Find Minimum Positive Variable in An Array?

turcay:

 

I missed to see the positive minimum in your requirement. Hence, smallest() function is not appropriate. There are two ways I can solve it.

[1] By comparing each positive number, and finding the minimum at the same time.

[2] Copying positive numbers to a nee array, and use smallest() function on the new array.

 

Both are shown below. Your query to those who have answered you  on MinFraction=Min(FractionOfQuarter[i]);

is not understandable as the context is not known.

 

Here comes the code:

 

Data have;
Length Var1 8 Var2 8 Var3 8 Var4 8 Var5 8 Var6 8;
Infile Datalines Missover;
Input  Var1 - Var6;
Datalines;
5 3 1 -1 -3 -5
Run;

Data _null_;
   Set have;
   Array k[*] Var:;
   do i = 1 to dim(k);
      if k[i] >= 0 then Min = Min(Min, k[i]);
   end;
   put min = ;
Run;

data _null_;
   set have;
   array k[*] Var:;
   array new[6] _temporary_;
   j = 1;
   do i = 1 to dim(k);
      if k[i] >= 0 then do; new[j] = k[i]; j + 1; end;
   end;
   firstmin = smallest(1, of new[*]);
   put firstmin = ;
run;
Super User
Posts: 11,343

Re: How to - Find Minimum Positive Variable in An Array?

[ Edited ]

@datasp smallest will work, just need to look at it a bit differently:

 

data want;
   set have;
   array k[*} var:  ;
   do i = 1 to dim(k);
      if smallest(i,of k(*)) ge 0 then do;
         val = smallest(i,of k(*));
         leave;;
      end;
   end;

run;

 

I will not make any claim to efficiency other than short code.

Notice that i will, if a positive value is found have, the index of the smallest positive value in the array which may be helpful.

Ask a Question
Discussion stats
  • 10 replies
  • 186 views
  • 1 like
  • 6 in conversation