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

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 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.

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
  • 741 views
  • 0 likes
  • 3 in conversation