Sas help

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Sas help

Hi,

How to select a random sample from a population? I have a dataset with 22mn observation and I have to select 10% random sample data from it. Please help!


Accepted Solutions
Solution
‎03-02-2014 04:15 PM
Super User
Posts: 19,850

Re: Sas help

Proc SurveySelect

View solution in original post


All Replies
Super Contributor
Posts: 644

Re: Sas help

I'm assuming you need this for operational reasons (eg testing systems) rather than statistical purposes (otherwise the sample is much larger than you probably need).

Data want ;

     Set have ;

     where ranuni(101) <= 0.1 ;

Run ;

In the above code the 101 is the seed for the random number generator.  It can be any positive number to ensure the sample is repeatable.  Using 0 instead will result in a non repeatable selection.

Richard

Super User
Super User
Posts: 7,070

Re: Sas help

Using the POINT= option will be much faster way to select a sample than reading the whole data set. More so when the sample percentage is small.

%let in=sashelp.cars;

%let out=ransel;

%let percent=10;

%let seed=0;

data &out;

   drop _count _left;

  _count = ceil((&percent/100)*_n);

  _left = _n;

   put "NOTE: Number of rows in &in is " _n +(-1) '.'

    / "NOTE: Random sample size = " _count +(-1) '.'

  ;

*---------------------------------------------------------------------;

* Step though the input data set. Select the row when the ratio of the;

* number of rows required to complete the sample to the number of rows ;

* remaining in the data set is greater than a random number (0,1). ;

*---------------------------------------------------------------------;

   do while (_count > 0);

   _k + 1;

     if (_count/_left > ranuni(&seed)) then do;

        set &in point=_k nobs=_n;

        output;

    _count = _count - 1;

     end;

   _left = _left - 1;

   end;

   stop;

run;

Solution
‎03-02-2014 04:15 PM
Super User
Posts: 19,850

Re: Sas help

Proc SurveySelect

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 183 views
  • 0 likes
  • 4 in conversation