Frage

data exp;
input y a b c;
cards;
1 2 3 6
4 5 . 8
1 . . 8
2 4 6 9
. 4 6 3
;
run;

My requirement is to get this data set into single data set with say 3 new columns without the blank values.

output should look like this :

x y z
1 5 6
4 4 8
1 4 8 
2 3 9
2 6 3
War es hilfreich?

Lösung

Here is what I came up with:

data _null_;
    set exp (obs=0) nobs=nobs;
    call symputx("nobs", nobs);
run;

proc transpose data=exp out=exp2;
run;

data exp3(keep=row final);
    set exp2;
    array col{&nobs};
    do i = 1 to &nobs;
        if col[i] ne . then do;
            row+1;
            if row = &nobs+1 then row = 1;
            final = col[i];
            output;
        end;
    end;
run;

proc sort data=exp3;
    by row;
run;

proc transpose data = exp3 out=exp4 (drop=_name_ row 
        rename=(col1=x col2=y col3=z col4=zz));
    by row;
run;

First I counted the observations in the starting data set and stored that to a macro variable (if you know it is always 5 you can skip this step and replace &nobs with 5). Then I transposed the data set so it is rotated 90 degrees. This allows me to then go through each number in order and output it to one long row, also adding a number for the row we want it to be in in the final output. Finally I transposed by row number to get the final output.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top