Jiangtang's profile技止于此BlogListsNetwork Tools Help

Blog


    10/30/2007

    SAS备忘录:format和informat

           被问到informat和format的用法和区别。都是关于变量属性的,以前讲过informat影响到SAS输入数据的格式,而format影响到数据的输出格式。还是不直观,说个能操作的简单例子。

           先说format。format影响到SAS的数据输出格式,这个我们举了个例子,输出当前时间:


    data time;
    x=date();
    format x yymmdd8.2;
    put x=;
    run;


          如果没有format那行,那么SAS系统就会把当前时间输出为一个数字(因为SAS就是用数字存储日期的),format就是指定x的输出格式,以便于我们阅读。


          format是改变输出格式,而informat则是关于SAS的读入格式。举个例子,假如你的D盘有个文本数据文件informat.txt,存的是一个时间变量time,有两个观测值:


    time
    1998-7-10
    1998-7-11

          你要是这么读入它,就会出错:


    data a;
    infile "d:\informat.txt" firstobs=2; 
    input time;
    run;


           先解释一下这段。这是一个标准的读入数据的程序,infile "d:\informat.txt"指明文件路径,后面的firstobs=2表明数据从第二行开始读入(因为第一行是变量名time)。如果是数值型的变量,这一定没问题,但这个time的格式是1998-7-11,SAS就读不了,如果不指定time的读入格式。
           规定读入格式,就要用到informat了,下面的程序就能正确读入informat.txt了:


    data aa;
    infile "d:\informat.txt" firstobs=2;
    input time;
    informat time yymmdd10.;
    run;


            informat time yymmdd10.就规定了time的读入格式,SAS系统就知道它存的是时间,就读进来了。读是读进来了,但这是SAS显示的却也是数字。为了让输出我们能明白,就要用到format,改变输出格式,完整的程序如下:

     
    data aaa;
    infile "d:\informat.txt" firstobs=2;
    input time;
    informat time yymmdd10.;
    format time yymmdd10.;
    run;

    Technorati Tags: , ,

    Comments (6)

    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[ghfbgiffgjbjbgi]
    Nov. 21
    Nov. 9
    Nov. 3
    Jiangtang Huwrote:
    我把示例代码附上吧,也贴在/mysas.net/forum:

    data time;
    input x datetime18.;
    format x datetime18.;
    cards;
    29nov2007:09:25:00
    03dec2007:10:23:50
    ;

    data time2;
    set time;
    y=timepart(x);
    format y time8.;
    run;
    Jan. 16
    Jiangtang Huwrote:
    SAS有几个时间函数,你可以试试。DHMS(date,hour,minute,second),或者HOUR()、HMS(.)。或者你就可以直接截断那个日期29nov2007:09:25:00,只从11列开始读入,11-18正好是09:25:00
    Jan. 12
    Picture of Anonymous
    (没有名字) wrote:
    你好,我在网上看到你这篇日志,我刚开始学习sas编程,想请教你一个问题:数据集a中包含一个变量,变量名为dtime,观测值均为29nov2007:09:25:00 ,03dec2007:10:23:50 这种格式,如何将观测中的日期,比如29nov2007和03dec2007去掉呢?仅仅保留时间,如09:25:00,10:23:50?谢谢!
    Jan. 11

    Trackbacks

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