2016-12-16 1 views
-1

SASで新しく、データセット内の変数のすべてのエントリが条件を満たす(つまり= 1)かどうかをチェックし、変数内のすべてのエントリが1か、少なくとも1つが1でないかどうかに依存します。 どのようにすればよいでしょうか?カラムsasのすべてのエントリをチェックしてダミー変数sasを返します

IF colvar = 1 THEN dummy_variable = 1 

は、元の変数と同じサイズの別の変数dummy_variableを作成します。

+0

このダミー変数を使って何をする予定ですか?新しい変数として追加し、すべての観測値の値を複製しますか?この1つの変数と1つの観測だけを持つ新しいデータセットを作成しますか?あるいは、マクロロジックを生成する必要があります。後でマクロロジックまたは別のデータステップでテストできます。 – Tom

答えて

0
* Generate test data; 
data have; 
    colvar=0; 
    colvar2=0; 
    do i=1 to 20; 
     colvar=round(ranuni(0)); 
     output; 
    end; 
    drop i; 
run; 

* Read the input dataset twice, first counting the number 
* of observations and setting the dummy variables to 1 if 
* the corresponding variable has the value 1 in any obser- 
* vation, second outputting the result. The dummy variables 
* remain unchanged during the second loop.; 
data want; 
    _n_=0; 
    d_colvar=0; 
    d_colvar2=0; 
    do until (eof); 
     set have end=eof; 
     if colvar = 1 
     then d_colvar=1; 
     if colvar2 = 1 
     then d_colvar2=1; 
     * etc.... *; 
     _n_=_n_+1; 
    end; 
    do _n_=1 to _n_; 
     set have; 
     output; 
    end; 
run; 
0

PROCのSQLはすぐに任意に定義された条件の要約を生成するための優れたツールです、ありがとうございました。あなたの状態は正確ではありません。私はテーブルのALL_ONE値が下のコードを生成したいと思う。すべての観察がCOLVAR = 1であるとき、それは1になります。 1つではない値は、条件が偽(0)になるため、ALL_ONEは1ではなく0の値になります。

結果を小さなテーブルに格納できます。

proc sql ; 
    create table check_if_one as 
    select min(colvar=1) as all_one 
      , max(colvar=1) as any_one 
      , max(colvar ne 1) as any_not_one 
      , min(colvar ne 1) as all_not_one 
    from my_table 
    ; 
quit; 

ただし、後で何らかの目的で簡単に使用できるマクロ変数に値を格納することもできます。

proc sql noprint ; 
    select min(colvar=1) into :all_one trimmed from my_table ; 
quit; 
関連する問題