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.formats和library.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;

时间: 2024-07-31 18:34:59

sas高级编程(3)format过程,管理format、永久使用format给指定variable、控制format搜索顺序、fmterr、利用数据集创建format,由format创建数据集、制表过程的相关文章

sas高级编程(1) 定点抽样、随机抽样、三种索引、管理索引

1:抽样 1.1 定点抽样 data sasuser.subset; do pickit=1 to totobs by 10; set sasuser.revenue point=pickit nobs=totobs; output; end; stop; run; 总体样本数量未知的情况下,使用set语句的nobs=选项获得总体观测行数,总体观测行数是在编译时确定的,并在创建pdv的时候被写入,存在文件描述部分中. point=n 表明要抽选第n条观测值,n必须为变量不能为常量. 为什么要使用

软件项目与过程管理第八周作业

内容:软件项目与过程管理课程内容总结 经过八周时间的学习,软件项目与过程管理课程已经逐渐接近了尾声.通过这八周的学习,我对软件项目与过程管理课程有了更深的理解. 一.关于团队项目. 团队项目是本次软件项目与过程管理课程中最重要的一部分.我们团队项目是作业管理系统.在项目开发的整个过程中,我们在项目经理的带领下,项目团队的每一个成员团结合作.相互沟通,团队成员之间相互学习彼此的优点和技术,在每个成员的共同努力下,基本完成了此次软件开发项目. 通过这次团队项目, 我的总结如下: 1.在项目的开发过程

Hbase合并Region的过程中出现永久RIT的解决

在合并Region的过程中出现永久RIT怎么办?笔者在生产环境中就遇到过这种情况,在批量合并Region的过程中,出现了永久MERGING_NEW的情况,虽然这种情况不会影响现有集群的正常的服务能力,但是如果集群有某个节点发生重启,那么可能此时该RegionServer上的Region是没法均衡的.因为在RIT状态时,HBase是不会执行Region负载均衡的,即使手动执行balancer命令也是无效的. 如果不解决这种RIT情况,那么后续有HBase节点相继重启,这样会导致整个集群的Regio

《Objective-C高级编程》の内存管理の学习笔记

此日志用于记录下学习过程中碰到的问题 <Objective-C高级编程> 人民邮电出版社  是一本写的很棒的书,日本作者对于细节抠的很仔细,深入浅出,推荐学习Objective-C的同学们购买.   #1 关于retainCount 变量在内存中是如何存放的? 在GNUStep(一种cocoa框架的互换框架)中retainCount和对象放置在一起,在对象地址之前.所以有如下获取GNUstep中retainCount的方式: -(NSUInteger) retainCount { return

《Objective-C 高级编程》干货三部曲(一):引用计数篇

总结了Effective Objective-C之后,还想读一本进阶的iOS书,毫不犹豫选中了<Objective-C 高级编程>: 这本书有三个章节,我针对每一章节进行总结并加上适当的扩展分享给大家.可以从下面这张图来看一下这三篇的整体结构: 注意,这个结构并不和书中的结构一致,而是以书中的结构为参考,稍作了调整. 本篇是第一篇:引用计数,简单说两句: Objective-C通过 retainCount 的机制来决定对象是否需要释放. 每次runloop迭代结束后,都会检查对象的 retai

UNIX环境高级编程---标准I/O库

前言:我想大家学习C语言接触过的第一个函数应该是printf,但是我们真正理解它了吗?最近看Linux以及网络编程这块,我觉得I/O这块很难理解.以前从来没认识到Unix I/O和C标准库I/O函数压根不是一码事.Unix I/O也叫低级I/O,也叫Unbuffered I/O,是操作系统内核部分,也是系统调用:而C标准I/O函数相对也成Buffered I/O,高级I/O,一般是为了效率考虑对这些系统调用的封装.以前使用getchar()经常为输入完后的回车而出错.那是不理解标准I/O实现时的

(七) 一起学 Unix 环境高级编程(APUE) 之 进程关系 和 守护进程

. . . . . 目录 (一) 一起学 Unix 环境高级编程(APUE) 之 标准IO (二) 一起学 Unix 环境高级编程(APUE) 之 文件 IO (三) 一起学 Unix 环境高级编程(APUE) 之 文件和目录 (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息 (五) 一起学 Unix 环境高级编程(APUE) 之 进程环境 (六) 一起学 Unix 环境高级编程(APUE) 之 进程控制 (七) 一起学 Unix 环境高级编程(APUE) 之 进程关系

C#高级编程(第8版)——委托声明、使用(第一节)

首先,声明一个Currency的结构.Currency结构有自己的ToString()重载方法和一个与GetCurrencyUnit()的签名相同的静态方法.这样,就可以用同一个委托变量调用这些方法了: struct Currency { public uint Dollars; public ushort Cents; public Currency(uint dollars, ushort cents) { this.Dollars = dollars; this.Cents = cents

Unix 环境高级编程 (APUE) 之 网络 IPC:套接字

一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字 . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录 (四) 一起学 Unix 环境高级编程 (APUE) 之 系统数据文件和信息 (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境 (六) 一起学 Unix 环境高级编程 (APU