/*FORMAT语句*/
1 FORMAT Profit Loss DOLLAR8.2 Saledate MMDDYY8.; 2 PUT Profit DOLLAR8.2 LOSS DOLLAR8.2 Saledate MMDDYY8.;
FORMAT语句指定每个变量具体的格式;
这里特别要注意的是,FORMAT语句中,指定Profit和Loss同一个格式,为DOLLAR8.2.
FORMAT过程
1 DATA Carsurvey; 2 INFILE 'c:myrawdatacars.dat'; 3 INPUT Age Sex Income color$; 4 PROC FORMAT; 5 VALUE gender 1='Male' 6 2='Female'; 7 Value agegroup 13-<20='Teen' 8 20<-65='Adult' 9 60-High='Senior'; 10 Value $ color 'W'='Moon White' /*请注意此处$的位置*/ 11 'B'='SKy Blue' 12 'Y'='Sunburst Yellow' 13 'G'='Rain cloud Gray'; 14 PROC PRINT DATA=Carsurvey; 15 FORMAT Sex gender. Age agegroup. color $ col. Income Dollar8.; 16 TITLE '...'; 17 RUN;
备注:
1.格式名要求:
- 包括$在内,不能超过32个字符;
- 不能以数字开头或结尾;
- 不能包含下划线以外的字符;
- 不能使已经存在的格式的名字。
2. Value后面的Range的要求:
- 字符型变量的值必须放在引号内;
- 若要复制的range不止一个,可用逗号将他们隔开或用连号(-)表示连续的范围;
- 关键词LOW和HIGH表示变量的最大值和最小值;
- 可用关键词OTHER给VALUE语句中未分配的值分配格式.
利用FORMAT过程来Grouping数据
1 DATA Books; 2 INFILE 'C:MyRawDataLibraryBooks.dat'; 3 INPUT Age BookType $ @@; 4 RUN; 5 6 *Define formats to group the data; 7 PROC FORMAT;/*定义一个新的FORMAT*/ 8 VALUE agegap 9 0-18 = '0 to 18' 10 19-25= '19 to 25' 11 26-49 ='26 to 49' 12 50-HIGH= '50+'; 13 VALUE agegpb 14 0-25 = '0 to 25' 15 26-HIGH= '26+'; 16 VALUE $ typ 17 ‘bio','non','ref'='Non-Fiction' 18 'fic','mys','sci'='Fiction'; 19 RUN; 20 21 *Create two way table with Age grouped into four catefories; 22 PROC FREQ DATA=books; 23 TITLE 'patron age by book type: four age group'; 24 TABLES BookType*Age/NOPERCENT NOROW NOCOL; 25 FORMAT Age agegap. BookType $ typ.;/*引用自定义的格式*/ 26 RUN; 27 28 *Create two way table with Age grouped into two catefories; 29 PROC FREQ DATA=books; 30 TITLE 'patron age by book type:Two age group'; 31 TABLES BookType*Age/NOPERCENT NOROW NOCOL; 32 FORMAT Age agegab. BookType $ typ.;/*引用自定义的格式*/ 33 RUN;