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;

 

 

 

 

 

 

 

 

 

 
 
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM