Help using Base SAS procedures

Combining observations

Reply
N/A
Posts: 1

Combining observations

Hi,

I'm working with a dataset that has the following format:

ID     Var1     Var2     Var3    Var4

1          .5        .          .          .

1          .          .5          .          .

1          .          .          .5         .

1          .          .          .          .5

2        .7          .          .          .

2          .          .7        .          .

2          .          .          .7         .

2          .          .          .          .7

etc

And I want to combine the observations so the end result is:

ID Var1     Var2     Var3     Var4

1     .5          .5          .5       .5

2     .7          .7          .7        .7

Any suggestions?

Thank you!

Respected Advisor
Posts: 3,156

Re: Combining observations

Posted in reply to pglezlatapi

Can't recall how many times I am being the copycat of  's ingenious solution:

data have;

input ID     Var1 Var2     Var3    Var4;

cards;

1          .5        .          .          .

1          .          .5          .          .

1          .          .          .5         .

1          . .          .          .5

2        .7          .          .          .

2          .          .7        .          .

2          .          .          .7         .

2          .          .          .          .7

;

data want;

update have(obs=0) have;

by id;

run;

Valued Guide
Posts: 860

Re: Combining observations

Posted in reply to pglezlatapi

here's another option but I like Hai.Kuo's better:

data have;

infile cards dsd;

input ID Var1 Var2 Var3 Var4;

cards;

1,.5,.,.,.

1,.,.5,.,.

1,.,.,.5,.

1,.,.,.,.5

2,.7,.,.,.

2,.,.7,.,.

2,.,.,.7,.

2,.,.,.,.7

;

proc sql;

create table want as

select distinct

id,

max(var1)as var1,

max(var2)as var2,

max(var3)as var3,

max(var4)as var4

from have

group by id

order by id;

Trusted Advisor
Posts: 1,228

Re: Combining observations

Posted in reply to pglezlatapi

Or

proc sql;

create table want as

select id,sum(var1) as var1,sum(var2) as var2, sum(var3) as var3, sum(var4) as var4 from have

group by id;

quit;

Ask a Question
Discussion stats
  • 3 replies
  • 255 views
  • 0 likes
  • 4 in conversation