Write and run SAS programs in your web browser

Select random observation.

Reply
New Contributor
Posts: 2

Select random observation.

[ Edited ]

Hii, can any one plz tell how can we retrieve a single random observation(row) from a data set?

Trusted Advisor
Posts: 1,470

Re: question.

you can use:

    set input(obs=1, firstobs=) ;  /* change firstobs to any row number you want */

SAS Super FREQ
Posts: 706

Re: question.

Hi

 

You can use some options on the SET statement to achieve this, see example below. Please be aware when using the POINT= option, you also need a STOP statement, as otherwise the DATA Step will loop. Have a look in the doc at the description of the POINT= and NOBS= options. The variable name used in the POINT= option is not written to the output dataset, this is why I use two variables.

 

data want;
  getObs = ceil( ranuni(0) * numObs);
  _getObs = getObs;
  set sashelp.cars point=_getObs nobs=numObs;
  output;
  stop;
run;

Also look at Proc SURVEYSELECT for more sophisticated ways to get a random sample from a SAS data set

 

Bruno

Super User
Posts: 9,867

Re: Select random observation.


proc surveyselect data=sashelp.class out=want sampsize=1;
run;

Ask a Question
Discussion stats
  • 3 replies
  • 262 views
  • 1 like
  • 4 in conversation