Copying previous column value to SAS

I am trying to copy the value from the previous column to the real column if there is a missing value, but something is wrong in the code I wrote.

data X; 
input A B C D E; 
cards;
1 . . . 2
2 2 3 . .
3 3 4 5 6 
4 4 4 2 .
. . 6 . .
;
run;

      

Program

data Y; 
set x; 
array arr(5) a--e; 

array brr(4) b--e; 

do j=1 to dim(arr); 
do i =2 to dim(brr);

    if brr(i)=. then brr(i)=arr(j); 
end;
end;
drop i j;
run;

      

However, the output I get is

1 . 1 1 2
2 2 3 2 2
3 3 4 5 6 
4 4 4 2 4
. . 6 6 6

      

It is not right! The result I want is as follows:

1 1 1 1 2
2 2 3 3 3
3 3 4 5 6 
4 4 4 2 4
. . 6 6 6

      

What's wrong with the code?

+3


source to share


1 answer


Do you want 4 4 4 2 2

instead 4 4 4 2 4

?

You only need one loop:

Try this code:



data Y; 
    set x; 
    array arr(5) a--e; 

    do i=2 to dim(arr); 
        if arr(i)=. then arr(i)=arr(i-1); 
    end;

    drop i;
run;

      

Also, don't forget to think about what's going on in this code! You can try checking every line and every i:

  • What is the value of arr (i)?
  • What is the value of arr (i-1)?
  • is the result of what is expected? (Convince yourself that the problem is solved :))
+4


source







All Articles