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

i have a dataset of the following kind

 

ID    ReportingDate   Start Date  Value
001   31Jan2018       31Jan2018   100
001   28Feb2018       31Jan2018   200
001   ....
002   30Sep2016       28Feb2014   400
002   31Oct2016       28Feb2014   500
002   ....

I need to create a condition such that only keep records with valid IDs where first instance of 'Start Date' is equal to or later than first instance of 'Reporting Date'. So essentially, ID 002 should not be kept in the dataset as the first Start Date is earlier than first Reporting Date.

 

I have been fiddling around with first. but cannot get the condition to work. Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
infile cards  truncover;
input ID    (ReportingDate   StartDate) (:date9.)  Value ;
format ReportingDate   StartDate date9.;	 
cards;
001   31Jan2018       31Jan2018   100
001   28Feb2018       31Jan2018   200
002   30Sep2016       28Feb2014   400
002   31Oct2016       28Feb2014   500
;

data want;
do until(last.id);
set have;
by id;
if first.id and  startdate>=reportingdate then f=1;
if f then output;
end;
drop f;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

data have;
infile cards  truncover;
input ID    (ReportingDate   StartDate) (:date9.)  Value ;
format ReportingDate   StartDate date9.;	 
cards;
001   31Jan2018       31Jan2018   100
001   28Feb2018       31Jan2018   200
002   30Sep2016       28Feb2014   400
002   31Oct2016       28Feb2014   500
;

proc sql;
create table want(drop=t) as
select *
from
(select * ,min(startdate)>=reportingdate as t from have group by id	)
group by id
having max(t)=1;
quit;
novinosrin
Tourmaline | Level 20
data have;
infile cards  truncover;
input ID    (ReportingDate   StartDate) (:date9.)  Value ;
format ReportingDate   StartDate date9.;	 
cards;
001   31Jan2018       31Jan2018   100
001   28Feb2018       31Jan2018   200
002   30Sep2016       28Feb2014   400
002   31Oct2016       28Feb2014   500
;

data want;
do until(last.id);
set have;
by id;
if first.id and  startdate>=reportingdate then f=1;
if f then output;
end;
drop f;
run;
eemrun
Obsidian | Level 7
perfect! thank you!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 878 views
  • 2 likes
  • 2 in conversation