Hi,
I am trying to find if a date in a variable falls under Month1,Month2, Month3,Month4....Month6 category using a do loop between startdt and incremented startdate i.e., Startdate+30.
By running the below logic, I get a value for the first record as Month2 and a value for rest of the Months too which is not correct. How can i fix this logic?
dataset--
start vardt enddt
01Jan2017 11Mar2017 01Jan2018
01Jan2017 02May2017 01Jan2018
01Jan2017 11Apr2017 01Jan2018
data test2;
set test1;
mindt=startdt;
maxdt=enddt;
array Months(6);
Mnth=30;
start=mindt;
do i = 1 to Months;
mnth=mnth+30;
stopdt=maxdt;
if startdt <= vardt<=startdt+mnth then Months(i)=1;
end;
start=start+mnth;
drop i;
run;
Thank you in advance.
data test1;
input start DATE9. vardt DATE9. enddt DATE9.;
FORMAT start Date9. vardt Date9. enddt Date9.;
DATALINES;
1jan2017 11Mar2017 1jan2018
1jan2017 02May2017 1jan2018
1jan2017 11Apr2017 1jan2018
;
run;
Data test2 (Drop = i);
set test1;
ARRAY Months{6};
DO i = 1 to DIM(Months);
IF vardt >= Start + (30*(i-1)) AND vardt <= Start + (30*i) THEN Months{i}=1;
END;
RUN;
Try this.
data test1;
input start DATE9. vardt DATE9. enddt DATE9.;
FORMAT start Date9. vardt Date9. enddt Date9.;
DATALINES;
1jan2017 11Mar2017 1jan2018
1jan2017 02May2017 1jan2018
1jan2017 11Apr2017 1jan2018
;
run;
Data test2 (Drop = i);
set test1;
ARRAY Months{6};
DO i = 1 to DIM(Months);
IF vardt >= Start + (30*(i-1)) AND vardt <= Start + (30*i) THEN Months{i}=1;
END;
RUN;
Try this.
Thank you Daniel.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.