全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
4835 8
2013-05-21

问题是这样的:现在有个很大的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;


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-5-21 11:30:18
分层,每层内只取一个?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-5-21 11:37:40
不能proc surveyselect?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-5-21 12:08:47
boe 发表于 2013-5-21 11:37
不能proc surveyselect?
是的,不能用proc surveyselect. 公司版本过旧
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-5-22 09:21:42
Not sure if that works in an old version. Code is not tested. Jingju


proc sort data =have out =have1; by patientID;

data have20 have21;
    call streaminit(1234);
    set have1 end =Eof; by patientid;
    retain maxs;
    s ++1;   
    output have20;
    if last.patientid then do;
        r =rand('uniform');
        output have21;
        if s >maxs then maxs =s;s =0;
    end;
    if Eof then call symputx('maxs', maxs);
    drop maxs;
run;

proc sort data =have21 out =have3(obs =5000); by r;

data have4(drop =s tab s1 i);
    call streaminit(1234);
    merge have20 have3(in =h keep =patientid s rename =(s=s1));
    by patientid;
    retain tab;
    array t[&maxs.] _temporary_;
    if h;
    if first.patientid then do;
        do i =1 to s1;
            t{i} =1/s1;
        end;
        tab =rand('tabled', of t{*});
        call missing(of t{*});
    end;
    if tab =s then output;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-5-23 04:16:32
How about this?
复制代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群