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

Hi all 

Best wishes to all of you for 2021

I am trying to find a simple solution  for the the data I have 

data have;
input cusip_id Name $10. date_issue :ddmmyy10. datadate :ddmmyy10.  ;
format date_issue ddmmyy10.;
format datadate ddmmyy10.;
datalines;
10343452 Henry PLC 01/04/2014  31/12/2012
10343452 Henry PLC 01/04/2014  31/12/2013
10343452 Henry PLC 01/04/2014  31/12/2014
10343452 Henry PLC 01/04/2014  31/12/2015
10343452 Henry PLC 01/04/2014  31/12/2016
;
run;
/*WHAT I WANT*/
data want ;
input cusip_id Name $10. date_issue :ddmmyy10. datadate :ddmmyy10.  ;
format date_issue ddmmyy10.;
format datadate ddmmyy10.;
datalines;
10343452 Henry PLC 01/04/2014  31/12/2013
;
run;

My solution:

data have;
     set have;
	 If dat_issue>=datadate;
run;
proc sort data=want out=want;
     by cusip descending datadate ;
run;
proc sort data=want out=want nodupkeys;
     by cusip  ;
run;

Is there  a way to be efficient and have this in a single step. Can this be done in proc sql when I join the cusip and issue_date with the dataset of compustat (that includes all the fiscal years i.ie datadate)? 

 

Many thanks in advance 

 

Regards

George

 

1 ACCEPTED SOLUTION

Accepted Solutions
singhsahab
Lapis Lazuli | Level 10
proc sql;
create table want as
select  *,monotonic() as row_num from have where date_issue ge datadate group by cusip_id having max(row_num)=row_num;
quit;

View solution in original post

4 REPLIES 4
singhsahab
Lapis Lazuli | Level 10
proc sql;
create table want as
select  *,monotonic() as row_num from have where date_issue ge datadate group by cusip_id having max(row_num)=row_num;
quit;
georgel
Quartz | Level 8
This is exactly what I wanted. Great Many thanks

Regards
George
Kurt_Bremser
Super User

If your dataset is already sorted in ascending order, a double DO loop does it:

data want;
do until (last.cusip);
  set have;
  by cusip;
  if datadate le date_issue then ref = datadate;
end;
do until (last.cusip);
  set have;
  by cusip;
  if datadate = ref then output;
end;
drop ref;
run;

In SQL, try this

proc sql;
create table want as
  select *
  from have
  where datadate le date_issue
  group by cusip
  having date_issue - datadate = min(date_issue - datadate)
;
quit;

Untested, posted from my tablet.

 

georgel
Quartz | Level 8
Dear Kurt
Many thanks for this solution and for your quick response. I really appreciated your help
Regards
George

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 867 views
  • 4 likes
  • 3 in conversation