Data SASdat.Emp;
input EMP_ID Name $ Salary Sal_Date date9.;
format Sal_Date date9.;
datalines;
101 A 10000 31-May-2016
102 B 15000 30-Jun-2016
103 C 20000 31-Jul-2016
104 D 18000 31-Aug-2016
105 E 13000 30-Sep-2016
106 F 80000 31-Oct-2016
107 G 30000 30-Nov-2016
108 H 35000 31-Dec-2016
;
I need only Emp_Id with 107
Hi @Kraj1989
Just a direct access
Data Emp;
input EMP_ID Name $ Salary Sal_Date date9.;
format Sal_Date date9.;
datalines;
101 A 10000 31-May-2016
102 B 15000 30-Jun-2016
103 C 20000 31-Jul-2016
104 D 18000 31-Aug-2016
105 E 13000 30-Sep-2016
106 F 80000 31-Oct-2016
107 G 30000 30-Nov-2016
108 H 35000 31-Dec-2016
;
data want;
i=nobs-1;
set emp nobs=nobs point=i;
output;
stop;
run;
Hi @Kraj1989
Just a direct access
Data Emp;
input EMP_ID Name $ Salary Sal_Date date9.;
format Sal_Date date9.;
datalines;
101 A 10000 31-May-2016
102 B 15000 30-Jun-2016
103 C 20000 31-Jul-2016
104 D 18000 31-Aug-2016
105 E 13000 30-Sep-2016
106 F 80000 31-Oct-2016
107 G 30000 30-Nov-2016
108 H 35000 31-Dec-2016
;
data want;
i=nobs-1;
set emp nobs=nobs point=i;
output;
stop;
run;
Alternatively,
data want;
set emp nobs=nobs ;
if _n_=nobs-1;
run;
@Kraj1989 wrote:
Data SASdat.Emp;
input EMP_ID Name $ Salary Sal_Date date9.;
format Sal_Date date9.;
datalines;
101 A 10000 31-May-2016
102 B 15000 30-Jun-2016
103 C 20000 31-Jul-2016
104 D 18000 31-Aug-2016
105 E 13000 30-Sep-2016
106 F 80000 31-Oct-2016
107 G 30000 30-Nov-2016
108 H 35000 31-Dec-2016
;
I need only Emp_Id with 107
Here's a silly question: Do you actually need Emp_id = 107 regardless of sort order or the 2nd to last record regardless of actual values stored in that record?
The content of values is a much more common question because data order can change.
If the Emp_id is what you want then
data want; set sasdat.emp; where Emp_id=107; run;
As ballardw notes, the question of whether you want the next to last record is physical order or in sorted order is an important one.
The approach using NOBS= is potentially dangerous. It works only for native SAS data sets (or data step views) that have not been edited. It potentially does not work for data sets in a database, or transport data sets, or data sets that have been edited.
Using the data above
Data Emp;
input EMP_ID Name $ Salary Sal_Date : date9.;
format Sal_Date date9.;
datalines;
101 A 10000 31-May-2016
102 B 15000 30-Jun-2016
103 C 20000 31-Jul-2016
104 D 18000 31-Aug-2016
105 E 13000 30-Sep-2016
106 F 80000 31-Oct-2016
107 G 30000 30-Nov-2016
108 H 35000 31-Dec-2016
;
proc sql;
create table want as
select *
from emp
having monotonic()=count(*)-1;
quit;
Interesting feedback.
So no NOBS= or MONOTONIC()
Data Emp;
input EMP_ID Name $ Salary Sal_Date : date9.;
format Sal_Date date9.;
datalines;
101 A 10000 31-May-2016
102 B 15000 30-Jun-2016
103 C 20000 31-Jul-2016
104 D 18000 31-Aug-2016
105 E 13000 30-Sep-2016
106 F 80000 31-Oct-2016
107 G 30000 30-Nov-2016
108 H 35000 31-Dec-2016
;
data want;
set emp end=lr;
n+1;
if lr;
n=n-1;
set emp point=n;
output;
run;
Yes aware of attrn, but how about
data want ;
if _n_=1 then do;
dcl hash H () ;
h.definekey ("_n_") ;
h.definedata ("EMP_ID"," Name"," Salary"," Sal_Date ") ;
h.definedone () ;
end;
set emp end=lr;
h.add();
if lr ;
_n_=_n_-1;
_iorc_=h.find() ;
run;
Only need the END= and FIRSTOBS= options to work. That should work for any dataset, including sequential access datasets (like XPORT files, views or what SAS used to call TAPE datasets).
data want ;
set emp;
set emp (firstobs=2 drop=_all_) end=eof;
if eof ;
run;
So on the next to last observation the EOF will be true. On the last observation the second SET will read past the end and the data step will stop before the IF statement is reached.
If there are less than 2 observations then none will be written.
Same method can be used to find the 3rd to last, 4th to last etc by just changing the value of the FIRSTOBS= option.
Hello @Kraj1989 Something I noticed just now which I should have earlier is that your date values are sorted and denote the last day of the month. If my observation is correct, a simple approach like the following is convenient
Data Emp;
input EMP_ID Name $ Salary Sal_Date : date9.;
format Sal_Date date9.;
datalines;
101 A 10000 31-May-2016
102 B 15000 30-Jun-2016
103 C 20000 31-Jul-2016
104 D 18000 31-Aug-2016
105 E 13000 30-Sep-2016
106 F 80000 31-Oct-2016
107 G 30000 30-Nov-2016
108 H 35000 31-Dec-2016
;
proc sql;
create table want as
select *
from emp
having Sal_Date=intnx('month',max(Sal_Date),-1,'e');
quit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.