SAS:(4)輸出入相關敘述句:INFORMAT、FORMAT、LIST、OUTPUT等敘述句

輸入相關

INFORMAT 敘述句

輸入格式的作用在於用來讀取特定形式的資料,在以informat敘述句宣告想要抓取的變數格式之後,input敘述句就可以使用列名輸入法,informat敘述句的使用方法有兩種:

一般用法

informat var1 [informat1] var2 [informat2] var3 [informat3];
input var1 var2 var3;

注意:當變數為文字常數或數值整數時,要加上"."符號,以表格式。
範例:

informat name 8. age 2. sex 1.

預設格式的用法

informat DEFAULT=[Defualt informat];
input var1 var2 var3;

注意:DEFAULT是用來宣告預設的格式,但如果敘述句中有對變數另行格式的宣告,則以另行宣告的格式優先。

informat DEFAULT=[Defualt informat] var1=[informat1];
input var1 var2 var3;

輸出相關

FORMAT 敘述句

當不想修改原始資料,又想將資料的呈現做適當的變化,就會使用FORMAT敘述句,使用方法有兩種:

在Proc Step的敘述句之後使用

範例:

Proc print data=mydata
format money dollar.;

說明:

SAS要Print mydata這個資料集,而Format對變數money開頭加上Dollar sign. 注意:要在字尾加上"."以宣告其為格式設定。

搭配Proc Format和value敘述句使用

當變數之中有許多選項時使用,如滿意度調查:非常滿意、滿意、普通….etc.,當遇到大量的滿意度問題時相當方便,使用方式如下:

proc format;
value 格式定義標籤 1="文字標籤" 2="文字標籤" 3="文字標籤" 4="文字標籤";
data;
input var1;
format var1 格式定義標籤;

格式定義標籤為使用者自行定義
注意:格式定義標籤不可以使用數字
範例:

poc format;
value stat 1="非常不滿意" 2="不滿意" 3="沒意見" 4="滿意" 5="非常滿意";
data
input name age statify
format statify stat.;

應用題:

以會計(x1)、統計(x2)、微積分(x3)三門課的成績算加權平均值(x4),平均未及格者,以***表示
會計2學分、統計3學分、微積分4學分

作法:

proc format;
value woo .="***"
data dataset1;
input x1 x2 x3;
x4=(x1*2+x2*3+x3*4)/9;
if x4>=60 then x4=(x1*2+x2*3+x3*4)/9;
if x4<60 then x4=.;
output;
cards;

;
proc print data=dataset1;
format x4 woo.;
run;

proc format敘述句的特性

  • 假如有一變數,出現在多個format敘述句時,則以最後一個format敘述句所宣告的格式為準。
  • 如Data step已有宣告格式,在PROC Step仍能以另一個FORMAT敘述句來替代。
  • 取消輸出變數的格式有以下兩種方法:
    1. 在format敘述句中給予變數,但卻不給予任何的"格式定義標籤"
    2. 使用ATTRIB敘述句

LIST敘述句

將系統正要處理的原始資料顯示在Log視窗或.log檔案,且顯示原始資料時會同時列出一個具有欄位刻度的尺規。

syntax

data;
input …;
LIST;

OUTPUT

在DATA STEP,Output敘述句現行的資料,寫到欲建立的資料集中。通常有以下幾種作用:

  1. 由一筆資料來產生數筆輸出的資料
  2. 由一個資料檔或資料集的資料來產生數個資料集

syntax

output 資料集名;

範例1:

data out1;
set mydata.timshan;
drop name address telephone;
output;

範例2:

data out2 out3;
set mydata.timshan;
if sex="F" then output out2;
else output out3;

下一堂課:Title、Footnote、Option敘述句的介紹

延伸閱讀

The FORMAT Procedure http://support.sas.com/onlinedoc/913/getDoc/zt/proc.hlp/a000063536.htm
PROC FORMAT Statement http://support.sas.com/onlinedoc/913/getDoc/zt/proc.hlp/a002473464.htm

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License