Hi -- I'm in need of a way to flag observations in a dataset within a group by groups of 5. Is there an easy way to do this?
For example:
GROUP | OBS | WANT |
1 | 1 | 1 |
1 | 2 | 1 |
1 | 3 | 1 |
1 | 4 | 1 |
1 | 5 | 1 |
1 | 6 | 2 |
1 | 7 | 2 |
1 | 8 | 2 |
1 | 9 | 2 |
1 | 10 | 2 |
1 | 11 | 3 |
1 | 12 | 3 |
2 | 1 | 1 |
2 | 2 | 1 |
2 | 3 | 1 |
2 | 4 | 1 |
2 | 5 | 1 |
2 | 6 | 2 |
2 | 7 | 2 |
2 | 8 | 2 |
2 | 9 | 2 |
2 | 10 | 2 |
data a;
do _n_=1 by 1 until(last.make);
set sashelp.cars;
by make;
if first.make then want=1;
if mod(_n_,5)=0 then do; output;want+1;end;
else output;
end;
keep make model want;
run;
demo
data have;
input GROUP OBS ;
cards;
1 1 1
1 2 1
1 3 1
1 4 1
1 5 1
1 6 2
1 7 2
1 8 2
1 9 2
1 10 2
1 11 3
1 12 3
2 1 1
2 2 1
2 3 1
2 4 1
2 5 1
2 6 2
2 7 2
2 8 2
2 9 2
2 10 2
;
data want;
set have;
by group;
if first.group then want=1;
if mod(obs,5)=0 then do; output;want+1;end;
else output;
run;
I think that's close but not the expected result. For example, check the first 3 rows of the Audi group:
data a;
set sashelp.cars;
by make;
obs=_n_;
if first.make then want=1;
if mod(obs,5)=0 then do; output;want+1;end;
else output;
keep obs make model want;
run;
data a;
do _n_=1 by 1 until(last.make);
set sashelp.cars;
by make;
if first.make then want=1;
if mod(_n_,5)=0 then do; output;want+1;end;
else output;
end;
keep make model want;
run;
@mdavidson You are welcome. My apologies for the lack of attention to detail earlier. I have had this problem for ages 😞 . Have a good day
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.