0

Data:

A B C D E
2 3 4 . .
2 3 0 0 .
0 3 4 1 1
0 . 4 0 1
2 0 0 0 1

Ideal output:

A B C D E
2 3 4 1 1
2 3 0 0 1
0 3 4 1 1
0 3 4 0 1
2 0 0 0 1

For each column, there are only 3 possible values: an arbitrary integer, zero, and missing value.

I want to replace the missing values with the non-zero value in the corresponding column.

If the arbitrary integer is zero, then missing value should be replaced by zero.

For actual problem, the number of row and number of columns are not small.

2
  • How did you derive that in the first column the missing must be replaced with 1 and in the 4th column must be replaced with 3? Commented May 18, 2015 at 12:05
  • 1st column has no missing value in the example. 4th columns has only one non zero value, which is 1. Commented May 18, 2015 at 12:25

1 Answer 1

2

Make two arrays--one with your column names and another with variables to hold the arbitrary integers. Loop through the data set once to get the integers (looping over the columns in the array), then again to output the values, replacing where necessary (again, looping through the columns in the array).

data want(drop=i int1-int5);
  do until (eof);
    set have end=eof;
    array _col a--e;
    array _int int1-int5;
    do i = 1 to dim(_col);
      if _col(i) not in (.,0) then _int(i)=_col(i);
    end;
  end;
  do until (_eof);
    set have end=_eof;
    do i = 1 to dim(_col);
      if missing(_col(i)) then _col(i)=_int(i);
    end;
    output;
  end;
run;
Sign up to request clarification or add additional context in comments.

2 Comments

Why first do until is placed before set statement?
This is a technique commonly known as a "DOW loop." A search on this term will bring up several papers that can explain how it works and what it is useful for. Basically, instead of the data step doing one iteration for each row of the data set, you can read multiple rows in each iteration. There are many reasons this construct can be useful; in this case, it is useful because it allows the data set to be read twice with the int1-int5 values being saved from the first read-through.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.