BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Preguntarius
Fluorite | Level 6

Hi everyone, I have a SAS table with the variable "target" which has only two possible values: 0 and 1.

Preguntarius_0-1719843874183.png

I want to build groups, starting from the first line, so that each group has at least 30 values 1 of "target" and so that each group has at least 2000 rows.

So, if f.e. in the first 2000 rows I already have 30 values 1 of "target", the first 2000 rows would build the first group. If not, I have to take more rows in the first group. And so on.

Does anybody has an Idea how to do it?

Thanks in Advance

Denis

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Something like this ought to work:

 

data want;
group = _n_;
n_targets = 0;
group_size = 0;
do until (group_size >= 2000 and n_targets >= 30); set have; group_size + 1; n_targets + target;
output; end; run;

It's untested, but you have the data to try it out.  As you might suspect, the last group might be a little smaller and you will need to figure out what to do with that.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

Something like this ought to work:

 

data want;
group = _n_;
n_targets = 0;
group_size = 0;
do until (group_size >= 2000 and n_targets >= 30); set have; group_size + 1; n_targets + target;
output; end; run;

It's untested, but you have the data to try it out.  As you might suspect, the last group might be a little smaller and you will need to figure out what to do with that.

Preguntarius
Fluorite | Level 6
Nice, thanks!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 348 views
  • 1 like
  • 2 in conversation