@tahos wrote:
Before going into macros I hard-coded the procedure as multiple data-steps and it worked. It seems weird that it would be not possible to do this with macros, which would be more practical as I could then use the same logic for multiple other operations which require me to get monthly information then I am only given the beginning and the end date.
It is possible to do this with macros, but not more efficient at all.
1. Determine the maximum number of months
2. Use an array to fill in your 0/1
First figure out how many possible p values you can have. If you know this ahead of time, its much easier.
data have;
informat mydate1 mydate2 $10.;
input id $ employed $ dummy $ mydate2 mydate1 ;
datalines;
1 true true 01JAN2019 03MAR2019
2 false false 01JAN2019 01JAN2020
3 true true 02FEB2019 04JUN2019
4 true true 01JAN2019 01JAN2020
;
%macro date_loop(start,end);
%*Calculate number of months needed from macro parameters;
%let start=%sysfunc(inputn(&start,date9.));
%let end=%sysfunc(inputn(&end,date9.));
%let dif=%eval(%sysfunc(intck(month,&start,&end)) + 1) ;
data want;
set have;
*convert variables to SAS dates for date calculations;
date_start = input(mydate2, date9.);
date_end = input(mydate1, date9.);
format date_start date_end date9.;
*Declare an array of the size needed;
array p(*) p1-p&dif.;
*Initialize array values to 0;
do i=1 to dim(p);
p(i) = 0;
end;
*calculate number of months for that record;
nMonths = intck('month', date_start, date_end, 'C');
*calculate starting index (number of months from Start);
nMonths2Start = intck('month',&start, date_start, 'C');
*set to 1 when conditions are met;
do i = nMonths2Start+1 to (nMonths2Start+nMonths+1);
if employed = 'true' or dummy = 'true' then p(i) = 1;
end;
run;
%mend date_loop;
*test macro;
options mprint symbolgen;
%date_loop(01jan2019, 01jan2020);
Can't test it right now but another option you can consider.
... View more