SAS统计中常用的10个命令

2017-05-29 MedSci MedSci原创

SAS是平时学习中常用到的数据处理软件之一。在处理大批量数据时,SAS不能说太好用呢。SAS也是学习起来十分简单的一个软件,掌握一些基本的命令,就可以满足日常的数据处理需求。01proc sort   data= aout= bnodup;    bystkcd date;    run;proc sort 是特别特别常用到的,因为许

SAS是平时学习中常用到的数据处理软件之一。在处理大批量数据时,SAS不能说太好用呢。SAS也是学习起来十分简单的一个软件,掌握一些基本的命令,就可以满足日常的数据处理需求。

01

proc sort   data= aout= bnodup;    bystkcd date;    run;

proc sort 是特别特别常用到的,因为许多后续命令都要求数据是按照一定格式排列的。比如下面会提到的merge和 first/last。此外,nodup允许我们使用sort命令来去除重复观测值。

02

datad;    mergeb c;    bystkcd date;run;

merge 可以在数据步中实现两个数据集合的合并。在by选项可以定义根据那些变量进行数据的合并。比如在上面给出的例子中,就是根据股票代码(stkcd)和日期(date)进行合并的。

03

datae;    setb;    bystkcd date;    iffirst.date then delete;    iflast.date then delete;run;

有时候,我们可能只需要一个对象所有日期的第一个或者最后一个观测值。这时候first和last就显得特别好用啦。先用前面所提到的sort先对数据集进行排序,然后在数据步先by排序的变量,接着就可以使用first和last对第一个和最后一个观测值进行处理。

04

proc expand    data=crsp_m  out=umd;

bypermno;

iddate;

convert ret = cum_return /     transformin=(+1)     transformout=(MOVPROD 6 -1);

quit;

如果需要滚动求和(Rolling average)或者滚动求积(Rolling product),proc expand是再方便不过了。以上面这个小程序为例子,我们要对crsp_m这个数据集进行处理,处理完成的数据集命名为umd。 上面的程序实现的就是对每一只股票(permno)在一个日期(id)计算一个累积6个月收益cum_return。其中cum_return可以表达如下:

cumreturn=(1+ret−1)(1+ret−2)(1+ret−3)(1+ret−4)(1+ret−5)(1+ret−6)-1

05

data cmpst;

setcmpst_raw;

dodate = rdq-90tordq+10;

output;

end;

run;

采用事件研究方法时,需要根据事件日构建事件窗。这时候可以利用上面例子的方式利用do实现,不过需要注意的是不要把output和end落下了,不然会报错的哦。上面的例子就是根据时间rdq,构建事件窗,事件窗是事件前90天到事件后10天。

06

proc means       data= crsp_mnwaynoprint;

classyear permno;

varret;

outputout =stat        mean= std=  ;

run;

我们还可能还常常需要求一个对象在给定时间内某变量的均值,标准差等统计值。这时候就用proc means。 上面的例子中,输入是股票的月收益率,输出送每只股票每年的月收益率的均值和标准差。加入nway是因为避免在输出的数据集stat中输出总体均值,标准差。

07

proc import     out= crsp_m        datafile= "C:\crsp_m.csv"         dbms=csv replace;        getnames=yes;run;       

proc export             data= results      outfile="C:\results.xlsx"      dbms=xlsxreplace;

label;

run;

然后我们可能常常需要导入和导出数据xlsx,xls,和csv格式的文件。一般会用到proc import 和proc export。用法就如上,不过需要注意的是,dbms需要与文件后缀名保持一致,所以记得改哦。

08

proc rank       data=crsp_mout=umd        group=10;

bydate;

varcum_return;

ranksmomr;

run;

在一些情景中,需要将样本按照某一变量的大小分成几组。 利用proc rank, 就可以轻松通过group来定义你分组的个数,通过var给出分组所依据的变量。ranks 后定义了分组对应的变量名。

09

proc univariate   data=crsp_m noprint;

whereexchcd = 1 ;

varsize;

bydate sic ; 

outputout= nyse_bp           pctlpts= 10 20 30          pctlpre= sizedec ;

run;

proc univariate的功能和proc rank很相似, 不过它输出的是一个样本中某一变量的分位数,根据这个分位数,我们可以进一步地对样本进行分组。 那在什么情况下我们会用到proc univariate呢?一个简单的例子就是我们需要对A 样本根据x 变量进行分组,但是分组是基于在B样本中x变量的分位数。 这时候先利用proc univariate B样本得到x变量的分位数,然后在用得到的分位数来对A样本进行分组。在读文献的时候,经常会遇到样本包含了NYSE,NASDAQ和AMEX三个交易所的股票,然后进行分组的时候只用NYSE子样本(NYSE Breakpoints)。

10

proc sql;    create tablecrsp_m3as

selecta.*, b.*

fromcrsp_m2asa,nyse_bpasb

wherea.date=b.dateanda.sic = b.sic;

quit;

除了在数据步使用merge来进行数据集的合,你还可以使用proc sql来进行merge。他们两者的功能相似,不过在进行一对多的合并的时候使用merge容易出错,所以这时候对推荐使用sql。

版权声明:
本网站所有内容来源注明为“梅斯医学”或“MedSci原创”的文字、图片和音视频资料,版权均属于梅斯医学所有。非经授权,任何媒体、网站或个人不得转载,授权转载时须注明来源为“梅斯医学”。其它来源的文章系转载文章,或“梅斯号”自媒体发布的文章,仅系出于传递更多信息之目的,本站仅负责审核内容合规,其内容不代表本站立场,本站不负责内容的准确性和版权。如果存在侵权、或不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。
在此留言
评论区 (4)
#插入话题
  1. [GetPortalCommentsPageByObjectIdResponse(id=2007771, encodeId=be18200e77105, content=<a href='/topic/show?id=cf4d1590956' target=_blank style='color:#2F92EE;'>#SAS#</a>, beContent=null, objectType=article, channel=null, level=null, likeNumber=34, replyNumber=0, topicName=null, topicId=null, topicList=[TopicDto(id=15909, encryptionId=cf4d1590956, topicName=SAS)], attachment=null, authenticateStatus=null, createdAvatar=, createdBy=8e4c53, createdName=jiyangfei, createdTime=Tue Oct 03 13:45:00 CST 2017, time=2017-10-03, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204357, encodeId=11dc20435e90, content=学习了谢谢分享。, beContent=null, objectType=article, channel=null, level=null, likeNumber=50, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/oLAjfB7s1ib06iaV3D9Tg5Kzuf9u71gZhPYMiajRtENwicoAABeQtfXOlic8ibhYSy6DvJZWUDVtjRvTfqBffr1XJ6JLtFB5kHicthl/0, createdBy=d41c2055892, createdName=184****9840, createdTime=Tue May 30 09:17:02 CST 2017, time=2017-05-30, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204231, encodeId=89ad204231c5, content=学习了谢谢分享。, beContent=null, objectType=article, channel=null, level=null, likeNumber=58, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/oLAjfB7s1ib06iaV3D9Tg5Kzuf9u71gZhPYMiajRtENwicoAABeQtfXOlic8ibhYSy6DvJZWUDVtjRvTfqBffr1XJ6JLtFB5kHicthl/0, createdBy=08bb2061153, createdName=189****7206, createdTime=Tue May 30 06:38:12 CST 2017, time=2017-05-30, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204198, encodeId=67fd20419890, content=非常详细生动, beContent=null, objectType=article, channel=null, level=null, likeNumber=52, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/ajNVdqHZLLDcohE5MKT1FYlH69x7qib45icLgBDMANxIiauEA62nuQI1eE8Ez2rO3yqOkRLH6ha6Lw0x1IBRm0N7g/0, createdBy=e1ff2033933, createdName=187****0626, createdTime=Tue May 30 05:47:25 CST 2017, time=2017-05-30, status=1, ipAttribution=)]
    2017-10-03 jiyangfei
  2. [GetPortalCommentsPageByObjectIdResponse(id=2007771, encodeId=be18200e77105, content=<a href='/topic/show?id=cf4d1590956' target=_blank style='color:#2F92EE;'>#SAS#</a>, beContent=null, objectType=article, channel=null, level=null, likeNumber=34, replyNumber=0, topicName=null, topicId=null, topicList=[TopicDto(id=15909, encryptionId=cf4d1590956, topicName=SAS)], attachment=null, authenticateStatus=null, createdAvatar=, createdBy=8e4c53, createdName=jiyangfei, createdTime=Tue Oct 03 13:45:00 CST 2017, time=2017-10-03, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204357, encodeId=11dc20435e90, content=学习了谢谢分享。, beContent=null, objectType=article, channel=null, level=null, likeNumber=50, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/oLAjfB7s1ib06iaV3D9Tg5Kzuf9u71gZhPYMiajRtENwicoAABeQtfXOlic8ibhYSy6DvJZWUDVtjRvTfqBffr1XJ6JLtFB5kHicthl/0, createdBy=d41c2055892, createdName=184****9840, createdTime=Tue May 30 09:17:02 CST 2017, time=2017-05-30, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204231, encodeId=89ad204231c5, content=学习了谢谢分享。, beContent=null, objectType=article, channel=null, level=null, likeNumber=58, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/oLAjfB7s1ib06iaV3D9Tg5Kzuf9u71gZhPYMiajRtENwicoAABeQtfXOlic8ibhYSy6DvJZWUDVtjRvTfqBffr1XJ6JLtFB5kHicthl/0, createdBy=08bb2061153, createdName=189****7206, createdTime=Tue May 30 06:38:12 CST 2017, time=2017-05-30, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204198, encodeId=67fd20419890, content=非常详细生动, beContent=null, objectType=article, channel=null, level=null, likeNumber=52, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/ajNVdqHZLLDcohE5MKT1FYlH69x7qib45icLgBDMANxIiauEA62nuQI1eE8Ez2rO3yqOkRLH6ha6Lw0x1IBRm0N7g/0, createdBy=e1ff2033933, createdName=187****0626, createdTime=Tue May 30 05:47:25 CST 2017, time=2017-05-30, status=1, ipAttribution=)]
    2017-05-30 184****9840

    学习了谢谢分享。

    0

  3. [GetPortalCommentsPageByObjectIdResponse(id=2007771, encodeId=be18200e77105, content=<a href='/topic/show?id=cf4d1590956' target=_blank style='color:#2F92EE;'>#SAS#</a>, beContent=null, objectType=article, channel=null, level=null, likeNumber=34, replyNumber=0, topicName=null, topicId=null, topicList=[TopicDto(id=15909, encryptionId=cf4d1590956, topicName=SAS)], attachment=null, authenticateStatus=null, createdAvatar=, createdBy=8e4c53, createdName=jiyangfei, createdTime=Tue Oct 03 13:45:00 CST 2017, time=2017-10-03, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204357, encodeId=11dc20435e90, content=学习了谢谢分享。, beContent=null, objectType=article, channel=null, level=null, likeNumber=50, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/oLAjfB7s1ib06iaV3D9Tg5Kzuf9u71gZhPYMiajRtENwicoAABeQtfXOlic8ibhYSy6DvJZWUDVtjRvTfqBffr1XJ6JLtFB5kHicthl/0, createdBy=d41c2055892, createdName=184****9840, createdTime=Tue May 30 09:17:02 CST 2017, time=2017-05-30, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204231, encodeId=89ad204231c5, content=学习了谢谢分享。, beContent=null, objectType=article, channel=null, level=null, likeNumber=58, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/oLAjfB7s1ib06iaV3D9Tg5Kzuf9u71gZhPYMiajRtENwicoAABeQtfXOlic8ibhYSy6DvJZWUDVtjRvTfqBffr1XJ6JLtFB5kHicthl/0, createdBy=08bb2061153, createdName=189****7206, createdTime=Tue May 30 06:38:12 CST 2017, time=2017-05-30, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204198, encodeId=67fd20419890, content=非常详细生动, beContent=null, objectType=article, channel=null, level=null, likeNumber=52, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/ajNVdqHZLLDcohE5MKT1FYlH69x7qib45icLgBDMANxIiauEA62nuQI1eE8Ez2rO3yqOkRLH6ha6Lw0x1IBRm0N7g/0, createdBy=e1ff2033933, createdName=187****0626, createdTime=Tue May 30 05:47:25 CST 2017, time=2017-05-30, status=1, ipAttribution=)]
    2017-05-30 189****7206

    学习了谢谢分享。

    0

  4. [GetPortalCommentsPageByObjectIdResponse(id=2007771, encodeId=be18200e77105, content=<a href='/topic/show?id=cf4d1590956' target=_blank style='color:#2F92EE;'>#SAS#</a>, beContent=null, objectType=article, channel=null, level=null, likeNumber=34, replyNumber=0, topicName=null, topicId=null, topicList=[TopicDto(id=15909, encryptionId=cf4d1590956, topicName=SAS)], attachment=null, authenticateStatus=null, createdAvatar=, createdBy=8e4c53, createdName=jiyangfei, createdTime=Tue Oct 03 13:45:00 CST 2017, time=2017-10-03, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204357, encodeId=11dc20435e90, content=学习了谢谢分享。, beContent=null, objectType=article, channel=null, level=null, likeNumber=50, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/oLAjfB7s1ib06iaV3D9Tg5Kzuf9u71gZhPYMiajRtENwicoAABeQtfXOlic8ibhYSy6DvJZWUDVtjRvTfqBffr1XJ6JLtFB5kHicthl/0, createdBy=d41c2055892, createdName=184****9840, createdTime=Tue May 30 09:17:02 CST 2017, time=2017-05-30, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204231, encodeId=89ad204231c5, content=学习了谢谢分享。, beContent=null, objectType=article, channel=null, level=null, likeNumber=58, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/oLAjfB7s1ib06iaV3D9Tg5Kzuf9u71gZhPYMiajRtENwicoAABeQtfXOlic8ibhYSy6DvJZWUDVtjRvTfqBffr1XJ6JLtFB5kHicthl/0, createdBy=08bb2061153, createdName=189****7206, createdTime=Tue May 30 06:38:12 CST 2017, time=2017-05-30, status=1, ipAttribution=), GetPortalCommentsPageByObjectIdResponse(id=204198, encodeId=67fd20419890, content=非常详细生动, beContent=null, objectType=article, channel=null, level=null, likeNumber=52, replyNumber=0, topicName=null, topicId=null, topicList=[], attachment=null, authenticateStatus=null, createdAvatar=https://wx.qlogo.cn/mmopen/ajNVdqHZLLDcohE5MKT1FYlH69x7qib45icLgBDMANxIiauEA62nuQI1eE8Ez2rO3yqOkRLH6ha6Lw0x1IBRm0N7g/0, createdBy=e1ff2033933, createdName=187****0626, createdTime=Tue May 30 05:47:25 CST 2017, time=2017-05-30, status=1, ipAttribution=)]
    2017-05-30 187****0626

    非常详细生动

    0

相关资讯

SAS作图是不真的又难又丑啊? 其实是你不知道

作者:谷鸿秋 数说君推荐语 作为一名统计狗,SAS的绘图一直是我的一个心病,之前也准备系统的写一些关于SAS绘图的文章,但….SAS的绘图实在让我提不起兴趣啊,直到我看到了这篇文章,才知道我之前那么天真,真是too simple!。 虽然SAS在商业统计分析软件领域一直是龙头老大,称王称霸多年,但如果说到统计绘图,那简直就是一个一直无法抹去的伤痛。 长久以来,SAS的统计绘图功能饱受诟病:

Handy-Weinberg平衡性检验的SAS实现案例

问题:如何计算handy-weinberg平衡性检验的卡方值?也就是表中的B项怎么计算的。 程序如下,基本思路是:gg gt tt是实际基因型频数,以此算出算出单个等位基因(g和t)的实际频率pg和pt,按照遗传平衡定律,可以分别算出 gg gt tt 的理论基因型频数agg agt att,然后按照拟合优度检验的方法来检验gg gt tt的实际基因型频数和理论基因频数的分布是否存在差异,卡方值

临床试验非劣效试验的样本量模拟计算SAS宏,以及PASS计算结果对比

例: 一种足癣标准治疗方案, 疗程结束时的真菌学清除率为80%。现提出一种相对简单的治疗方案, 为考察对比新方案与标准治疗方案的疗效, 拟设计一个非劣效性试验, 临床提出新方案的疗效达到标准方案清除率的90% 作为临床非劣效的相对标准, 即: 当新方案与标准方案在疗程末的真菌学相对清除率( 新方案P标准方案) 的95% 可信区间下限不低于0. 9时, 即可判断其临床非劣效于标准方案

郭继鸿:心电学2015四大新近进展

6月14日在河北沧州举办的第四届中国基层心血管病大会上,来自北京大学人民医院的郭继鸿教授针对心电学的4项最新进展做了精彩演讲,分别为Holter初筛睡眠呼吸暂停综合征、体表三维标测技术、遗传性心律失常新分类和假性心肌肥厚。一、Holter初筛睡眠呼吸暂停综合征(SAS)SAS是指在睡眠8h中,呼吸反复暂停,使人体发生系列病理生理改变的临床综合征。可分

利用SAS进行分层随机抽样

 抽样方法(Sampling Method)是按照一定程序,从所研究对象的全体(母体)中抽取一部份(样本)进行调查或观查,并在一定的条件下,运用数理统计的原理和方法,对母体的数量特征进行估计和推断。   抽样方法可分为随机抽样(亦称为机率抽样 Probability Sampling)和非随机抽样(亦称为非机率抽样 Non-Probability Sampling) 两大类,这两类的抽样方