How to contrast two sets of variables (in SAS)

2019-02-28 06:34发布

问题:

The data looks like:

ID----X1----X2----X3----Z1----Z2----Z3

For each ID, consider two sets of variables {X1, X2, X3} and {Z1, Z2, Z3} that:

  • The numbers of Xs and Zs may be equal or not. They may also have missing values.
  • Values of variables in each set is unique. That is, for each ID, X1 not equal X2 not equal X3. The same applies for Zs.
  • Values of Xs and Zs can be equal, and there comes the question. How can I create a new data that retains equal values of Xs and Zs and exclude unequal values. For example, if X1 is equal to any Zs, then X1 will be retained together with the Z.

Consider a hypothetical data:

data temp;

input ID x1 x2 x3 z1 z2 z3;

datalines;

1001      11      12      13     .     12     11

1002      21      22      23    24     25     26

1003      31      32      33    31     32      .

1004      41      42      43    41     44     45
;

run;

I want it to be:

1001     11     12     .     .     12     11

1002     .       .     .     .      .      .

1003     31     32     .    31     32      .

1004     41      .     .    41      .      .

回答1:

If I understand correctly, you need to process each array separately. First, you set X values to missing that are not in Z, then go back and set values in Z that are not in X. Try this:

data want;
   set temp;
   array Xarr{*} x:;
   array Zarr{*} z:;

   do _i_=1 to dim(Xarr);
      if not (Xarr(_i_) in Zarr) 
         then Xarr(_i_) = .;
      end;

   do _i_=1 to dim(Zarr);
      if not (Zarr(_i_) in Xarr) 
         then Zarr(_i_) = .;
      end;
   drop _i_;
run;


回答2:

Not next to sas right now, but this should be roughly what you are after:

data test;
    set temp;

    array arrx{*} x:;
    array arrz{*} z:;

    arriter = max(dim(arrx), dim(arrz));

    do _i = 1 to arriter;
    if arrx{_i} ne arrz{_i} then do;
        arrx{_i} = .;   
        arrz{_i} = .;
    end;
    end;
run;

I'm not sure what you want to do with cases where there are more elements in x than in z or vice versa, so this code will leave as they are.



标签: sas