hello,
I want to select the last observation by crsp_portno
and REPORT_DT (is a date)
data first_by_group;
set number_stocks ;
by crsp_portno REPORT_DT;
if last.REPORT_DT then output;
run;
if last.crsp_portno;
If you want the last observation for a given crsp_portno, then you use the above. I realize that my sentence is not what you said. You said "last observation by id and date", in which case the code you showed ought to work fine (and so, what is wrong with it?) Which brings up another point ... please do NOT show us your code and then not even bother to tell us what is wrong with it.
hello,
I want the last observation by date and id or my code give the last observation by id only
thanks a lot
@sasphd wrote:
I want the last observation by date and id or my code give the last observation by id only
This doesn't make sense. Your code will give the last observation for each value of REPORT_DT. Please show us a portion of your actual data, provided as working SAS data step code (examples and instructions).
Your code will produce one observation for each crsp_portno*report_dt combination. So if some portfolios had multiple obs for a given report_dt, then you would get the last one of that set, for each report_dt.
data want;
set number_stocks ;
by crsp_portno REPORT_DT;
if last.REPORT_DT then output;
run;
But as @PaigeMiller and @Reeza suspect (and so do I) you likely have one record for each crsp_portno*report_dt combination, and you want only the last one. i.e. the most recent date for each portfolio. If so, then @PaigeMiller's solution is what you want.
If this doesn't help, then please provide sample data, in the form of a working data step, and the expected output from that sample.
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!
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.