0

私のデータは次のようになり、「欲しい」列を取得する方法がわかりません。残念ながら、retain、lag、および sum 関数のさまざまな組み合わせを試しましたが、成功しませんでした。

month   quantity1   quantity2   want
1       a           x           x+sum(b to l)
2       b           y           sum(x to y)+sum(c to l)
3       c           z           sum(x to z)+sum(d to l)
4       d       
5       e       
6       f       
7       g       
8       h       
9       i       
10      j       
11      k       
12      l       

この件についてご協力いただきありがとうございます

4

3 に答える 3

2

数量 1 を合計してからマクロ変数に値を格納すると便利です。余分なデータの例を使用します。

proc sql;
   select sum(qty1) into:sum_qty1 from temp;
quit;

data want;
   set temp;
   value1+qty1;
   value2+qty2;
   want=value2+&sum_qty1-value1;
   if missing(qty2) then want=.;
   drop value:;
run; 
于 2016-07-08T18:04:31.940 に答える
1

「ローリング 12 か月の合計」のように聞こえます。その場合、別のデータ構造 (2 つの変数ではなく、24 行 1 つの変数) を使用する方がはるかに簡単です。次に、すべての ETS ツール、または SQL または SAS データ ステップの単純なプロセスを取得します。

データを再構築できない/再構築しない場合は、データを一時配列 (またはハッシュ テーブルですが、初心者にとっては配列の方が簡単です) にロードすることでこれを行うことができます。これにより、すぐにすべてにアクセスできます。例:

data have;
  do month = 1 to 12;
    q_2014 = rand('Uniform')*10+500+month*5;
    q_2015 = rand('Uniform')*10+550+month*5;
    output;
  end;
run;

data want;
  array q2014[12] _temporary_;       *temporary array to hold data;
  array q2015[12] _temporary_;
  if _n_=1 then do;                  *load data into temporary arrays;
    do _n = 1 to n_data;
      set have point=_n nobs=n_data;
      q2014[_n] = q_2014;
      q2015[_n] = q_2015;
    end;
  end;
  set have;
  do _i = 1 to _n_;                          *grab the this_year data;
    q_rolling12 = sum(q_rolling12,q2015[_i]);
  end;
  do _j = _n_+1 to n_data;
    q_rolling12 = sum(q_rolling12,q2014[_j]);*grab the last_year data;
  end;
run;
于 2016-07-08T17:18:55.833 に答える