Michael
Michael

Reputation: 75

Combining columns in a SAS dataset

My question involves combining multiple columns in a dataset into a single column. I know that this question has been answered several times before, but that was only for a single variable outside of a larger dataset. This is for two variables in a larger data set.

Basically, here is what the data looks like:

Range Pass Height Height_1 Height_2 Height_3  V   V_1   V_2   V_3   Breed Treatment
2     6    200    .        .        .         20  .     .     .     AAAA  1
2     8    .      179      .        .         .   18    .     .     BBBB  1
2     10   .      .        199      .         .   .     21    .     CCCC  1
2     12   .      .        .        188       .   .     .     19    DDDD  1
3     6    155    .        .        .         17  .     .     .     BBBB  2
3     8    .      201      .        .         .   22    .     .     AAAA  2
3     10   .      .        195      .         .   .     20    .     CCCC  2
3     12   .      .        .        188       .   .     .     18    DDDD  2

So basically, I want to combine all four "Height" columns into a single Height column and all four "V" columns into a single V column while keeping all the other columns unchanged.

Upvotes: 1

Views: 367

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269803

You would do this in SQL using coalesce(), which SAS proc SQL supports:

proc sql;
    select range, pass,
           coalesce(Height, Height_1, Height_2, Height_3) as height,
           coalesce(V, V_1, V_2, V_3) AS V,
           Breed, Treatment
    from dataset;

Upvotes: 4

Related Questions