Offset column by value in other column

Reply
Contributor
Posts: 27

Offset column by value in other column

Say I have:

data test;

     input offset P_1 P_2 P_3 P_4 P_5 P_6;

     cards;

     0 1 2 3 4 5 6

     2 1 2 3 4 5 6

     4 1 2 3 4 5 6

     ;

     run;

I want to reassign column values based on the value in column "offset."

That is, for line 1: P_1 = P_1, P_2 = P_2, P_3 = P_3, P_4 = P_4, P_5 = P_5, P_6 = P_6

Line 2: P_1 = P_3, P_2 = P_4, P_3 = P_5, P_4 = P_6 P_5 = . P_6 = .

Line 3: P_1 = 5, P_2 = P_6 P_3 = . P_4 = . P_5 = . P_6 = .

Where I run out of columns to reassign (ex, I have no P_7) I want to set the values to null.

So my ending dataset is:

0 1 2 3 4 5 6

2 3 4 5 6  . .

4 5 6  . .  . .

Super User
Posts: 19,855

Re: Offset column by value in other column

data want;

set test;

    array p(*) p_1-p_6;

    do i=1 to dim(p);

   

    if i+offset<=dim(p) then p(i)=p(i+offset);

    else p(i)=.;

    end;

run;

Respected Advisor
Posts: 3,156

Re: Offset column by value in other column

data test;

     input offset P_1 P_2 P_3 P_4 P_5 P_6;

     cards;

     0 1 2 3 4 5 6

     2 1 2 3 4 5 6

     4 1 2 3 4 5 6

     ;

     run;

       data want;

         set test;

         array p_ p_:;

         if offset ne 0 then

do i=1 to dim(p_);

if i<= dim(P_)-offset then p_(i)=p_(i+offset);

            else call missing(P_(i));

end;

drop i;

      run;

Haikuo

Ask a Question
Discussion stats
  • 2 replies
  • 160 views
  • 6 likes
  • 3 in conversation