Jiangtang's profile技止于此BlogListsNetwork Tools Help

Blog


    11/7/2007

    答客问:SAS对列求和

    SAS Base自带的累加函数SUM,虽然有漂亮的缩写方式如SUM(of x1-x100000),但几乎没有什么应用价值(除了在数组array下),因为SUM函数是对行求和,而在通常的二维表中,我们经常需要对列求和。这个问题用proc SQL过程最为方便,还有一个简单的替代方案就是在proc print步下使用SUM选项。比如,对count求和:

    data a;
        input ID sex $ count;
    cards;
    1 M 3
    2 F 4
    3 M 5
    4 F 5
    ;
    proc print data=a;
        sum count;
    run;

    结果显示:

    Obs    ID    sex    count

     1      1     M        3
     2      2     F        4
     3      3     M        5
     4      4     F        5
                        =====
                          17

    如果想按sex分组显示,则先对sex排序:

    proc sort data=a;
        by sex;
    run;
    proc print data=a;
        sum count;
        by sex;
    run;

    结果显示:

    --------------------------------------------- sex=F -----------------------------------------------

                                             Obs    ID    count

                                               1     2      4
                                               2     4      5
                                             ---          -----
                                             sex            9

    ---------------------------------------------- sex=M -----------------------------------------------

                                             Obs    ID    count

                                               3     1       3
                                               4     3       5
                                             ---          -----
                                             sex             8
                                                          =====
                                                            17

    当然,你可以让结果更紧凑些,比如在proc print中的by sex下面再加一个id sex,看起来就是这样:

    sex    ID    count

     F      2       4
            4       5
    ---          -----
     F              9

     M      1       3
            3       5
    ---          -----
     M              8
                 =====
                   17

    Technorati Tags: , ,

    Comments (4)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    您需要二手液晶显示屏废旧液晶屏么?我们是不折不扣的二手液晶屏、旧液晶屏大批发商,长期大量供应可再利用的旧液晶屏。我公司提供的各种尺寸的二手液晶屏, 不同厚薄如笔记本屏,均已经过我们严格的分类,检验,测试流程。请访问协力液晶屏www.sceondhandlcd.com[ghfbieeedfacaja]
    Nov. 21
    Nov. 9
    Nov. 3
    Ke Xijianwrote:
    非常感谢您这篇文章。本人是学医的,但需要用到SAS软件,恰巧在网上搜索到你的文章。正好帮助我解决了问题。非常感谢。有机会在向你学习下其他的SAS知识。
    June 14

    Trackbacks

    The trackback URL for this entry is:
    http://johnthu.spaces.live.com/blog/cns!2053CD511E6D5B1E!330.trak
    Weblogs that reference this entry
    • None