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

I am wanting to have SAS return the last weight for each ID. I was able to do this successfully with the intial weight but the final weight is pulling the last weight from the previous ID. Why is this happening?

 

Thanks!

 

data Test1; set Test;
by ID;
retain last_avrgweight;
if last.ID then last_avrgweight=AvrgWeight;
run;

 

IDWeight
12100
12102
12103
12105
12106
25210
25211
25212
25215
25216
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Essentially, you are asking SAS to take the last value found, and then go back and add that to earlier observations.  That can be done, but requires a more complex DATA step:

 

data want;

do until (last.id);

   set have;

   by id;

run;

last_avrgweight=AvrgWeight;

do until (last.id);

   set have;

   by id;

   output;

end;

run;

 

The top DO loop reads all the observations for an ID, so the last value is available once that loop ends.  The bottom loop reads the same observations, and outputs them (including the value for the new variable).

 

View solution in original post

7 REPLIES 7
Reeza
Super User

Your question is unclear. How is it not working? What are you trying to get vs what do you have?

llt34c
Calcite | Level 5

I want SAS to take the last weight for each ID and list that as a new variable. Currently, SAS is taking the last weight from the previous ID and listing it as the new variable (thus this is the incorrect ID).

Reeza
Super User

You have specified retain and only store it at the last record so that's correct behaviour. 

 

It would help if you illustrated what you wanted but right now my suggestion would be to remove retain. 

Astounding
PROC Star

Essentially, you are asking SAS to take the last value found, and then go back and add that to earlier observations.  That can be done, but requires a more complex DATA step:

 

data want;

do until (last.id);

   set have;

   by id;

run;

last_avrgweight=AvrgWeight;

do until (last.id);

   set have;

   by id;

   output;

end;

run;

 

The top DO loop reads all the observations for an ID, so the last value is available once that loop ends.  The bottom loop reads the same observations, and outputs them (including the value for the new variable).

 

Reeza
Super User

Or sort the other direction...

 

proc sort data=have;

by id descending amount;

 

data want;

set have; 

by id;

retain lastVal;

if first.id then lastVal=amount;

 

run;

 

proc sort data=want;

by id amount;

run;

Astounding
PROC Star

That works as long as the last value is also the largest value.  That might be true here, and the sample data shows it that way.  But I didn't want to assume it if it wasn't stated.

llt34c
Calcite | Level 5
Thank you! I really appreciate it!

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!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2258 views
  • 1 like
  • 3 in conversation