楼主: moshushi8928
4924 8

求助SAS高手随机选择样本问题 [推广有奖]

  • 4关注
  • 1粉丝

高中生

55%

还不是VIP/贵宾

-

威望
0
论坛币
3 个
通用积分
0
学术水平
5 点
热心指数
5 点
信用等级
5 点
经验
124 点
帖子
13
精华
0
在线时间
39 小时
注册时间
2011-12-7
最后登录
2021-12-15

楼主
moshushi8928 发表于 2013-5-21 10:34:30 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

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


二维码

扫码加我 拉你入群

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

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

关键词:surveyselect observation inner join Distinct observat 样本

沙发
boe 发表于 2013-5-21 11:30:18
分层,每层内只取一个?
Gorgeous girl , I love !

藤椅
boe 发表于 2013-5-21 11:37:40
不能proc surveyselect?
Gorgeous girl , I love !

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

报纸
jingju11 发表于 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?
  1. %let seed=12345;
  2. %let sampsize=5000;
  3. data hell;
  4. do PatientID=1 to 20000;
  5. do visit=1 to ceil(ranuni(&seed)*100);
  6. measure = exp(5+rannor(&seed));
  7. _ORDER_ = ranuni(&seed);
  8. output;
  9. end;
  10. end;
  11. stop;
  12. run;
  13. proc sort data=hell;
  14. by _ORDER_;
  15. proc means data=hell noprint nway min order=data;
  16. class PatientID;
  17. var _ORDER_;
  18. output out=sample(drop=_freq_ _type_) idgroup(min(_ORDER_) out(visit measure)=);
  19. proc print data=sample(obs=&sampsize);
复制代码
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

7
moshushi8928 发表于 2013-5-23 04:34:34
邓贵大 发表于 2013-5-23 04:16
How about this?
好高端啊,但不知道为什么要do PatientID=1 to 20000?是想产生20000个patient ID吗? 我这个作业中patient ID已经有了,存储在源数据集aaa中, 那该如何实现呢?菜鸟多谢大牛了!

8
moshushi8928 发表于 2013-5-23 04:35:13
jingju11 发表于 2013-5-22 09:21
Not sure if that works in an old version. Code is not tested. Jingju
非常感谢哈,不过可能是版本因素,run不起来

9
邓贵大 发表于 2013-5-24 01:50:25
moshushi8928 发表于 2013-5-23 04:34
好高端啊,但不知道为什么要do PatientID=1 to 20000?是想产生20000个patient ID吗? 我这个作业中pat ...
Replace 'visit measure' with the list of variables you want to keep from your dataset.
  1. %let seed=12345;
  2. %let sampsize=5000;
  3. data hell;
  4. set aaa;
  5. _ORDER_ = ranuni(&seed);

  6. proc sort data=hell;
  7. by _ORDER_;
  8. proc means data=hell noprint nway min order=data;
  9. class PatientID;
  10. var _ORDER_;
  11. output out=sample(drop=_freq_ _type_) idgroup(min(_ORDER_) out(visit measure)=);
  12. proc print data=sample(obs=&sampsize);
  13. run;
复制代码
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-5 14:05