hi all,
I got stuck at one point of my sas code it all about to retrieve only first and last record of my dataset
I tried in multiple ways in sas and SQL I succeed in SQL by using max and min statements in the code but till now I didn't get in sas
so kindly guide the current code for fetch only 2 records that are first and last record
in sas i tried if _n_ = 1;
but i got only first rec i want the last one too
Thnaks & regards
rohitkrishna
Try this:
data want;
set have (obs=1);
output;
set have point=nobs nobs=nobs;
output;
run;
nobs=nobs creates a variable (which will not be included in the output) that holds the total number of observations in the dataset. point=nobs uses this variable to retrieve only this last observation.
Since the first statement creates the necessary end-of-file condition to end the data step, no explicit stop statement is needed.
Just to cover the boundary condition that there is one and only one observation in the source dataset add and IF .
Otherwise the result will have the same observation twice.
data want;
set have (obs=1);
output;
if nobs>1;
set have point=nobs nobs=nobs;
output;
run;
Also if you cannot use the NOBS= and/or the POINT= option (perhaps your input is a VIEW instead of dataset) then here is method that will read the whole file and just keep the first and last observation.
data test;
set sashelp.class end=eof;
if _n_=1 or eof;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.