turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- How to - Find Minimum Positive Variable in An Arra...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-04-2017 11:34 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to turcay

10-04-2017 11:52 AM

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

Thank you

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to turcay

10-04-2017 12:37 PM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to datasp

10-05-2017 04:30 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to turcay

10-04-2017 11:57 AM

A similar question was asked here:

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to turcay

10-04-2017 12:15 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to turcay

10-05-2017 03:14 AM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

10-05-2017 04:37 AM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to turcay

10-05-2017 04:41 AM

Okay,

It seems it works when I type the following;

`MinFraction=Min(of FractionOfQuarter[*]);`

Thanks

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to turcay

10-05-2017 05:05 AM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to datasp

10-05-2017 12:00 PM - edited 10-05-2017 06:21 PM

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