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

I'm using SAS to do machine learning. I would like to randomly split my data into 60% training, 20% validation, and 20% test data sets. How do I do that in SAS?

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

There are many ways. Here is one:

 

data training validation test;
  set sashelp.class;
  _n_=rand('uniform');
  if _n_ le .6 then output training;
  else if _n_ le .8 then output validation;
  else output test;
run;

Art, CEO, AnalystFinder.com

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

There are many ways. Here is one:

 

data training validation test;
  set sashelp.class;
  _n_=rand('uniform');
  if _n_ le .6 then output training;
  else if _n_ le .8 then output validation;
  else output test;
run;

Art, CEO, AnalystFinder.com

Reeza
Super User

Are you using SAS EM? If so, check the Partition task. 

Ksharp
Super User
Or PROC SURVEYSELECT.


%let dsid=%sysfunc(open(sashelp.class));
%let nobs=%sysfunc(attrn(&dsid,nlobs));
%let dsid=%sysfunc(close(&dsid));

%let train=%sysevalf(0.6*&nobs,int);
%let valid=%sysevalf(0.2*&nobs,int);
%let test=%eval(&nobs-&train-&valid);

%put &train &valid &test;

proc surveyselect data=sashelp.class group=(&train &valid &test) out=want;
run;

data train valid test;
 set want;
 select(groupid);
 when(1) output train;
 when(2) output valid;
 when(3) output test;
 otherwise;
 end;
run;

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
  • 4 replies
  • 2996 views
  • 3 likes
  • 4 in conversation