Hi,
From a given SAS Dataset, how can I fetch last but one record or third record from the last without doing hard coding. I don't want to do hard coding the values as the number of observations keep on changing.
TIA.
Hi @VVDR Using SASHELP.CLASS dataset that has 19 records, here is an illustration-
The comments should make sense to you
/*last but one=numofrecs-1,third=last minus 2 records*/
data want;
/*last but one=numofrecs-1,third=last minus 2 records*/
do _n_= nobs-1, nobs-2;
set sashelp.class nobs=nobs point=_n_;
output;
end;
stop;
run;
Hi @VVDR Using SASHELP.CLASS dataset that has 19 records, here is an illustration-
The comments should make sense to you
/*last but one=numofrecs-1,third=last minus 2 records*/
data want;
/*last but one=numofrecs-1,third=last minus 2 records*/
do _n_= nobs-1, nobs-2;
set sashelp.class nobs=nobs point=_n_;
output;
end;
stop;
run;
Are these assignment questions?
Here one way to get what the question asked for. The code below selects the 3rd last column.
%let n_obs_from_last=3;
data want;
obs=nobs -&n_obs_from_last +1;
if obs>0 then
do;
set sashelp.class nobs=nobs point=obs;
output;
end;
stop;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.