Random sas从表中随机选择10个OB

Random sas从表中随机选择10个OB,random,sas,Random,Sas,我有一个包含1000个OB的数据集,我希望选择10个随机OB。 据我所知,我需要使用RANUNI或RAND,但我不知道如何实现 坦克有很多方法可以做到这一点,但最简单的方法可能是 data have; do x=1 to 1000; output; end; run; proc surveyselect data=have out=want seed=123 noprint method=srs sampsize=10; run; 有很多方

我有一个包含1000个OB的数据集,我希望选择10个随机OB。 据我所知,我需要使用RANUNI或RAND,但我不知道如何实现


坦克

有很多方法可以做到这一点,但最简单的方法可能是

data have;
    do x=1 to 1000;
        output;
    end;
run;

proc surveyselect data=have out=want seed=123 noprint
     method=srs
     sampsize=10;
run;

有很多方法可以做到这一点,但最简单的可能就是这样

data have;
    do x=1 to 1000;
        output;
    end;
run;

proc surveyselect data=have out=want seed=123 noprint
     method=srs
     sampsize=10;
run;

此宏从数据集中随机选择观测值

输入:

+---------+---+----+
| counter | x | y  |
+---------+---+----+
|       1 | 2 |  2 |
|       2 | 3 |  6 |
|       3 | 4 | 12 |
|       4 | 5 | 20 |
|       5 | 6 | 30 |
+---------+---+----+

data have;
   do counter=1 to 1000;
      x=counter+1;
      y=counter*x;
      output;
   end;
run;
宏:

%macro select_random_obs(libname,memname,num);%macro d;%mend d;
/*
libname - libname of your dataset
memname - name of dataset
num - num of obs to select randomly
*/
proc sql noprint; /*select num of obs in your dataset (if it is not static value)*/
   select nobs into:max from dictionary.tables where libname="%upcase(&libname)" and memname="%upcase(&memname)";
quit;
%let rand_list=; /*macro variable that will contains random nums of obs to select*/

data _null_; /*init rand_list macro variable*/
   length tList $32000.;
   n=0;
   do while (n<&num);
      if n=0 then tList="";
      repeat:
      u = rand("Uniform");
      k = ceil( &Max*u );
      str=strip(input(k,best12.));
      do i=1 to countw(tList,' ');
         if scan(tList,i,' ') = k then goto repeat;
      end;
      tList=catx(' ',tList,str);
      n=n+1;
   end;
   call symputx('rand_list',tList);

run;
%put &=rand_list;

data want; /*create new data set that contain right number of random observations*/
   set have;
   if _N_ in (&rand_list);
run;

%mend select_random_obs;

%select_random_obs(work,have,10);

此宏从数据集中随机选择观测值

输入:

+---------+---+----+
| counter | x | y  |
+---------+---+----+
|       1 | 2 |  2 |
|       2 | 3 |  6 |
|       3 | 4 | 12 |
|       4 | 5 | 20 |
|       5 | 6 | 30 |
+---------+---+----+

data have;
   do counter=1 to 1000;
      x=counter+1;
      y=counter*x;
      output;
   end;
run;
宏:

%macro select_random_obs(libname,memname,num);%macro d;%mend d;
/*
libname - libname of your dataset
memname - name of dataset
num - num of obs to select randomly
*/
proc sql noprint; /*select num of obs in your dataset (if it is not static value)*/
   select nobs into:max from dictionary.tables where libname="%upcase(&libname)" and memname="%upcase(&memname)";
quit;
%let rand_list=; /*macro variable that will contains random nums of obs to select*/

data _null_; /*init rand_list macro variable*/
   length tList $32000.;
   n=0;
   do while (n<&num);
      if n=0 then tList="";
      repeat:
      u = rand("Uniform");
      k = ceil( &Max*u );
      str=strip(input(k,best12.));
      do i=1 to countw(tList,' ');
         if scan(tList,i,' ') = k then goto repeat;
      end;
      tList=catx(' ',tList,str);
      n=n+1;
   end;
   call symputx('rand_list',tList);

run;
%put &=rand_list;

data want; /*create new data set that contain right number of random observations*/
   set have;
   if _N_ in (&rand_list);
run;

%mend select_random_obs;

%select_random_obs(work,have,10);

太好了,谢谢。但它总是得出相同的观察结果。我如何让它每次选择不同的OB?只需松开种子=123 Part太好了,谢谢。但它总是得出相同的观察结果。我如何让它每次选择不同的OB?只需松开种子=123部分