BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
natson001
Calcite | Level 5

Supposed my dataset like this:

obs x

1     5

2     6

3     9

4     8

5     6

6     7

I want to replace the first obs with the sum of the first and the second (5 replayed by 5+6=11) and the third with the sum of the third and fourth (9 replace by 9+8=17) , etc.

The resulting dataset like this:

obs x

1    11

2    6

3    17

4     8

5     13

6     7

What is the best way to do that?

Thanks!

Andrew

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Strange but simple request... Here goes:

data have;

input obs x;

datalines;

1     5

2     6

3     9

4     8

5     6

6     7

;

data want;

if not end then set have(firstobs=2 rename=x=y) end=end;

set have;

if mod(_n_, 2) = 1 then x = x+y;

keep obs x;

run;

PG

PG

View solution in original post

6 REPLIES 6
Reeza
Super User

Why is the second and 4th left alone?

natson001
Calcite | Level 5

Because I want them to be there.

Reeza
Super User

Well if the logic is in your head then I guess you'll have to hard code it.  Seems kinda pointless then.

PGStats
Opal | Level 21

Strange but simple request... Here goes:

data have;

input obs x;

datalines;

1     5

2     6

3     9

4     8

5     6

6     7

;

data want;

if not end then set have(firstobs=2 rename=x=y) end=end;

set have;

if mod(_n_, 2) = 1 then x = x+y;

keep obs x;

run;

PG

PG
natson001
Calcite | Level 5

Straightforward and beautiful! Thanks PG!

stat_sas
Ammonite | Level 13

Another way to do it.

data want;
set have;
if mod(_n_,2) then flag+1;
run;

proc sql;
select obs,case when obs=min(obs) then sum(x) else x end as x from want
group by flag
order by obs;
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
  • 6 replies
  • 856 views
  • 0 likes
  • 4 in conversation