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

Hello,

Suppose i have such dataset :

date   id    res     sumres

1       a       10      

1       a       15      

1       a       10       35

2       a       12       

2       a        8      

2       a       10       30

1       b       10     

1       b       17      

1       b       12       39

;

I need to calculate these sumres (35, 30, 39....) in a new dataset which are for id=a and date=1, for id=a and date=2, id=b and date=1, id=b=2 and so on... i have dates from 1...30.

its probably a loop,  if first.date=....do;..but cant figure it out.

anyone knows how to do it? i'd be immesely grateful.

cheers,

M

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

If your data are EXACTLY as shown, and you really only want the sumres calculation to be non-missing for the last record in each set, then it is easy.  I don't have access to SAS to test the following code at the moment, but it should give you enough of an idea to solve your problem.

data want (drop=calculation);

  set have;

  by id date;

  if first.date then calculation=res;

  else calculation+res;

  if last.date then sumres=calculation;

run;

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

If your data are EXACTLY as shown, and you really only want the sumres calculation to be non-missing for the last record in each set, then it is easy.  I don't have access to SAS to test the following code at the moment, but it should give you enough of an idea to solve your problem.

data want (drop=calculation);

  set have;

  by id date;

  if first.date then calculation=res;

  else calculation+res;

  if last.date then sumres=calculation;

run;

bluetail
Calcite | Level 5

art297, thank you for a quick reply   - you're a star! Smiley Happy  i tried it before but missed out the ' if last.date' part

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 702 views
  • 0 likes
  • 2 in conversation