问题是这样的:现在有个很大的dataset,我们要从中随机找5000个指定变量不同的数据,例如,如果我设var="patient ID", 我就要找5000个不同的patient ID, 而一个Patient ID会在这个dataset中出现很多次,所以随机寻找中要避免两种情况:1.随机寻找了两个相同的观测值(此部分我已经编出)。2. 随机寻找了两个不同的观测值,但他们的patient ID是一样的(此部分未完成)。望各位大牛指点,感激不尽。我的想法是用两个array和几个do while,一个array装distinct Observation, 一个array装distinct patient ID。其实我觉得想法不难,难的是如何用SAS实现。另外,由于sas版本非常旧,用不了proc surveyselect这个过程。以下是我已经完成的代码:
%macro ransample(dsin,dsout,SampleSize,var);(注释:dsin“输入数据集”,dsout“输出数据集”,SampleSize,var“指定变量”)
proc sort data=PDL.&dsin out=a;
by &var;
run;
data randfile;
array PickIts[&SampleSize];
array PickVar[&SampleSize];
do i=1 to &SampleSize;
PickIt=ceil(ranuni(34567)*TotObs);
if PickIt in PickIts then do;
do PickIt=ceil(ranuni(34567)*TotObs) while (PickIt in PickIts);
end;
If _N_=PickIt then call symput('selectvar', &var);
If &selectvar not in PickVar then do;
PickIts=PickIt;
Pickvar=&selectvar;
set a point=PickIt nobs=TotObs;
output;
end;
else do;
(此处不知道该如何构建循环了,假设我们还拿patient ID为例,这个observation虽然没有在装distinct observation
array中出现过,但它的patient ID却之前出现过,所以此观测量不合格,我们要重新random select)
end;
end;
else do;
If _N_=PickIt then call symput('selectvar', &var);
If &selectvar not in PickVar then do;
PickIts=PickIt;
Pickvar=&selectvar;
set a point=PickIt nobs=TotObs;
output;
end;
else do;
(空缺,如上)
end;
end;
stop;
run;
proc sql;
create table PDL.&dsout as
select a.* from a inner join randfile
on a.&var = randfile.&var ;
quit;
%mend ransample;