BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mdavidson
Quartz | Level 8

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

6 REPLIES 6
Reeza
Super User
Try the MOD() function along with BY processing to reset COUNT at the start of each GROUP. .

IF mod(obs, 5) = 1 then count+1;
novinosrin
Tourmaline | Level 20

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;
mdavidson
Quartz | Level 8

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;

novinosrin
Tourmaline | Level 20
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
Quartz | Level 8
Thank you, this is perfect! Genius!
novinosrin
Tourmaline | Level 20

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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1117 views
  • 0 likes
  • 3 in conversation