BookmarkSubscribeRSS Feed
pglezlatapi
Calcite | Level 5

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!

3 REPLIES 3
Haikuo
Onyx | Level 15

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;

Steelers_In_DC
Barite | Level 11

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;

stat_sas
Ammonite | Level 13

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1477 views
  • 0 likes
  • 4 in conversation