2016-11-21 33 views
-1

我試圖使用參數表格作爲sas格式。我嘗試了很多方法,但其中任何一個都可行。我的想法是。如何在sas宏中引用另一個表格

  1. 使用proc格式。我不確定是否可以使用數據集中的兩個特定列來構建SAS格式。

  2. 構建一個SAS格式的宏,它的格式與格式類似。

    %macro test(var1=); 
    %GLOBAL &var_aux; 
    proc sql noprint; 
    Select trasformated_value into :&var_aux 
    from parametric_table 
    where original_value=&var1.; 
    var1=&var_aux; 
    quit; 
    drop &var_aux; 
    %mend; 
    

與宏的問題是,我不知道如何將一個值返回到原來的查詢。

data transformation; 
set transformation; 
New_value = %test(old_value); 
run; 
+0

不確定爲什麼選票很近,這個問題很清楚。如果您對語言一無所知,請不要投票結束,這不是一個絕對明確的案例。 – Joe

回答

1

你需要編寫一個函數式的宏,如果你做你上面做什麼,這是不很容易做到(可能的,但不是充分的容易成爲值得做的事情)。

宏不是天生的功能,它們只是您在其他地方編寫和重複使用的代碼。所以你不能真的在等號的右邊調用一個宏,除非它包含的唯一的非宏代碼在等號的右邊是有效的代碼。

在這種情況下,我同意format作爲解決方案。您需要使用proc format中的cntlin選項,並且您需要在運行數據步驟之前創建該格式。最少需要fmtname,start,label。通常也包括type。對於「其他」,hlo="o"行也是一個好主意。

data for_fmt; 
    set parametric_table; 
    retain fmtname 'PARAMETF' type 'N'; *or 'C' and include a $ if character; 
    rename 
    original_Value = start 
    transformed_value = label 
    ; 
    output; 
    if _n_=1 then do; 
    hlo='o'; 
    call missing(original_value); 
    transformed_value = .; *or ' ' or whatever you want a non-match to return; 
    output; 
    end; 
run; 

proc format cntlin=for_fmt; 
quit; 

確保你沒有重複start值,但除此之外,這是你最好的辦法爲你描述。然後你有

data transformation; 
    set transformation; 
    new_value = input(put(old_value,PARAMETF.),BEST12.); *or whatever depending on what you are doing. Format makes CHAR value always, so `input` to convert to number.; 
run; 

這裏沒有宏需要,雖然你當然可以(而且,我應該說)應該有一個通用的宏來創建這樣的格式。

+0

我想你在'if _n_ = 1 then'; block中缺少'output;'語句。 – Quentin

+0

謝謝昆汀和喬。你的回答對我來說真的很有用。我無法在網上找到任何與此相同的解釋。 – diagonal

+0

隨意更改爲標題或文字,以便更清楚地幫助其他人。我知道我的英文不好,所以很難找到合適的單詞。再次感謝你! – diagonal

相關問題