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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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