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.

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