*Needle in a haystack: searching for the year marital status changes from married to divorced.; * We use arrays here in a couple of ways. First is a recode. Then we have a temporary array for a table lookup of year. Finally, we pick the year based on a search across columns - finding the needle in the haystack.; libname y "y:\sas\arrays"; libname library (y); data nlswomen; set y.nlswomen; * for recode; array original{*} R0002400 R0133700 R0205100 R0288200 R0308300 R0367600 R0455600 R0491300 R0661400 R0666600 R0721700 R0869900 R0997700 R1290700 R1664710 R3507200 R4278200 R5447500 R6516200; array mstatus{19}; *for recoded values; do i=1 to dim(original); * this loop does recoding; if original{i}=2 then mstatus{i}=1; else mstatus{i} = original{i}; end; * table lookup, to translate waves to years; array year{19} _temporary_ (1967 1971 1972 1974 1976 1977 1979 1981 1982 1984 1986 1987 1989 1992 1995 1997 1999 2001 2003); *to use in table lookup; m2d = 0; do i=2 to dim(mstatus); * A loop searching for a specific change in marital status from married to divorced; if (mstatus{i-1} eq 1 and mstatus{i} eq 4) then do; m2d=1; yeardiv = year{i}; end; * because we loop over all the variables, this identifies the last time someone goes from married to divored; end; drop i; *We might soemtimes want to keep i, it identifies the wave; run; proc freq; tables m2d yeardiv; run;