I'm using a dataset which is something like :
+----------+--------+-------+
| Variable | Level | Value |
+----------+--------+-------+
| sexe | men | 10 |
| | female | 20 |
| age | 0-20 | 5 |
| | 20-40 | 5 |
| | 40-60 | 10 |
| | >60 | 10 |
+----------+--------+-------+
And I would like to fulfill the "blank" cells using the previous non-blank cell to obtain something like this.
+----------+--------+-------+
| Variable | Level | Value |
+----------+--------+-------+
| sexe | men | 10 |
| sexe | female | 20 |
| age | 0-20 | 5 |
| age | 20-40 | 5 |
| age | 40-60 | 10 |
| age | >60 | 10 |
+----------+--------+-------+
I tried various possibilities in DATA step mostly with the LAG() function. The idea was to read the previous row when the cell was empty and fill with that.
DATA test;
SET test;
IF variable = . THEN DO;
variable = LAG1(variable);
END;
RUN;
And I obtained
+----------+--------+-------+
| Variable | Level | Value |
+----------+--------+-------+
| | men | 10 |
| sexe | female | 20 |
| | 0-20 | 5 |
| age | 20-40 | 5 |
| | 40-60 | 10 |
| | >60 | 10 |
+----------+--------+-------+
The problem was the good string is not always just one row upper. But I don't understand why SAS put blank in the first and 3d line. It didn't have to modify this line because I said "If variable = .". I know how to do this in Python or in R with some for loop but I didn't find good solution in SAS.
I tried to put the string inside a variable with "CALL SYMPUT" and also with "RETAIN" but it didn't work too.
There must be a simple and elegant way to do this. Any idea?