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

I'm interested in developing a macro that can split a table into 4 datasets proportional to the size of the input table. Four tables with an equal (or roughly equal) number of records. So to create a macro that takes a table and breaks it up into 4 based on the size of the input table. So for a table with 100,000 records, I'd want it to break it into 4 tables with 25,000 records each.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data
  want1
  want2
  want3
  want4
;
set have;
select (mod(_n_,4));
  when (0) output want1;
  when (1) output want2;
  when (2) output want3;
  when (3) output want4;
end;
run;

I guess it can be seen how this step can be made dynamic.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User
data
  want1
  want2
  want3
  want4
;
set have;
select (mod(_n_,4));
  when (0) output want1;
  when (1) output want2;
  when (2) output want3;
  when (3) output want4;
end;
run;

I guess it can be seen how this step can be made dynamic.

FreelanceReinh
Jade | Level 19

Hi @PegaZeus,

 

Just to add a variant of Kurt Bremser's solution: With two modifications to his code you can achieve a partition into four blocks of consecutive observations of dataset HAVE (first, second, third and fourth quarter):

set have nobs=n;
select (int(4*(_n_-1)/n))

Edit: If you use this in a macro, you should name variable n differently, e.g. ___n,  or dynamically in order to avoid name conflicts with a possibly existing variable n in dataset HAVE.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 790 views
  • 3 likes
  • 3 in conversation