-
- Format 过程创建的格式可以在之后的变量联系起来。该过程以PROC FORMAT开始,继而跟着一个或多个value语句:
- PROC FORMAT;
- VALUE name range-1="formatted-text-1";
- range-2="formatted-text-1";
- ...
- range-n="formatted-text-1";
- 在value语句中的name,就是要创建格式的名字。若格式是针对字符型数字,必须在name前加上$。name命名不能超过32个字符(包括$),不能以数字开始或结尾,除下划线_的任何特殊字符。之外,name不能和已存在的格式的名字。每个range指变量中想用等号右边的格式表示的值或范围。下面是一些正确的范围定义的例子:
- "A"="Asia"
- 1,2,3,4="odd"
- 50000-high="not affordable"
- 13-<20="teenager"
- other="bad data"
- 字符型变量值必须以引号引用,如果不知一个变量值,那么用","把数值分析或用"-"表示一连续的范围。在一个范围用到的关键词low及high表示该变量无缺失值的最小值或最大值。也可用"<"来表示不包括范围的结尾值。其他的关键词可以用到指定某个格式,在这里并未一一列出。
-
- 举例:宇宙汽车公司为了调查消费者对汽车颜色的喜好情况开展了一项调查。他们已经拥有消费者的信息,如年龄,性别(编码男性表示为1,女性表示为2),年收入及喜好汽车颜色(黄色,灰色,蓝色或白色),下面是数据:
- 19 1 14000 y
- 45 1 65000 g
- 72 2 35000 b
- 31 1 44000 y
- 58 2 83000 w
- 下面的程序利用PROC FORMAT过程读取数据,创建年龄,性别及汽车颜色的格式,然后用该格式打印出来:data carsurvey;
- input age sex income color$@@;
- cards;
- 19 1 14000 y
- 45 1 65000 g
- 72 2 35000 b
- 31 1 44000 y
- 58 2 83000 w
- ;
- run;
- proc format;
- value gender 1="男性"
- 2="女性";
- value agegroup 13-<20="teen"
- 20-<65="adult"
- 65-high="senior";
- value $col "w"="moon white"
- "b"="sky blue"
- "y"="sunburst yellow"
- "g"="rain cloud gray";
- run;
- proc print data=carsurvey;
- format sex gender. age agegroup. color $col. income dollar8.;
- title"Survey results printed with user-defined formats";
- run;
Survey results printed with user-defined formats |
Obs age sex income color 1 teen 男性 $14,000 sunburst yellow 2 adult 男性 $65,000 rain cloud gray 3 senior 女性 $35,000 sky blue 4 adult 男性 $44,000 sunburst yellow 5 adult 女性 $83,000 moon white



雷达卡




京公网安备 11010802022788号







