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

The first pick  the first row from the data set kaplan.

How come the statement _n_=n does not pick the last row.?

How do I pick the last row?

data KS_;

set Kaplan;

if _n_ =1 then keep=1;

if keep ne 1 then delete;

drop keep survival;

run;

data KSm_&p(rename=survival1=(survival_max time=time_max));

set Kaplan_&p;

if _n_ =n then keep=1;

if keep ne 1 then delete;

drop keep survival;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
sam369
Obsidian | Level 7


Hi desireatem,

You can try this to get last record

data KSm_&p(rename=survival1=(survival_max time=time_max));

set Kaplan_&p nobs=nobs;

if _n_ =nobs then keep=1;

if keep ne 1 then delete;

drop keep survival;

run;

Thanks

Sam

View solution in original post

5 REPLIES 5
sam369
Obsidian | Level 7


Hi desireatem,

You can try this to get last record

data KSm_&p(rename=survival1=(survival_max time=time_max));

set Kaplan_&p nobs=nobs;

if _n_ =nobs then keep=1;

if keep ne 1 then delete;

drop keep survival;

run;

Thanks

Sam

Linlin
Lapis Lazuli | Level 10

example:

data first_last;

set sashelp.class nobs=nobs;

if _n_ =1 or _n_=nobs;

proc print;run;

                      Obs     Name      Sex    Age    Height    Weight

                          1     Alfred      M      14     69.0      112.5
                          2     William     M      15     66.5      112.0

Message was edited by: Linlin

Astounding
PROC Star

If your data set is large, you can skip reading the middle of it:

data want;

set have nobs=_nobs_;

output;

set have point=_nobs_;

output;

stop;

run;

Good luck.

Tom
Super User Tom
Super User

Need to watch for when NOBS=1 .

data class ;

  set sashelp.class (obs=1);

run;


data first_last ;

  retain point 1 ;

  set class point=point nobs=nobs ;

  output;

  if point = nobs then stop;

  else point = nobs ;

run;

jaredp
Quartz | Level 8

Get the last row with the the END option in the SET statement.

data want; set sashelp.class end=eof;

if eof then output;

run;

EOF is short for end of file.  Programmers like to use this term, but you can put whatever you want here.  For example, this would also work:

data want2; set sashelp.class end=awesome;

if awesome then output;

run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 33658 views
  • 8 likes
  • 6 in conversation