2017-01-11 6 views
0

のためのユニークな値を出力:サスは、多くの変数

a b c 
1 1 1 
2 1 2 
2 2 1 
3 1 2 
3 2 2 

を列AおよびBの私は、1回の操作で一意の値を印刷したい(私の本当のデータセットに、私は40を持っています列私はのためのユニークな値を印刷したい

私は、このコマンドを使用して、SQLでそれを行うことができます。

proc sql; 
create table test as 
select distinct a from 
A 

しかし、その後、私は多くの時間を要し、すべての変数、それを実行する必要があります私は。本当にtを印刷したいこの形式上の彼のユニークな値:

a 1 2 3 
b 1 2 

またはより良い

a b 
1 1 
2 2 
3 . 

それが彼らのそれぞれを介してすべての変数名とループを入れて、独自の変数を印刷することが可能であることができれば、私は次のようになります非常に幸せです

SASでも可能ですか?私は完全な解決策は必要ありません。正しい方向に向けるだけです。

答えて

0

これは長いですが、あなたが望む答えを与える必要があります。

data test; 
    length a b c 3; 
    input a b c; 
    datalines; 
    1 1 1 
    2 1 2 
    2 2 1 
    3 1 2 
    3 2 2 
    ; 
run; 

** put variables into list **; 
proc sql noprint; 
    select NAME 
    into: varlist separated by " " 
    from dictionary.columns 
    where upcase(libname)="WORK" and upcase(memname)="TEST"; 
quit; 

** loop over each variable **; 
%macro loop_through(varlist); 
    %do i=1 %to %sysfunc(countw(&varlist.));; 
     %let var=%scan(&varlist.,&i.); 

     /* output the frequency for each variable only keeping the value */ 
     proc freq data = test noprint; 
      table &var./out=freq_&var.(keep=&var.); 
     run; 

     /* merge that output with the same dataset each iteration */ 
     data freq1_&var.; set freq_&var.; 
      temp_var = "temp"; 
     run; 

     data merged; 
      merge freq1:; 
      by temp_var; 
     run; 
    %end; 

    /* remove duplicates after merging */ 
    %do i=1 %to %sysfunc(countw(&varlist.));; 
     %let var=%scan(&varlist.,&i.); 
     data merged; set merged; 
      if lag(&var.) = &var. then &var. = .; 
     run; 
    %end; 

    /* delete individual frequency datasets */ 
    proc datasets nolist; delete freq:; quit; 

    /* output final dataset */ 
    data final; set merged; 
     drop temp_var; 
    run; 
%mend; 

%loop_through(&varlist.); 
+0

こんにちは。ありがとう。これは私の問題の正確な解決策です:) – Havsula

0

私は、これは時間を節約することを知りませんが、あなたはunion allを使用して結果を組み合わせることができます。

proc sql; 
create table test as 
    select distinct 'a', a from t union all 
    select distinct 'b', b from t union all 
    . . .; 
0

これは私の心の中で、あなたに最も近いを取得します、一意の値を印刷する最初の時間をマージした後に、重複を削除する2番目の時間を - それは二回、それらをループし、リストにあなたの変数を置きます。

data have; 
input a b c; 
datalines; 
1 1 1 
2 1 2 
2 2 1 
3 1 2 
3 2 2 
;;;; 
run; 
proc tabulate data=have out=want; 
    class a b c; 
    tables n,(a b c); 
run; 

次に、結果を好きなものに置き換えて、必要な場所に正確に移動します。

関連する問題