2012-02-15 41 views

回答

2

你可以這樣做沒有格式:

data test; 
    monthtext="January"; 
    month=month(input("01"||substr(monthtext,1,3)||"2000",date9.)); 
run; 
4

你可以讓自己的格式:

options fmtsearch=(work); 

proc format; 
invalue MonNum 
    JANUARY = 1 
    FEBRUARY = 2 
    ; 
run; 

data Month; 
length month $10; 
input Month $; 
month=upcase(month); 
monthnum=input(month,monnum.); 
datalines; 
    January 
    February 
    ; 
Run; 

Proc report data=work.month nowd; 
column month monthnum; 
run; 
0

這取決於你將如何在你的代碼中使用此。如果您需要在代碼的多個部分重複此映射,那麼我會建議使用PROC FORMAT創建自定義格式,該格式可用於數據步驟或其他過程。如果您只是在一個數據步驟中執行此映射,那麼您可以使用SELECT/WHEN或IF/ELSE IF邏輯來執行相同操作。在SAS中還有很多其他的方法可以實現,但我認爲這兩種方法是最直接的。

1

使用輸入函數將字符類型轉換爲數字類型的方法。但我會同意創建自定義格式更好。

data test;  
    input monthchar $15.; 
    datalines; 
    December 
    January 
    March 
    ; 
    run; 

data test; 
    set test; 
    monthnum=month(input(cats(1,substr(monthchar,1,3),2000),date9.)); 
    run; 
相關問題