NonSleeper
NonSleeper

Reputation: 851

How to contrast two sets of variables (in SAS)

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:

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      .      .

Upvotes: 1

Views: 121

Answers (2)

BellevueBob
BellevueBob

Reputation: 9618

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;

Upvotes: 3

undershock
undershock

Reputation: 803

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.

Upvotes: 1

Related Questions