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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 417 views
  • 1 like
  • 2 in conversation