Hello, I would like to create a flag (binary, 1 or 0) using a do loop, with a 2 month look back period to see if a test that an observation has taken a test (also a month-based binary flag) that had occurred in the current month, the previous month, and the month prior to the previous month (each test occurrence is based on a monthly flag of 1 or 0; 1 being a positive test) . For example, if the subject had a positive test in January and was also flagged positive for the test in December and November, then a new flag indicating that the months of January, December, and November will also be 1. This will also be performed for the following months for 12 more months. For more clarity, please the code here without the DO loop: data test_wide;
set test_merge;
*January;
if (test_in_mo_3 = 1) AND (test_in_mo_2 = 1) AND (test_in_mo_1 = 1) THEN test_3mo_1 = 1;
else test_3mo_1 = 0;
*Feb;
if (test_in_mo_4 = 1) AND (test_in_mo_3 = 1) AND (test_in_mo_2 = 1) THEN test_3mo_2 = 1;
else test_3mo_2 = 0;
*March;
if (test_in_mo_5 = 1) AND (test_in_mo_4 = 1) AND (test_in_mo_3 = 1) THEN test_3mo_3 = 1;
else test_3mo_3 = 0;
*April;
if (test_in_mo_6 = 1) AND (test_in_mo_5 = 1) AND (test_in_mo_4 = 1) THEN test_3mo_4 = 1;
else test_3mo_4 = 0;
*May;
if (test_in_mo_7 = 1) AND (test_in_mo_6 = 1) AND (test_in_mo_5 = 1) THEN test_3mo_5 = 1;
else test_3mo_5 = 0;
*June;
if (test_in_mo_8 = 1) AND (test_in_mo_7 = 1) AND (test_in_mo_6 = 1) THEN test_3mo_6 = 1;
else test_3mo_6 = 0;
*July;
if (test_in_mo_9 = 1) AND (test_in_mo_8 = 1) AND (test_in_mo_7 = 1) THEN test_3mo_7 = 1;
else test_3mo_7 = 0;
*August;
if (test_in_mo_10 = 1) AND (test_in_mo_9 = 1) AND (test_in_mo_8 = 1) THEN test_3mo_8 = 1;
else test_3mo_8 = 0;
*Sept;
if (test_in_mo_11 = 1) AND (test_in_mo_10 = 1) AND (test_in_mo_9 = 1) THEN test_3mo_9 = 1;
else test_3mo_9 = 0;
*Oct;
if (test_in_mo_12 = 1) AND (test_in_mo_11 = 1) AND (test_in_mo_10 = 1) THEN test_3mo_10 = 1;
else test_3mo_10 = 0;
*Nov;
if (test_in_mo_13 = 1) AND (test_in_mo_12 = 1) AND (test_in_mo_11 = 1) THEN test_3mo_11 = 1;
else test_3mo_11 = 0;
*Dec;
if (test_in_mo_14 = 1) AND (test_in_mo_13 = 1) AND (test_in_mo_12 = 1) THEN test_3mo_12 = 1;
else test_3mo_12 = 0;
run; Please note that test_in_mo_2 and test_in_mo_1 flags refer to the months of December and November (of the previous year), respectively. The flag variable test_in_mo_3 refers to January, test_in_mo_4 refers to February, and so on until test_in_mo_14 refers to December of the current year. I've been struggling to make this code into a DO loop, any advice or code would be greatly appreciated. I am on SAS 9.3. Thank you in advance.
... View more