zoukankan      html  css  js  c++  java
  • sas高级编程(3)format过程,管理format、永久使用format给指定variable、控制format搜索顺序、fmterr、利用数据集创建format,由format创建数据集、制表过程

    /***************************************************格式过程************************************************/

    PROC FORMAT <option(s)>;

    EXCLUDE entry(s);
    INVALUE <$>name <(informat-option(s))>
    value-range-set(s);
    PICTURE name <(format-option(s))>
    value-range-set-1 <(picture-1-option(s) )>
    <…value-range-set-n<(picture-n-option(s))>>;
    SELECT entry(s);
    VALUE <$>name <(format-option(s))>
    value-range-set(s); 
    主要功能:使用输出格式重编码变量
    PROC FORMAT Statement:Define formats and informats for variables
    VALUE <$>name <(format-option(s))>  :names the format that you are creating(给自己的格式起一个名字)
    名字的限制:A numeric format name can be up to 32 characters in length. A character format name can be up to 31 characters in length. If you are creating a character format, then use a dollar sign ($) as the first character.
    value-range-set(s):specifies the assignment of a value or a range of values to a formatted value
    格式为:value-or-range-1<..., value-or-range-n>= | [existing-format] 
    The variable values on the left side of the equal sign prints as the character string on the right side of the equal sign(等式右边的格式将作为等式左边的格式的替换输出)
    几个特殊关键字
     You can use the keyword OTHER= as a single value. OTHER matches all values that do not match any other value or range
    You can use LOW or HIGH as one value in a range, and you can use the range LOW-HIGH to encompass all values.(high,low指定数据集中的最大值和最小值,不包括缺失值)
    0<-100  小于号靠近哪端,哪端就不被包括在内,'-'(表示等于号)一定要在low或high端
    在proc中使用格式语句,格式只在该过程有效,同时,无论在过程步中使用还是data步中使用,变量最初的值并没有改变
    PROC FORMAT library=libraryPath;  *定义的格式的储存位置;
    VALUE fsmt low-<60='C' 60-<80='B' 80-100='A'; *数值格式的转换,fsmt是格式码,小于60分输出c,后面的类似;
    value $sexf '1'='male' '2'='female'; *字符格式的转换;
    value color 0,4-9 = 'other color'; *离散变量的格式表示方式;
    PROC PRINT DATA=ep.score; FORMAT t1-t3 fsmt.; *将t1-t3变量按照fsmt的格式输出,也就是见数值转化为相应的字母,格式后面打点才能正确输出,打点是为了区别变量名和格式名; run;

    注意:如果有两个区域1-3 3-5 那么3是包括在第一个区域中
    小变化
    1.1:Creating a Format with Overlapping Ranges 
    要显示出来多个分类,用MLF选项
    在tabulate means summary几个过程中支持
    /*允许输出逇一行包含几个分类*/

    VALUE format-name (MULTILABEL); proc format; value dates (multilabel) '01jan2000'd - '31mar2000'd = '1st Quarter' '01apr2000'd - '30jun2000'd = '2nd Quarter' '01jul2000'd - '30sep2000'd = '3rd Quarter' '01oct2000'd - '31dec2000'd = '4th Quarter' '01jan2000'd - '30jun2000'd = 'First Half of Year' '01jul2000'd - '31dec2000'd = 'Second Half of Year'; run;
    1.2:picture format
    格式基本不变,用关键字picture代替了value
    PROC FORMAT;
        PICTURE format-name
        value-or-range='picture';
    RUN;

    picture的意思:specifies a template for formatting values of numeric variables,模板是一个单引号内的字符串,最大长度为40

    这种格式化有什么优势?

    针对于11223344这种数据格式,如果要将其转化为(11)22-33-44,用模板来做,十分简单,value达不到想要的效果

    关于这种格式的三种selector

    这里举两个例子说明

    proc format;
    picture rainamt
    0-2='9.99 slight'
    2<-4='9.99 moderate'
    4<-<10='9.99 heavy'
    other='999 check value';
    run;
    data rain;
    input Amount;
    datalines;
    4                4.00
    3.9              3.90
    20               020
    .5               0.50
    6                6.00
    ;
    run;

    解释输出结果
    所有的输出格式必须与单引号内的输出格式相对应
    比如4 对应4.00 后面为什么是0,因为sas规定,如果单引号中'xxx slight',如果x不为0(x不必一样,且x为1-9),那么在值对应不到的位置全部赋为0
    如果x为0,那么在对应的位置,值不变,不对应的位置全部赋为空 01 对应 00变为 1
    Directives are special characters that you can use in the picture to format date, time, or datetime values
    能使得在输出时使用日期格式
    标准格式如下
    PICTURE format-name
    value-or-range='picture' (DATATYPE=SAS-date-value-type);
    
    proc format;
    picture mydate
    low-high='%0d-%b%Y  ' (datatype=date);    /*注意红色字的长度要和输出结果集中的长度一致,否则sas会进行阶段,这里输出占10个格子,所以在写的时候后面就要留两个空格*/
    run;

     2:管理format

    2.1:format过程进行管理 select exclude,选择性的显示需要显示的,logical操作

    /*列出你选择的库中的所有格式的具体描述形式,可以选择某些和排除某些*/
    PROC
    FORMAT LIB=library FMTLIB; SELECT format-name; EXCLUDE format-name; RUN;
    /*在输出的结果中<在哪边就是不包括哪*/

    2.2:catalog过程管理,可以拷贝,删除,physical&logical操作

    PROC CATALOG CATALOG=libref.catalog;
        CONTENTS <OUT=SAS-data-set>;
        COPY OUT=libref.catalog <options>;
        SELECT entry-name.entry-type(s);
        EXCLUDE entry-name.entry-type(s);
        DELETE entry-name.entry-type(s);
    RUN;
    QUIT;
    9968  proc catalog catalog=work.formats;
    9969      copy out=renmin.formats;
    9970      select Me.format;
          select x.formatc; /*字符型后面加c*/
    9971 run; NOTE: 正在将条目“ME.FORMAT”从目录“WORK.FORMATS”复制到目录“RENMIN.FORMATS”。 9972 quit;

     3:永久format

    PROC DATASETS LIB=SAS-library <NOLIST>;
        MODIFY SAS-data-set;
        FORMAT variable(s) format; /*不规定format则为取消当前的format*/
    QUIT;
     4:控制format搜索顺序
    默认情况下sas搜索work.formats、library.formats
     如果想要自己规定后面的搜索顺序,那么要将自己写好的catalog写在后面
    OPTIONS FMTSEARCH= (catalog-1 catalog-2...catalog-n);
    注意:如果只写库名,那么sas只会在库中的默认文件夹formats下搜索,库名和文件名都写则会在自己规定的文件夹搜索
    如果没包括work.formatslibrary.formats(一定要全部包含才能改变默认顺序),那么sas还是会先搜索这两个再搜索你自己规定的,想要提前搜索,自己规定的文件就要将这两个写入函数中
     5:指定format error的输出信息
     
    6:利用数据集中的数据创建格式,很明了,就是读入数据集中的数据而不用再手动指定格式,对于需要很多格式的很大的程序应该适用,将格式和数据集结合后就可以很方便的对格式添删数据,cntlin/cntlout=后面跟的都是数据集,
    library可简写为lib,省略即为保存在work中
    PROC FORMAT LIBRARY=libref.catalog CNTLIN=SAS-data-set;
    libref.catalog is the name of the catalog in which you want to store the format
    
    SAS-data-set is the name of the SAS data set that you want to use to create the format.

    使用的数据集要用相应的变量才能进行使用,必须有的变量为FmtName Start Label,可选的为end,如果是字符型还需要加上type。变量的种类不能超过这些

    由format创建数据集
    PROC FORMAT LIBRARY=libref.catalog CNTLOUT=SAS-data-set;
        SELECT format-name format-name. . . ;
        EXCLUDE format-name format-name. . . ;
    RUN;

    proc format lib=library.formats cntlout=sasuser.runs;
     
    /***************************************************制表过程************************************************/
     
    制表过程
    PROC TABULATE <option(s)>;
    BY <DESCENDING> variable-1
    <…<DESCENDING>variable-n>
    <NOTSORTED>;
    CLASS variable(s) </ options>;
    CLASSLEV variable(s) /
    STYLE=<style-element-name | PARENT>
    <[style-attribute-specification(s)] >;
    FREQ variable;
    KEYLABEL keyword-1='description-1'
    <…keyword-n='description-n'>;
    KEYWORD keyword(s) /
    STYLE=<style-element-name | PARENT>
    <[style-attribute-specification(s)] >;
    TABLE <<page-expression,>row-expression,> column-expression</ table-option(s)>; *常用;
    VAR analysis-variable(s)</ options>;
    WEIGHT variable; 
     
    table语句中用到的四种运算符
    *:creates categories from the combination of values of the class variables and constructs the appropriate headings for the dimension
    blank:places the output for each element immediately after the output for the preceding element. This process is called concatenation(横向链接)
    ():group elements and associate an operator with each concatenated element in the group
    <>:specify denominator definitions, which determine the value of the denominator in the calculation of a percentage(其中置放分母)
    table语句中也可以放入sas默认的一些统计量,例如n(非缺失值个数),Max,min等等和class中的变量进行混合计算。
    data test;
    input a b @@;
    cards;
    1 1 2 1 2 2 1 3 2 3 2 1 1 2 2 1 1 3 2 3
    ;
    proc tabulate;
    class a b;
    table b*a b*a a b; *相乘表示维度联合,单独的表示单个层级;
    run;

     
    options linesize=200;
    data test;
    input a b c @@;
    cards;
    1 1 4 2 1 5 2 2 4 1 3 6 2 3 5 2 1 6 1 2 4 2 1 6 1 3 5 2 3 5
    ;
    proc tabulate;
    class a b c;
    table a*b a*c a*(b c) a,b*c; *这里不能用简单的乘法考虑,两个相乘的要考虑成两个层级的概念!!;
    run;

    options linesize=200;
    data test;
    input a b c @@;
    cards;
    1 1 4 2 1 5 2 2 4 1 3 6 2 3 5 2 1 6 1 2 4 2 1 6 1 3 5 2 3 5
    ;
    proc tabulate;
    class a b c;
    table a all,b*c*f=3.1/rts=5; * *f=3.1单独看为格式的表示方式 all表示一个汇总;
    run;

     
     
     
     
     
     
     
     
  • 相关阅读:
    镜像的上传和下载
    ps 命令
    过滤不合格数据
    云计算5-3-2法则
    Python Django初入门
    python web框架
    BootStrap、EasyUI、JQueryUI
    JS正则
    ngonx FastCGI 相关参数调优
    Windows10远程连接错误-出现身份验证错误
  • 原文地址:https://www.cnblogs.com/yican/p/4054669.html
Copyright © 2011-2022 走看看