Hi brettmullga, Please find below the solution using datastep and sql. I have created a dataset based on the information you gave and tried to get the result in the final dataset. If you have more months column then the coding would need to be updated. Please let me know if you have any questions. All the best. Thanks. Rafi DATA original; INPUT Participant Month1 Month2 Month3 Month4; CARDS; 1 8 1 . 9 2 . . 3 15 3 4 5 . 12 4 1 . . 10 ; RUN; * Count total for each month; PROC SQL; CREATE TABLE m_tot AS SELECT *, COUNT (*) as total, COUNT(month1) AS m1_tot, COUNT(month2) AS m2_tot, COUNT(month3) AS m3_tot, COUNT(month4) AS m4_tot FROM original ORDER BY participant; QUIT; * Using datasetp - 1) flag patient movement in each month and 2) flag patient who has value in previous month(s); DATA flag; SET m_tot; IF month1 NE . AND month2 NE . THEN m1tom2f = 1; IF month2 NE . AND month3 NE . THEN m2tom3f = 1; IF month3 NE . AND month4 NE . THEN m3tom4f = 1; IF month2 NE . AND month1 NE . THEN m2tom1f = 1; IF month3 NE . AND N(month1,month2) GE 1 THEN m3tom1f = 1; IF month4 NE . AND N(month1,month2,month3) GE 1 THEN m4tom1f = 1; RUN; * Using proc sql - count necessary values; PROC SQL; CREATE TABLE finalx AS SELECT *, COUNT(m1tom2f) AS m1tom2, COUNT(m2tom3f) AS m2tom3, COUNT(m3tom4f) AS m3tom4, COUNT(m2tom1f) AS m2tom1, COUNT(m3tom1f) AS m3tom1, COUNT(m4tom1f) AS m4tom1 FROM flag ORDER BY participant; QUIT; * Final dataset where all the necessary information for outputs will be found; DATA final; SET finalx (DROP=participant month1 month2 month3 month4 m1tom2f m2tom3f m3tom4f m2tom1f m3tom1f m4tom1f) END=eof ; IF eof; LABEL total = 'Toatl participant' m1_tot = 'Total participant in Month 1' m2_tot = 'Total participant in Month 2' m3_tot = 'Total participant in Month 3' m4_tot = 'Total participant in Month 4' m1tom2 = 'Participated in Month 1 and Month 2' m2tom3 = 'Participated in Month 2 and Month 3' m3tom4 = 'Participated in Month 3 and Month 4' m2tom1 = 'Participated in Month 2 and in earlier month(s)' m3tom1 = 'Participated in Month 3 and in earlier month(s)' m4tom1 = 'Participated in Month 4 and in earlier month(s)' ; RUN;
... View more