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

Hi all,

 

I have a dataset with 5472 observations but I would like to split it up into two datasets with 2736 observations each. I would like to do this by keeping the first 12 observations for the first dataset and the second 12 for the second, third 12 for the first, fourth 12 for the second, and so on. Is there a succinct way to do this? I am aware of the mod function but that only returns every nth value. Any help is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Double MOD function?
retain group class;
if mod(_n_, 12) = 1 then group+1;
class = mod(group, 2);

You can probably combine those as well.

View solution in original post

6 REPLIES 6
Reeza
Super User
Double MOD function?
retain group class;
if mod(_n_, 12) = 1 then group+1;
class = mod(group, 2);

You can probably combine those as well.
novinosrin
Tourmaline | Level 20

@tbanh Please go with squeaky clean @Reeza 's solution, here is some fun to play with 🙂

 

data have;
do i=1 to 5472;
output;
end;
run;

data one two;
do _iorc_=1 to 2;
do _n_=1 to 12 until(lr);
set have end=lr;
if _iorc_=1 then output one;
else output two;
end;
end;
run;
Astounding
PROC Star

I'm not sure this is elegant, but it works:

 

data want1 want2;

do _n_=1 to 12;

   link retrieve;

   output want1;

end;

do _n_=1 to 12;

   link retrieve;

   output want2;

end;

return;

retrieve:  set have;

return;

run;

 

By using the same SET statement, both loops continue reading the next observation, then the next, etc.

Reeza
Super User

12 makes me think it's monthly data, is there perhaps another variable that would identify the groups? If so, there are ways to make this entirely data driven. 

tbanh
Fluorite | Level 6

Hi Reeza,

 

No it is not monthly data. I had PROC MCMC output simulated data from the posterior distribution for predicted values. Each individual (N = 228) had observations representing a quantitative task and a verbal task, thus my wanting to separate each individual's observations into a quantitative dataset and a verbal dataset. Thank you for your help!

Reeza
Super User

@tbanh wrote:

Hi Reeza,

 

No it is not monthly data. I had PROC MCMC output simulated data from the posterior distribution for predicted values. Each individual (N = 228) had observations representing a quantitative task and a verbal task, thus my wanting to separate each individual's observations into a quantitative dataset and a verbal dataset. Thank you for your help!


Do you have a variable that indicates if it's qualitative or quantitative? Trusting an implied order is a risky game.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 6 replies
  • 1609 views
  • 6 likes
  • 4 in conversation