Vivian
Vivian

Reputation: 309

How to assign last var

I have a big data set such as

ca1   ca2   ca3   ca4   ca5   ca6   ca7   ca8   ca9
 1     1     8     8     8     8     8     8     8
 1     0     0     8     8     8     8     8     8
 1     1     8     8     8     8     8     8     8
 0     8     8     8     8     8     8     8     8
 0     2     2     2     1     8     8     8     8
 0     0     0     1     2     1     8     8     8

I want to catch the var before the first var=8, but ignore 2, such as

ca1   ca2   ca3   ca4   ca5   ca6   ca7   ca8   ca9   new   type

 1     1     8     8     8     8     8     8     8     1     2
 1     0     0     8     8     8     8     8     8     0     3
 1     1     8     8     8     8     8     8     8     1     2
 0     8     8     8     8     8     8     8     8     0     1
 0     2     2     2     1     8     8     8     8     1     5
 0     0     0     1     2     1     8     8     8     1     6

new means new var type means which place

how can i do in the sas thank a lot

Upvotes: 1

Views: 59

Answers (2)

Joe
Joe

Reputation: 63424

I'm not totally clear on what "ignore 2" means. If it doesn't really factor into things, then this is an interesting application of whichn, which identifies the first value in the 2nd through end arguments that is equal to the first argument.

data want;
  set have;
  array ca[9];
  type = whichn(8,of ca[*])-1;
  *what if it is all 8s - handle that somehow;
  new = ca[type];
run;

If it is a problem (and possible) for 2 to appear in this position, then you may need to use Pekka's version (which is probably more 'standard' but a bit slower if this is a really big dataset).

Upvotes: 0

Pekka
Pekka

Reputation: 2448

You can use arrays with a do loop like this.

data bidata2;
    set bigdata;
    array c ca1--ca9;
    do i = 1 to 9;
        if c(i)=8 then leave;
        else if c(i) NE 2 then type = i;
    end;
    if type NE . then new = c(type);
drop i;
run;

Upvotes: 1

Related Questions