data have;
input Name $ Date:date9.;
format date date9.;
datalines;
Ben 31Dec2013
Ben 31Jan2014
Ben 31Mar2014
Ben 30Apr2014
Ben 30Jun2014
Ken 31Mar2014
Ken 31May2014
;
run;
data want;
merge have have(rename=(name=_name date=_date) firstobs=2);
if name=_name then do;
do i=1 to intck('month',intnx('month',date,0),intnx('month',_date,0)-1);
want_date=intnx('month',date,i);output;
end;
end;
format want_date date9.;
keep name want_date;
run;
I'm merging them once I've made the list.
data have;
input Name $ Date:date9.;
format date date9.;
datalines;
Ben 31Dec2013
Ben 31Jan2014
Ben 31Mar2014
Ben 30Apr2014
Ben 30Jun2014
Ken 31Mar2014
Ken 31May2014
;
run;
proc sort data=have out=sorted;
by Name Date;
run;
data datelist_main;
set sorted;
by Name Date;
if first.name or last.name;
run;
data datelist(rename=(dt=DATE));
set datelist_main;
by Name Date;
retain dt;
if first.name then do;
dt=date;
output;
end;
if last.name then do;
do while(dt<Date);
dt=intnx('month',dt,1,'end');
output;
end;
end;
drop date;
run;
data want;
merge datelist(in=inA) have(in=inB);
by name date;
if inA and not(inB);
run;
Make sure that the "have" dataset is sorted by name and date, then do a "look-ahead":
data want;
merge
have
have (
firstobs=2
rename=(
name=_name
date=_date
)
)
;
output;
if name = _name
then do;
date = intnx('month',date,1,'e');
do while (date lt _date);
output;
date = intnx('month',date,1,'e');
end;
end;
drop _:;
run;
data have;
input Name $ Date:date9.;
format date date9.;
datalines;
Ben 31Dec2013
Ben 31Jan2014
Ben 31Mar2014
Ben 30Apr2014
Ben 30Jun2014
Ken 31Mar2014
Ken 31May2014
;
run;
data want;
merge have have(rename=(name=_name date=_date) firstobs=2);
if name=_name then do;
do i=1 to intck('month',intnx('month',date,0),intnx('month',_date,0)-1);
want_date=intnx('month',date,i);output;
end;
end;
format want_date date9.;
keep name want_date;
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.