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.
Last modified on January 28, 2011.







