Solved
Valued Guide
Posts: 864

# Multiple set statements with counter

I am using the following code find multiple id's in one dataset.  It works but the counter can be improved for my purposes.  Any help will be appreciated.

data test;

do until(last.power_id);

set mdj.mfb_total;

by power_id;

count+1;

if first.power_id then count = 1;

end;

do until (last.power_id);

set mdj.mfb_total;

by power_id;

if count > 1 then output;

end;

run;

Have

power_id count

2               2

2               2

3               3

3               3

3               3

Want

power_id count

2               1

2               2

3               1

3               2

3               3

Thank You,

Mark

Accepted Solutions
Solution
‎01-14-2015 10:03 AM
PROC Star
Posts: 1,460

## Re: Multiple set statements with counter

Ahh, a Double DoW Loop.

Probably easiest to create a separate counter variable in the second loop.

Something like (untested):

```data test (drop=count1 rename=(count2=count));
count1=0;
count2=0;

do until(last.power_id);
set mdj.mfb_total;
by power_id;
count1=count1+1;
end;

do until (last.power_id);
set mdj.mfb_total;
by power_id;
count2=count2+1;
if count1 > 1 then output;
end;

run;

```

All Replies
Super Contributor
Posts: 355

## Re: Multiple set statements with counter

You mean, you would like to have individual counts by power_id's (??)

data mfb_total;
input power_id;
datalines;
2
2
3
3
3
;
run;

Data Test;
Retain Count 0;
Set mfb_total;
By power_id;
If First.power_id then count=0;
count+1;
Run;

Solution
‎01-14-2015 10:03 AM
PROC Star
Posts: 1,460

## Re: Multiple set statements with counter

Ahh, a Double DoW Loop.

Probably easiest to create a separate counter variable in the second loop.

Something like (untested):

```data test (drop=count1 rename=(count2=count));
count1=0;
count2=0;

do until(last.power_id);
set mdj.mfb_total;
by power_id;
count1=count1+1;
end;

do until (last.power_id);
set mdj.mfb_total;
by power_id;
count2=count2+1;
if count1 > 1 then output;
end;

run;

```

Valued Guide
Posts: 864

## Re: Multiple set statements with counter

That makes sense and seems obvious now that you say it.  Thanks.

🔒 This topic is solved and locked.