Contributor
Posts: 50

# data step looping and conditional logic

Hi,

I need to create a process where I conditionally select records in a datastep. What i'm trying to do (using a simplified example below) is analyze each record to determine if 1 of 2 criteria is met.

1st - I need to keep only 1 record for each "account" in the final output dataset (in the order they occur in this dataset).

2nd - Do a capacity check, to see if a "vendor" has hit their "limit" - which needs to be a running total.

2a - If vendor has hit capacity but is the only one on the account then it needs to be selected. It also needs to check to see that if an account has multiple vendors, and if the vendor on the 1st record for the account is over capacity, then use the 2nd record for the account.

There's just a lot of back and forth with checking records and subtotals and was wondering if anyone has tackled anything like this and could provide any insight.

Thanks,

Tom

data have;
input account vendor \$ limit;
datalines;
101 a 2
101 b 3
102 a 2
103 a 2
101 a 2
102 b 3
104 c 2
103 b 2
;run;

Want:

account vendor limit selected_vendor vendor_total

101    a    2    a    1
102    a    2    a    2
105    a    2    a    3
104    c    2    c    1
103    b    2    b    1

PROC Star
Posts: 2,375

## Re: data step looping and conditional logic

[ Edited ]

There is no 105 in your input data.

The order does not appear to be respected (103 appears before 104 in the input).

Super User
Posts: 10,787

## Re: data step looping and conditional logic

```Honestly,I don't understand what you are talking about .
Where is 105   coming from ?
Assuming I know what you are looking for .

data have;
input account vendor \$ limit;
datalines;
101 a 2
101 b 3
102 a 2
103 a 2
101 a 2
102 b 3
104 c 2
103 b 2
;
;run;

proc sort data=have ;by vendor account ;run;
data temp;
set have;
by vendor account;
if first.account;
run;
data want;
set temp;
by vendor ;
if first.vendor then n=0;
n+1;
if n le limit;
run;

```
Contributor
Posts: 50

## Re: data step looping and conditional logic

I'm sorry i think I had the wrong data set in there...this was the one I showed the results for...

data have;
input account vendor \$ limit;
datalines;
101 a 2
101 b 3
102 a 2
103 a 2
105 a 2
104 a 2
102 b 3
104 c 2
103 b 2
;run;

Discussion stats
• 3 replies
• 342 views
• 0 likes
• 3 in conversation