2011-11-22 6 views
5

Windows上の別のプログラムからSASプログラムを呼びたいと思っています。コマンドラインからバッチモードでSASを呼び出す経験はありますが、メッセージを受け取ってそのメッセージを処理する実際の経験はありません。私は、SASプログラムの中からstdinからの読み方についてかなりの情報を見つけましたが、私のSASプログラムをstdoutまたはstderrに書き出す方法を理解できていないようです。 Windowsでこれを行うことはできますか?WindowsのSASからstdoutとstderrをキャプチャしますか?

SASプログラムから、私は次の操作を行いたいと思います:

  • は手動でエラーを検出し、SASプログラム内でログファイル
  • にそれらを印刷するのではなく、stderrに警告メッセージとエラーメッセージをリダイレクトしますおよび/またはその他の問題を検出し、stderrまたはstdoutに出力します。 SAS呼び出す

    SAS

    data test; 
        attrib i length=8; 
    
        do i = 1 to 10; 
         put 'putting'; *how can i make this go to stdout?; 
         putlog 'putting to log'; *this can go to the log - that is okay; 
         if i = 5 then do; 
          *pretend this is an error I am manually detecting - how can i make this go to stderr?; 
          put 'we found 5'; 
         end; 
         output; 
        end; 
    run; 
    
    data _null_; 
    
        1 = y; *this is an error detected by SAS. How can I make this go to stderr?; 
    
    run; 
    

    のPython:このからコンソールに

    import subprocess 
    import os 
    
    
    if __name__ == '__main__': 
    
        filename = os.path.normpath(r'C:\Users\oob\Desktop\sas_python_test.sas') 
        sas_executable = os.path.normpath(r'C:\Program Files\SAS\SASFoundation\9.2\sas.exe') 
    
        cmd = r'"' + sas_executable + r'"' + " " + r'"' + filename + r'"' 
    
        p = subprocess.Popen(cmd,shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE) 
        print p.communicate() 
    

    私の結果をされています。ここ

は、私が試したものである

('', '') 
+1

私はSASを一度も使用していませんが、コンソールアプリケーションですか? stdout/stderrのハンドルがないかもしれません。その場合、PyWin32の 'win32com'モジュールを使って[SAS using OLE](http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#oleauto.htm)を試してください。 – eryksun

+0

有用な名前のついたパイプの使用に関するSASの文書へのリンクは次のとおりです。 http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#unnamed.htm興味深いのは – RWill

答えて

2

私の知る限り、これは直接達成することはできません知っています。これをエミュレートするには、-sysparmコマンド行パラメーターを使用してsasに固有のファイル名を渡し、STDOUT出力をそのファイルに保存します。その後、-logコマンド行パラメーターを使用して、sasログを別のファイルに送信することができます。 SASプログラムが終了すると、選択したプログラムを使用して、生成された各ファイルを解析することができます。残念ながら、sasプログラムが終了するまでログファイルはロックされています。そのため、SASを使用してログファイルを処理する場合は、セカンダリのフォローアッププログラムを起動する必要があります。 (つまり、作成中のSASプログラム内からログファイルを読み取ることはできません)。またはWARNING:あなたがERRORで始まる行を探すことができ、ログ読みたら

は、適切な行動を取ると(私の場合、それは私に電子メールを送信します)。あなたのコーディングスタイルに合ったロジックを追加したいかもしれません。たとえば、SASがNOTESとみなすものには、ERRORSと見なされるものがあります。また、SASが私が気にしないエラーとして扱うものもあります。私が使用しているロジックは次のとおりです。

data problems log; 
    length line $1000; 

    infile "&logfile"; 
    input; 

    logfile = "&logfile"; 
    line_no = _n_; 
    line = _infile_; 
    problem = 0; 

    if 
    (
    line =: "ERROR:" 
    or line =: "WARNING:" 
    or line =: "NOTE: Numeric values have been converted to character values" 
    or line =: "NOTE: Character values have been converted to numeric values" 
    or line =: "NOTE: Missing values were generated as a result of performing an operation on missing values" 
    or line =: "NOTE: MERGE statement has more than one data set with repeats of BY values" 
    or line =: "NOTE: Invalid (or missing) arguments to the INTNX function have caused the function to return" 
    or line =: "INFO: Character variables have defaulted to a length of 200" 
    or line =: "NOTE: Invalid" 
) 
    and not 
    (
    line =: "WARNING: Your system is scheduled to expire" 
    or line =: "WARNING: The Base Product product with which Session Manager is associated" 
    or line =: "WARNING: will be expiring soon, and is currently in warning mode to indicate" 
    or line =: "WARNING: this upcoming expiration. Please run PROC SETINIT to obtain more" 
    or line =: "WARNING: information on your warning period." 
    or line =: "WARNING: This CREATE TABLE statement recursively references the target table. A consequence" 
    or line =: "WARNING: Unable to copy SASUSER registry to WORK registry. Because of this, you will not see registry customizations during this" 
    or line =: "WARNING: Estimates did not improve after a ridge was encountered in the objective function." 
    or line =: "WARNING: Estimates may not have converged." 
    or line =: "ERROR: A lock is not available for" 
    or line =: "ERROR: Errors printed on pages" 
) 
    then do; 
    problem = 1; 
    output problems; 
    end; 
    output log; 
run; 

希望します。

+0

ちょっと考えて - あなたはSASをSTDOUTにエミュレートすることができます。古いSAS/Intranetモジュールもこの目的のために働くでしょう.... –

+0

ありがとうございました。私はsysparmとlogバッチ処理の前にバッチ処理の引数を渡す必要があるので、このアプローチをとると思います。ロックされているログファイルについてはあまりにも悪いです。私は可能な限り多くのものを検出することができます(例えば予想した数とは違った数の観測値)。エミュレートされたSTDERRに出力することもできます。 – oob

2

は、私は便利なSASのWindowsのバージョンを持っていないが、UNIX上で、私はこのようにSTDOUTにリダイレクト:

data _null_; 
    file STDOUT; 
    do i=1 to 10; 
    put i=; 
    end; 
run; 

STDERRにエラーログをリダイレクトしますが、STDERRへの印刷は次のようになりますかわかりません:

あなたのためのSTDERRにログをリダイレクトするGoogleで検索
ods listing file=STDERR; 

proc print data=sashelp.class; 
run; 
+0

です。 。これはWindowsでは動作しません。私はSASがstdout/stderrハンドルを持っていないと思います:( – oob

+0

'ods listing file = STDERR;' unix上で動作しますThanx –

-1

proc printto log=STDERR; 
    run; 

    data _null_; 
     1=x; 
    run; 
+0

ありがとう私はこれを試しましたが、stderrにログをリダイレクトしません。 'stderr.log'。 – oob

関連する問題