Jiangtang's profile技止于此BlogListsNetwork Tools Help

Blog


    1/22/2008

    用SAS读入原始数据(2):PC文件

    *****************************************************

    用SAS读入原始数据(1):文本文件

    用SAS读入原始数据(1):文本文件(续)

    ****************************************************

    引例:读入.csv文件(带逗号分隔的文本文件)

    用SAS读入原始数据(1):文本文件提到过后缀名为.csv的文件(带逗号分隔的文本文件),可以采用列举输入模式读入。比如下面的数据,第一行是变量名,另两行是记录,你把它贴到记事本,然后另存为.csv格式,比如叫a.csv,就存于C:\:

    Name,Job,Department,PhoneNum
    "Brown, Ann",112,Publications,2587
    "Krueger, John",,Sales,3456

    利用带修饰的列举输入模式,可以这么读入:

    data a1;
        infile 'C:\a.csv' firstobs=2 dsd;
        input Name:$ 12. Job Department:$12. PhoneNum;
    run;

    由于源文件是以逗号为分割符,但逗号同时是Name这一字段的分隔符,所以infile语句后面加了一个dsd,见《用SAS读入原始数据(1):文本文件(续)》

    幸运的是,.csv同时也是SAS支持的所谓PC Files之一。对所有的PC数据文件,读入语句看上去是这样的:

    proc import
        datafile='C\a.csv'
        out=a2
        dbms=csv replace;
    run;

    上面两段脚本具有同样的功效。

    PC文件及其DBMS标识

    proc import的一般语句为:

    proc import
        datafile='文件名及盘符'
        out=自定义的数据集名
        dbms=DBMS标识符 replase;
        <其他可选项>;
    run;

    上面dbms标识符就是SAS系统能识别的PC文件标志。你可以在SAS-File-Import Data-Standard Data Sourse中找到PC文件的下拉列表。几个常用的有:

    DBMS标识符 后缀 描述
    Excel2000 .xls Microsoft Excel 97, 2000 or 2002 Workbook
    csv .csv Comma Separated Values (*.csv),分隔符为逗号
    Access .mdb Microsoft Access 97 Database

    proc import语句比较简单,而且可以通过菜单方式生成代码。

    例2:读入Excel文件

    假设D盘下一个叫a1.xls的Excel文件,其中第一行为变量名,其余为观测值,另一个a2.xls与a1.xsl的区别仅在于a2.xls的第一行就是观测值。相应的读入语句为:

       

    proc import
        out=a1
        datafile="D:\a1.xls"
        dbms=excel2000 replace;
    run;

    proc import
        out=a2
        datafile="D:\a2.xls"
        dbms=excel2000 replace;
        getnames=no;
    run;

    例3:读入Access数据库文件

    假设D盘下一个不设密码的Access数据库文件,叫tianxdata.mdb,我们要读入其中一张叫stockclose的表,以下代码是自明的:

    proc import
        out=tianx.stock
       datatable="stockclose"
        dbms=access2000 replace;
       database="D:\tianxdata.mdb";
    run;

    *****************************************************************************

    参考资料:

    1. 汪嘉冈《SAS V8基础教程》,北京:中国统计出版社,2001
    2. 朱世武《SAS编程技术与金融数据处理》,北京:清华大学出版社,2003
    Technorati Tags:

    Comments (2)

    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[ghfdhdhfbdbbeee]
    Nov. 21
    Nov. 9

    Trackbacks

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