BookmarkSubscribeRSS Feed
mariamabotaleb
Calcite | Level 5

Hello, 

I am trying to sum multiple observations with the First.Variable Last.Variable feature, however it never works in adding all the observations together, it either gives me the first or last variable values  


Data T_Total;
set T;
by m**; 
if first. m** then TOTAL_DAYS=0;
TOTAL_DAYS= sum (TOTAL_DAYS,SUPPLY_DAYS);
retain TOTAL_DAYS;
if last. m** then output;
run;

I also tried changing the order of sentences, or using the 

TOTAL_DAYS= TOTAL_DAYS+SUPPLY_DAYS;

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

There's no double-asterisk in this syntax.

 

And instead of TOTAL_DAYS=sum (TOTAL_DAYS,SUPPLY_DAYS); you probably want

 

total_days+supply_days;

Better yet, don't program this yourself in a DATA step. Use PROC SUMMARY, where this is a built-in feature.

 

--
Paige Miller
Astounding
PROC Star

When you use first. and last. variables, there should not be a space after the dot.  Incorrect:

first. m

Correct:

first.m

And as was noted, asterisks are not a legitimate part of a variable name.  Clean up the obvious inaccuracies, and see if any problems remain.  Your syntax for summing should work, even if other choices exist.

Tom
Super User Tom
Super User

Make sure that TOTAL_DAYS does not already exist as a variable in the input dataset T.  Because if it does then as you read observations from T the value you calculated after reading the pervious observation will be overwritten with what is read from T.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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