%let size = 25;
data want;
set have;
retain group 1;
if mod(_n_,&size.) = 0 then group + 1;
run;
You now have a group designator which can be used in multiple ways, e.g. to switch sheets when using ODS EXCEL and a reporting procedure.
Splitting tables is rarely necessary. You can leave this as one big table, with an identifier variable to indicate which of the four parts, then perform analyses BY this identifier variable.
Example:
data cars;
set sashelp.cars;
if mod(_n_,25)=1 then identifier+1;
run;
proc means data=cars;
by identifier;
run;
If, for example, you only want to work on the data when identifier=3 (and not the rest of the data), you can do this too
proc means data=cars;
where identifier=3;
run;
%let size = 25;
data want;
set have;
retain group 1;
if mod(_n_,&size.) = 0 then group + 1;
run;
You now have a group designator which can be used in multiple ways, e.g. to switch sheets when using ODS EXCEL and a reporting procedure.
@Kurt_Bremser wrote:
%let size = 25; data want; set have; retain group 1; if mod(_n_,&size.) = 0 then group + 1; run;
You now have a group designator which can be used in multiple ways, e.g. to switch sheets when using ODS EXCEL and a reporting procedure.
I agree with your comment. But the code needs a minor tweak. The first group above would only have 24 observations.
I think this will do it:
retain group 0;
if mod(_n_ - 1,&size.) = 0 then group + 1;
@Kurt_Bremser wrote:
I think this will do it:
retain group 0; if mod(_n_ - 1,&size.) = 0 then group + 1;
Or my code earlier will do it as well.
Wrote a macro for this a while back:
https://gist.github.com/statgeek/49e54641ceaf58bc4fe5dc2062bc89cb
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.