BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have a SAS table, MYTABLE, with the variable YEAR, among others. The table is sorted by YEAR in ascending order.

The last(highest) YEAR is supposed not to be known in advance.

Since the ascending order by YEAR, I know that the value of YEAR (at least) in the last record contains the last(highest) YEAR.

I want to keep all the records for the last 5 years. If I could assign the value of YEAR in the last record to a variable LASTYEAR I could have the condition

IF YEAR >= LASTYEAR - 4; .

I would prefere to do this in one data step, but two data steps would be OK. I don't want to use PROC SQL.

Is this possible?
2 REPLIES 2
data_null__
Jade | Level 19
[pre]
data allWithLastAge;
set sashelp.class(keep=age rename=age=target) point=nobs nobs=nobs;
put target=;
do until(eof);
set sashelp.class end=eof;
if age eq target then output;
end;
stop;
run;
proc print;
run;
[/pre]
DanielSantos
Barite | Level 11
Is it absolutely necessary to have the data sorted in a ascending order?

Normally you just need the data sorted, reversing to descending would simplify greatly the solution by reducing it to a single pass.

Assuming that YEAR is numeric:

data RESULTS;
set DATA;
if _N_ eq 1 then call symput('MAX_YEAR',put(YEAR,best.));
if not (YEAR >= symgetn('MAX_YEAR') -4) then stop;
run;

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1136 views
  • 0 likes
  • 3 in conversation