This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / 这里有没有懂SAS的?孕妇流产的可能性与其流产历史有关,过去流产多次,这次流产的概率就高。这是统计推理。
在SAS中,如何生成流产历史这个变量?要在每次怀孕时,累加出过去已经流产多少次了。再SAS 中可能要用 RETAIN Statement。The dataset is already sorted by MotherID and PregnancyID. 这个RETAIN要在新的MotherID出现时回零,每出现一次流产加1。
好久不用SAS,忘的差不多了。求救。
-23456789(大白呼);
2004-8-10
{355}
(#1836504@0)
-
如果对应每个pregencyid的流产indicator是个0--1变量, 最后每个孕妇输出一个流产总数. 那可以这样写:proc sort data=liuchan(keep=motherid pregencyid liuchan_flag);
by motherid pregencyid;
run;
data liuchan_tot;
set liuchan;
by motherid pregencyid;
if first.motherid then liuchan_sum=0;
liuchan_sum + liuchan_flag;
if last.motherid;
keep motherid liuchan_sum;
run;
-yahoohotmail(夏天怎么不热);
2004-8-10
{272}
(#1836593@0)
-
流产不是有无,而是次数。比如5此怀孕,第一次和第三次流产,那么在第一次怀孕前流产了0次,第二次怀孕前是1,第三次怀孕前是1,第四次怀孕前2,第五次怀孕前也是2。所以不是if所能及,要用retain。我就是忘了到下一名孕妇时怎么使retain回零。谢谢。
-23456789(大白呼);
2004-8-10
{46}
(#1836761@0)
-
确实不用retain. 你的表里应该有一个column是怀孕的结果, 流产为1, 其它为0. run一下程序就行了.data a;
input motherid pregnencyid liuchan_flag;
cards;
112 1 1
112 2 0
112 3 1
112 4 0
112 5 0
007 1 1
007 2 0
007 3 1
007 4 0
007 5 0
;
run;
proc sort data=a;
by motherid pregnencyid;
run;
data liuchan;
set a;
by motherid pregnencyid;
if first.motherid then liuchan_sum=0;
liuchan_sum + liuchan_flag;
run;
proc print;
run;
-yahoohotmail(夏天怎么不热);
2004-8-10
{376}
(#1836802@0)
-
Try this oneDATA cumulative;
SET mother;
BY mother_id pregnancy_ID;
RETAIN cum_count;
IF FIRST.mother_id THEN cum_count=0;
IF pregnancy_id is something then cum_count=cum_count+1;
RUN;
-mark80(longwalking);
2004-8-10
{178}
(#1836883@0)
-
这个有点象。待我明天到公司试一试。先谢了。那个BY statement就是让RETAIN在遇到一个新的motherID时回零的是吧?
-23456789(大白呼);
2004-8-11
(#1837137@0)
-
憋了两天,我终于整出来了。我感谢二位的热心帮助,现将正确的程序公布如下:DATA Pregnancy; SET Pregnancy;
BY MotherID PregnancyID;
IF MotherID EQ LAG(MotherID) AND LAG(Outcome)='Abortion'
THEN LastAbortion = 1;
ELSE LastAbortion=0;
RUN;
备注:前面的程序定义了变量(LastAbortion)上一次怀孕是否流产。
DATA Pregnancy; SET Pregnancy;
BY MotherID PregnancyID;
RETAIN PreAbortions;
IF First.MotherID THEN PreAbortions=0;
PreAbortions+LastAbortion;
DROP LastAbortion;
RUN;
-23456789(大白呼);
2004-8-12
{412}
(#1838879@0)
-
Good work. You may try this one as alternative.DATA Pregnancy;
SET Pregnancy;
BY MotherID PregnancyID;
RETAIN count;
IF First.MotherID THEN count=0;
PreAbortions = count;
IF Outcome = 'Abortion' THEN count=count + 1;
DROP count
RUN;
-mark80(longwalking);
2004-8-12
{197}
(#1838937@0)
-
很高兴能和大白呼及mark80讨论SAS. 现在我明白了原来大白呼想要累加本次怀孕之前(不含本次)的流产数目. 那用一个DATA STEP 就可以了. 程序见内. 另外,RETAIN是不需要的.当在SAS中用“A + B;”进行累加时,A本身就是被“retain”的. 再用RETAIN语句是多余了.
假设Outcome是0-1变量.
DATA Pregnancy;
SET Pregnancy;
BY MotherID PregnancyID;
LastAbortion=lag(Outcome);
IF first.Motherid THEN DO;
LastAbortion=.;
PreAbortions=0;
END;
ELSE PreAbortions + LastAbortion;
DROP LastAbortion;
run;
-yahoohotmail(夏天怎么不热);
2004-8-12
{327}
(#1839241@0)
-
改进一下, 适应Outcome的字符属性.DATA Pregnancy;
SET Pregnancy;
BY MotherID PregnancyID;
LastAbortion = lag(Outcome= 'Abortion' );
IF first.Motherid THEN DO;
LastAbortion= . ;
PreAbortions= 0 ;
END;
ELSE PreAbortions + LastAbortion;
DROP LastAbortion;
run;
-yahoohotmail(夏天怎么不热);
2004-8-12
{245}
(#1839593@0)
-
That is good one. very interesting!
-mark80(longwalking);
2004-8-12
(#1839777@0)
-
为什么不用RETAIN? SAS的PDV只HANDLE当前的OBS.RETAIN就是用来KEEP 上一个 OBS的.
-redbird("tannis");
2004-8-12
(#1839807@0)
-
For simple accumulative sum, his code shall work.
-23456789(大白呼);
2004-8-13
(#1840249@0)
-
It is always good to know there are any alternatives
-mark80(longwalking);
2004-8-13
(#1840392@0)
-
借帖子问一下,哪能下到sas呀?万分感谢
-march2000(eagle);
2004-8-13
(#1840432@0)