Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

In SAS, how can I randomly assign half the cases to one group and the remaining half to another?

In SAS, you may wish to assign half the cases to one group and the remaining half to another. Suppose you have a data set of 100 observations. You wish to randomly select 50 cases and run an analysis, and then use the other 50 cases to run the same analysis to compare the two results. Use the following SAS syntax:

data random1(drop=k n) random2(drop=k n); retain k 50 n 100; set original; if ranuni(76543)<=k/n then do; output random1; k=k-1; end; else do; output random2; end; n=n-1; run;

In the above example, original is the name of your SAS data set.

The resulting SAS data sets, random1 and random2, are complementary, and each includes 50 observations.

Notice that the number 76543 in the example above is a seed for the random number generator. You can use any integer less than 231 as a seed, and a different seed will yield a different set of division of your data set. If the seed is less than or equal to 0, the time of day is used instead.

For more about statistical and mathematical software, email the UITS Stat/Math Center, visit the center's web page, or phone 812-855-4724 (IUB) or 317-278-4740 (IUPUI). The center is located in Bloomington at 410 N. Park Avenue, and is open for consultation by appointment Monday-Friday 9am-5pm.

This is document aehl in domain all.
Last modified on January 28, 2011.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.