Here are some simple ways to extract various types of random samples from SAS datasets, using only base SAS.

Sampling without replacement.
A simple way to select N random elements from a dataset without replacement is to first randomly permute the dataset, and then take the first N elements of the permuted dataset. The process of permuting a dataset is surprisingly useful, and worthy of a macro:

%macro permute(dsn_in=, dsn_out=, tag_name=);
	&local dsn_temp = temp_work_space;

	data &dsn_temp;
		set &dsn_in;
		&tag_name = uniform(-1); 

	proc sort data=&dsn_temp;
		by &tag_name;

	data &dsn_out (drop=&tag_name);
		set &dsn_temp;
%mend permute;


The parameters dsn_in and dsn_out are the names of the dataset to be permuted and the name of the resultant dataset, respectively. The tag_name parm requires explanation. The macro permutes a dataset S by creating a temporary dataset T which consists of S with an additional field. That field contains uniformly distributed random numbers in (0,1) and goes by the name &tag_name. The value of that name should be chosen to avoid collisions with variable names already in S.

A simple example of usage:

data counts;
do count = 1 to 20;
	output;
end;
run;

* Choose the tag_name to avoid collisions with existing variables in dsn_in ;
%permute(dsn_in=counts, dsn_out=permuted_counts, tag_name=foobar);
run;

Sampling with replacement
A simple way to choose with replacement is to use the point keyword when reading the data. The macro randomly chooses N elements from &dns_in and writes them to &dsn_out

%macro choose(dsn_in=, dsn_out=, N=);
  DATA &dsn_out;
  do k = 1 to &N;
        choice = ceil( uniform(-1) * FILE_SIZE );   * choice is a random integer in 1 .. FILE_SIZE ;
        set &dsn_in point=choice nobs=FILE_SIZE;
        output;
  end;
  stop;
%mend choose;

* here's the usage ;
%choose(dsn_in=counts, dsn_out=chosen, N=5);
run;

Bernoulli sampling
In the methods above, the user specifies in advance the number of records to be chosen. In some cases, one might want the number of elements selected to vary randomly. Essentially one considers each element and decides whether to select it by tossing a coin. The process is so simple it’s not worth bothering with a macro.

DATA bernoulli_choice;
	set counts;  * use our old friend counts as input;
	if (uniform(-1) < .5) then output;
run;

The choice of .5 as the selection probability guarantees that any subset of counts (including the empty set and all of counts) is equally likely to be selected.