Skip to main content
National Longitudinal Survey of Youth 1979 (NLSY79)

NLSY79 Appendix 15: Recipiency Event History Data

The NLSY79 surveys have solicited information about program recipiency since the first round in 1979. Methods used to collect information about program recipiency from 1979-1992 and from 1993 to the present differed. These methods are described below. The variables found in the RECIPIENT MONTH and RECIPIENT YEAR areas of interest have been constructed to provide users with a more straightforward sequence of variables containing monthly and yearly receipt amounts (if any) and flags for the survey year in which the data was reported. The roots of these question names are contained in the table below. Recipiency event history variables exist for five types of recipiency (question name roots in parentheses): AFDC (Q13A-), Food Stamps (Q13F-), SSI/SSDI/other public assistance/welfare (Q13SSI-), unemployment compensation (Q13U-), and spouse/partner unemployment compensation (Q13S-). Since survey year 2018, respondents have been asked to distinguish between SSI and SSDI if possible. Array items are present from January 2017 through the current interview.

Recipient Month Question Name Roots Recipient Year Question Name Roots
UNEMPR-[MOYR]-AMT UNEMPR-TOTAL-[YEAR]
UNEMPSP-[MOYR]-AMT UNEMPSP-TOTAL-[YEAR]
AFDC-[MOYR]-AMT AFDC-TOTAL-[YEAR]
FDSTMPS-[MOYR]-AMT FDSTMPS-TOTAL-[YEAR]
SSI-[MOYR]-AMT SSI-TOTAL-[YEAR]
SSDI-[MOYR]-AMT SSDI-TOTAL-[YEAR]
[MOYR]-RECIP-FILL WELFARE-AMT-[YEAR]

The current NLSY79 release contains revised recipiency event history data for survey years 1979-2002. Some anomalies in the construction of the recipiency event history data in previous releases necessitated these revisions. Researchers should use only the recipiency variables with reference numbers beginning with "G." These include the revised variables for survey years 1979-2002 (containing data from calendar years 1978 through the 2002 interview) and the most up-to-date data series for 2003 through the most recent survey. The unrevised original series has been included in the release as a non-primary set of variables for historical purposes.

This appendix first contrasts the collection of information on recipiency in the paper and pencil interviewing (PAPI) years (1979-1992) to that of the computer-assisted personal interviewing (CAPI) years (1993-present). It then describes the creation processes for the recipiency event history variables. A discussion of the kinds of problems necessitating the revisions (PDF) is available.

Program recipiency in paper-and-pencil interviews

In paper-and-pencil (PAPI) NLSY79 rounds (1992 and prior), information on R and spouse unemployment compensation, AFDC, Food Stamps and other welfare recipiency was gathered for the calendar year prior to the interview year only. For instance, someone interviewed in 1992 was asked about the months of recipiency in 1991 only. An average figure per week/month was then asked for the entirety of 1991. For example, if a respondent said s/he was receiving AFDC in March, April and May of 1991, and again in September and October of 1991, s/he was only asked for an average amount per month received during those months in 1991.

Data collected in this manner generates a complete event history only for respondents who were interviewed at each interview date. For those respondents, information would be present for each month benefits were received from January 1978 through December 1991 (the year before the 1992 interview). However, a respondent skipping one or more interviews would be missing information for each calendar year preceding missed interview years. For example, a respondent missing the 1985 and 1990 interviews would be missing recipiency information for calendar years 1984 and 1989.

Program recipiency in CAPI interviews

In the computer-assisted-personal-interviewing (CAPI) NLSY79 rounds (1993 - present) respondents are asked about receipt from government programs since their last interview. Surveys through the 1990s (1993, 1994, 1996 and 1998) also contain some specialized questions to aid the transition from the previous method to the current method of collection and to minimize the number of respondents with any interruptions in their month-to-month event histories.

Respondents are now asked if they have received benefits at all since their date of last interview. Individuals who report no recipiency since their last interview year to the next section of the interview. If the respondent answers "yes," s/he is then asked for the date when the benefits began. This is considered the first spell. Respondents are then asked if benefits have been received continuously since this start date. If the respondent answers "yes," receipt has been continuous, s/he is asked for the average dollar amount received per month/week for each year in the spell. These respondents then proceed to the next section of the interview. If the respondent answers that receipt has not been continuous since this first start date, s/he is asked to report the first date s/he stopped receiving benefits. Average dollar figures per month/week are collected for each year within this first spell.

All respondents who report completing a first spell since their last interview are asked if they started receiving benefits again since the first spell ended. In the 1993, 1994 and 1996 survey years, Information on up to five spells is collected in the manner described above. If there are more than five spells, the respondent is asked about the first five and the most recent. Beginning in 1998, respondents were asked about all spells of receipt. 

In interviews following the initial CAPI interview (1993 in most cases), respondents are asked to verify the last month they reported receiving benefits (if any). They are then asked about any recipiency since their date of last interview. In the electronic questionnaires, the retrospective recipiency event history is collected from the date of the last interview, providing a more continuous longitudinal record, even for respondents who skip interviews.

The flow of questions in the recipiency modules is illustrated in Figure A15.1.

Figure A15.1 Flow of Program Recipiency Questions in CAPI Interviews

In the 2018 survey, some updates were made to the Income module of the questionnaire, including some relatively minor updates to the recipiency-related segments. While the structure of each of the recipiency question series remained essentially the same, the SSI questions (beginning with Q13SSI-) were split into separate segments for the respondent and/or any dependent children (Q13SSI-), and a spouse/partner (Q13SSI-SP-) if applicable. In addition, respondents were asked to differentiate between SSI and SSDI if they know which they are receiving. In addition, specification was added to the TANF segment of questions (beginning with Q13A-) to specify that the questions refer to dependent children where applicable. Finally, language referencing SNAP was added to the Food Stamps segment of questions (Q13F-) as the program has been renamed.

Variable creation

PAPI interviews

For most of the PAPI years, the yearly and monthly receipt/non-receipt variables are taken directly from responses, and the average monthly value of benefits is used for each month that the respondent reports receiving benefits. For unemployment compensation, weekly averages were collected. This weekly average was multiplied by 4.3 and then used as the monthly average. However, there are two main exceptions to this. First, the Food Stamp program underwent a change in 1979. Prior to this, recipients were allowed to purchase food stamps at a price below their market value. Because the 1979 interview asked respondents about recipiency in 1978, respondents who reported receiving food stamps were asked how much they paid for the food stamps in addition to the dollar value of the food stamps received in the last month they received benefits in 1978. The net transfer for 1979 is estimated by subtracting the dollar amount paid from the dollar value received. In all subsequent years, respondents were only asked for the dollar value received in the last month of the previous year that benefits were received.

The second exception concerns SSI and other forms of public assistance/welfare. The series of questions pertaining to public assistance/welfare and SSI has undergone some changes since the beginning of the survey. Initially, in 1979, respondents were asked in a single question if they had received income from any of the sources mentioned above. Respondents were also asked in which months benefits were received and the average amount received each month. They were then asked to identify from which sources they received benefits. However, it is not possible to identify how much of this amount is attributable to each source if more than one source was reported.

From 1980 through 1984, the question was divided into two separate ones. Respondents were first asked if they had received any benefits from SSI in the preceding year. They were then asked in which months benefits were received and the average amount received each month. A second set of questions asked respondents if they had received public assistance/welfare in the preceding year and, if so, in which months and the average amount received each month.

The format of the questions was changed once again in 1985 and remained the same through 1996. As with the initial interview in 1979, respondents were asked if they had received any benefits from SSI, public assistance/welfare. They were then asked in which months benefits were received and the average amount received each month. However, unlike the 1979 interview, respondents were not asked to identify the source of the benefits.

In 1998, to address major welfare reform policies passed in 1996, the series was altered once again. Respondents were asked first about SSI receipt only. A separate series of questions was added to solicit information on other types of general public assistance.

The final changes were made for the 2000 interview and have been in effect since that time. Information on other forms of public assistance was curtailed significantly. The series of questions collecting information about SSI remained in the instrument.

Users should be aware that the responses since the 2000 interview contain the least amount of information, pertaining to SSI receipt. This is reflected in the recipiency event history variables since 2000 as well.

CAPI interviews

Due to the way PAPI interviews collected data (for the calendar year prior to the survey year), information on recipiency is available beginning with January of 1978. Designating this to be month 1 of the monthly event history, all start and stop dates can be identified by their month number. This may be easily calculated using the following algorithm: month_# = (year - 1978) x 12 + month. For instance, June of 1993 would be: (1993 - 1978) x 12 + 6 = 186. Once all start and stop dates have been calculated, the event history for each individual can be created.

To illustrate this, consider Case 1 from Table A15.1. This respondent was not interviewed in 1992 which means that her/his event history from the PAPI survey years would contain information up through December of 1990. Thus, the beginning month of the CAPI event history would be January of 1991 (month 157). According to the example, this respondent was receiving benefits in December of 1991 and continued to do so until June of 1991 (month 162) and then received no further benefits. The dollar amount event history would then be formed by placing dollar value reported for the average benefits in 1991, $135, into months 157 - 162 and zeros into the dollar amounts for months 163 - 186. This same logic can be applied to each respondent, regardless of the number of reported spells of recipiency: placing reported dollar amounts into all months within a spell (from start_spell(i) to stop_spell(i) ) and zeros into all months outside of spells (1 + stop_spell(i) to start_spell(i+1) -1).

To illustrate more completely how each respondent's event history was created, Table A15.1 depicts four additional hypothetical cases. Cases 2 and 4 represent respondents who receive continuously after their start dates; Case 3 depicts a respondent who reports no benefit receipt; and Case 5 represents a respondent who reports two completed spells of recipiency. Table A15.2 presents the event histories which would result if the information had been given by the respondents portrayed in Table A15.1.

Table A15.1 Five hypothetical CAPI cases

Question

Case #1 Case #2 Case #3 Case #4 Case #5

Interview date

6/93 6/93 10/93 7/93 8/93

Year of last interview

1991 1991 1991 1991 1991

Receive Dec year before last interview?

Y Y N N N

Spell_0 continuous?

N Y      

First stop date spell_0

6/91        

Average monthly/weekly benefits in '91 months (rec'd Dec or year before last int)

135        

Receive since Jan of last interview?

    N Y Y

Start date spell_1

      3/91 3/91

New spell since stop date spell_0

         

Start date spell_1

         

Spell_1 continuous?

      Y N

Stop date spell_1

        9/91

Average monthly/weekly benefits in '91 months (1st new spell)

        200

New spell since stop date spell_1?

        Y

Start date spell_2

        2/93

Spell_2 continuous?

        N

Stop date spell_2

        5/93

Average monthly/weekly benefits in '93 months (2nd new spell)

        225

New spell since stop date spell_2?

        N

Average monthly/weekly benefits in '91 months (rec'd contn'ly since last start date)

  145   157  

Average monthly/weekly benefits in '92 months (rec'd contn'ly since last start date)

  152   160  

Average monthly/weekly benefits in '93 months (rec'd contn'ly since last start date)

  175   163  
Table A15.2. Resultant event histories
Month/Year Case #1
Dollar
Case #2
Dollar
Case #3
Dollar
Case #4
Dollar
Case #5
Dollar
1/91 135 145 0 0 0
2/91 135 145 0 0 0
3/91 135 145 0 157 200
4/91 135 145 0 157 200
5/91 135 145 0 157 200
6/91 135 145 0 157 200
7/91 0 145 0 157 200
8/91 0 145 0 157 200
9/91 0 145 0 157 200
10/91 0 145 0 157 0
11/91 0 145 0 157 0
12/91 0 145 0 157 0
1/92 0 152 0 160 0
2/92 0 152 0 160 0
3/92 0 152 0 160 0
4/92 0 152 0 160 0
5/92 0 152 0 160 0
6/92 0 152 0 160 0
7/92 0 152 0 160 0
8/92 0 152 0 160 0
9/92 0 152 0 160 0
10/92 0 152 0 160 0
11/92 0 152 0 160 0
12/92 0 152 0 160 0
1/93 0 175 0 163 0
2/93 0 175 0 163 225
3/93 0 175 0 163 225
4/93 0 175 0 163 225
5/93 0 175 0 163 225
6/93 0 175 0 163 0
7/93 -4 -4 0 163 0
8/93 -4 -4 0 -4 0
9/93 -4 -4 0 -4 -4
10/93 -4 -4 0 -4 -4
11/93 -4 -4 -4 -4 -4
12/93 -4 -4 -4 -4 -4

In each CAPI interview from 1993 through the present survey year, information is collected for all time up to the current interview date. Because all respondents are not interviewed in the same month, the resultant event histories would be of unequal length. In order to avoid this, a -4 is placed into each monthly dollar value from the month following the interview month to last month of the field period for the most recent survey. These -4s function merely as place savers and will be replaced by information collected from the next interview. For example, if the respondent represented by Case 1 is interviewed in September of 1994 and reports no benefit receipt since the last year, then the -4s for July to December of 1993 become 0s and -4s are placed in the dollar values for October to December of 1994. These new -4s would later be replaced by information from the 1996 interview.

Handling don't knows and refusals

In PAPI years when the respondent did not know whether s/he had received benefits in the previous year, a "-2" was placed in all months and dollar values for that year. For respondents who refused to answer this question, "-1" was entered into all months and dollar values for that year.

In CAPI years, when asked for the start or stop date of a spell, a respondent could respond "don't know." When the respondent does not know (or refuses to answer) the start date of a spell of recipiency, s/he is then asked approximately how many months/weeks s/he received benefits and how much s/he received in the last month/week s/he received benefits. If a respondent does not know the start date and there are valid responses for these questions (i.e., responses greater than zero), the start date is set at the first possible point of the unfilled event history and "-2" is placed into the months that the respondent reports receiving. For example, if a respondent last interviewed in 1990 and being interviewed in 1993 responds that s/he has received benefits since January of the last interview year but does not know when s/he started receiving, the start date is set at January of 1990. If this same respondent reports that s/he received benefits for six months and received $200 the last month s/he received benefits, then "-2" would be filled into the January through June of 1990 dollar values for these months. The dollar value monthly variable for July 1990 through the interview date would then be filled with zeros. If the respondent does not know the stop date but has reported a start date, the same logic is employed using the reported start date.

Fill flags

The recipiency variables have been assigned an XRND survey year classification. The electronic questionnaire format collects recipiency information from years when an individual has missed an interview. This allows variables for past years to be updated with data from the most recent survey, similar to the Work History arrays. The traditional non-interview code "-5" is not found in these data. In order to identify the "true" non-interviews in each year, a series of fill flags have been created. These are monthly variables which indicate the interview year from which the information was collected. For the vast majority of the PAPI years, the data will have come from the interview after that calendar year, i.e., data for March 1985 will have been provided in the 1986 interview. Respondents for whom variables representing months between 1978 and 1991 that are not filled in this manner (from the interview year after that calendar year) will have generally missed a number of interviews and had their information filled in from a CAPI survey year years later, when the event history format began collecting data from the date of last interview.

Programs for Recipiency Event Histories (survey year 2022)

Click below to view programming code.

Respondent Unemployment Benefits Receipt

%let cEndYr = 123;		*(2023-1900);
%let cEndMo = 1488;     *(123+1)*12;
%let cYrs = 46;			*(2023-1978)+1;
%let cMos = 552;		*46*12;
%let cFillFlag = 2022;

data Q13;
 set prg2022.in_inc_2022;
 
VARIABLE NAMES IN PUBLIC RELEASE		VARIABLE NAMES IN PROGRAM
  Q13RECIP-2_2022       			RECIP2                                      
  Q13U-43B_2022         			q43B
  Q13U-43C~M_2022       			q43CM
  Q13U-43C~Y_2022       			q43CY
  Q13U-44A1_CHK1.01_2022			q44A1CHK101
  Q13U-44A1_CHK1.02_2022			q44A1CHK102
  Q13U-44A1_CHK1.03_2022			q44A1CHK103
  Q13U-44A1_CHK1.04_2022			q44A1CHK104
  Q13U-44A3.01_2022     			q44A301
  Q13U-44A3.02_2022     			q44A302
  Q13U-44A3.03_2022     			q44A303
  Q13U-44A3.04_2022    				q44A304
  Q13U-44A5.01_2022     			q44A501
  Q13U-44A6.01_2022     			q44A601
  Q13U-44B.01_2022    				q44B01
  Q13U-44B.02_2022      			q44B02
  Q13U-44B.03_2022      			q44B03
  Q13U-44B.04_2022      			q44B04
  Q13U-44C.02~M_2022    			q44C02M
  Q13U-44C.02~Y_2022    			q44C02Y
  Q13U-44C.03~M_2022    			q44C03M
  Q13U-44C.03~Y_2022    			q44C03Y
  Q13U-44C.04~M_2022    			q44C04M
  Q13U-44C.04~Y_2022    			q44C04Y
  Q13U-44D.01_2022      			q44D01
  Q13U-44D.02_2022      			q44D02
  Q13U-44D.03_2022      			q44D03
  Q13U-44D.04_2022      			q44D04
  Q13U-45A.01_2022      			q45A01
  Q13U-45A.02_2022      			q45A02
  Q13U-45A.03_2022      			q45A03
  Q13U-45A.04_2022      			q45A04
  Q13U-45B.01~M_2022    			q45B01M
  Q13U-45B.01~Y_2022    			q45B01Y
  Q13U-45B.02~M_2022    			q45B02M
  Q13U-45B.02~Y_2022    			q45B02Y
  Q13U-45B.03~M_2022    			q45B03M
  Q13U-45B.03~Y_2022    			q45B03Y
  Q13U-45B.04~M_2022    			q45B04M
  Q13U-45B.04~Y_2022    			q45B04Y
  Q13U-47E.01_2022      			q47E01
  Q13U-47E.02_2022      			q47E02
  Q13U-47E.03_2022      			q47E03
  Q13U-47E.04_2022      			q47E04
  Q13U-62.01_2022       			q6201
  Q13U-62.02_2022       			q6202
  Q13U-62.03_2022       			q6203
  Q13U-62.04_2022       			q6204
  Q13U-62A.01_2022      			q62A01
  Q13U-62A.02_2022      			q62A02
  Q13U-62A.03_2022      			q62A03
  Q13U-62A.04_2022      			q62A04
  Q13U-63.01_2022       			q6301
  Q13U-63A.01_2022      			q63A01
  Q13U-63AA.01_2022     			q63AA01
  Q13U-63B.01_2022      			q63B01
  Q13U-64.01_2022       			q6401
  Q13U-64.02_2022       			q6402
  Q13U-64.03_2022       			q6403
  Q13U-64.04_2022       			q6404
  CURDATE~D_2022        			intDay     
  CURDATE~M_2022        			intMo 
  CURDATE~Y_2022        			intYr
  
data recip1;
 merge R30.lintdate2022 Q13 prg2022.oldin2022 (keep=norcid uydolo:);
 by norcid;
 if (intMo > 0); 
recip = RECIP2;
                   
data recip;
 set recip1;
 
* convert Y2K changes to simple years;
array yrs (*) intYr q43cy
              q44c01y q44c02y q44c03y q44c04y 
              q45b01y q45b02y q45b03y q45b04y;
do i=1 to dim(yrs);
    if yrs(i) > 0 then yrs(i)=yrs(i)-1900;
end;

* date of this interview;
intDt=intYr*12+intMo;
/* ==========================================================================
  Merge info from several sources + convert weeks to months.
  ========================================================================== */
dummy1 = -4; 
dummy2 = -4; 
dummy3 = -4; 

if q44c01y>-4 and q43cy>-4 and q44c01y<q43cy then do;
	q44c01y=q43cy;
	q44c01m=q43cm;
end;
else if q44c01y>-4 and q43cy>-4 and q44c01y>q43cy then do;
	q44c01y=q44c01y;
	q44c01m=q44c01m;
end;
else if q44c01y>-4 and q43cy>-4 and q44c01y=q43cy then do;
	q44c01m = max(q44c01m, q43cm);
	q44c01y = max(q44c01y, q43cy);
end;
else if q44c01y<=-4 and q43cy>-4 then do;
	q44c01m = max(q44c01m, q43cm);
	q44c01y = max(q44c01y, q43cy);
end;
/* nrOfRMo - # of months x was received (used when start/stop dates are not known).*/
array nrOfRMos  (*) q63a01 dummy1 dummy2 dummy3;
nrOfRMos(1) = max(nrOfRMos(1), q63aa01, q63ab01);
do s = 1 to dim(nrOfRMos);
    if nrOfRMos(s) > 0 then
        nrOfRMos(s) = max(round(nrOfRMos(s)/4.3),1);  
end;
* How much did R get during each month (by spells);
array howMuch (*) q47e01-q47e04;   
array q63b    (*) q63b01-q63b04;   
do s=1 to dim(howMuch);
    if howMuch(s) = -4 then howMuch(s) = q63b(s);             
    if howMuch(s) > 0 then howMuch(s)=round(howMuch(s)*4.3,1);
end;

/* ==========================================================================
   Recipiency status (rec) and amounts (dol) by months
   All months from 1978jan till dec of current year, indexed from 1900jan.
   78*12+1 = 937
   ========================================================================== */
array rec (937:&cEndMo);      * Did they receive x in that month?;
array dol (937:&cEndMo);      * How much?;

/* ========================================================================== 
 * No/unknown recipiency
 * ========================================================================== */
if recip=0 then do;
    do m=lint to intDt;
        rec(m)=-4;
        dol(m)=-4;
    end;
end;
else if recip in (-1,-2) then do;
    do m=lint to intDt;
        rec(m) = Recip;
        dol(m) = Recip;
    end;
end;
else if recip in (-3) then do;
    do m=lint to intDt;
        rec(m) = Recip;
        dol(m) = Recip;
    end;
end;

/* ==========================================================================
 * There was some recipiency, get details 
 * ========================================================================== */
else do;
check = 0;
array startMo (*) q44c01m q44c02m q44c03m q44c04m;
array startYr (*) q44c01y q44c02y q44c03y q44c04y;
array endMo (*) q45b01m q45b02m q45b03m q45b04m;
array endYr (*) q45b01y q45b02y q45b03y q45b04y;
array cont    (*) q44d01-q44d04;
array q44b    (*) q44b01-q44b04;
array q64     (*) q6401-q6404;  

/* ==========================================================================
 * Calculate starts and ends of spells if possible
 * ========================================================================== */
array starts (*) starts1-starts4; 
array ends   (*) ends1-ends4;     
* Continuous recipiency "ends" at interview date;
do s = 1 to dim(cont);
    if cont(s) = 1 then do;
        endYr(s) = intYr;
        endMo(s) = intMo;
    end;
end;
* Compute starts/stops/last for basic cases (dates are known);
do s = 1 to dim(starts);
    if startMo(s) > 0 & startYr(s) > 0 then
        starts(s) = startYr(s)*12+startMo(s);
    if endMo(s) > 0 & endYr(s) > 0 then
        ends(s) = endYr(s)*12+endMo(s);
end;
seamFlag=0;
do s = 1 to dim(starts);
	if starts(s) > 0 then do;
        if starts(s)=lint   then seamFlag=1;   
        if starts(s)=lint-1 then seamFlag=2;   
		if 0<starts(s)<lint-1 then seamFlag=3; 
	end;
end;
lint_real=lint-936-1;
do s=1 to dim(starts);
  if starts(s) > 0 & starts(s)<lint then starts(s)=lint;
end;

* Try to deduce starts/ends from # of mos;
do s = 1 to dim(starts);
    if nrOfRMos(s) > 0 then do; 
        if ends(s) > 0 then do;
            starts(s) = ends(s) - nrOfRMos(s)+1;
			check=5.1;
            if (starts(s) < lint) then check = 5;  
        end;
        else if starts(s) > 0 then do;
            ends(s) = starts(s) + nrOfRMos(s) - 1;
			check=100+s+0.1;  
			if ends(s)>intDt then check=100+s; 
        end;
		else if starts(s) = .  & ends(s) = .  then do; 
            if s = 1 then do; starts(s) = lint; check=201+0.1; end;   
			else do; starts(s) = ends(s-1) + 1; check=200+s+0.1; end; 
            ends(s) = starts(s) + nrOfRMos(s) - 1;
			if ends(s)>intDt then check=200+s; 
        end;
    end;
end;

last = 0;
if q44b01=2 or q43B = 2 then last = 1;
do s = 1 to dim(starts);
    if ((startMo(s) > 0 | startYr(s) > 0 | endMo(s) > 0 | endYr(s) > 0 |nrOfRMos (s) > 0) & last < s) then last = s;
end;

/* -----------------------------------------------------------------------------
 * Edges of recipiency spells and periods between: n1 r1 n2 r2 ...  (NNNRRR???NRRR)
 * Under ideal conditions they touch, but often there is an unclear zone between them (-2)
 * ----------------------------------------------------------------------------- */
array rs(*) rs1-rs4;        * Recipiency starts;
array re(*) re1-re4;        * Recipiency ends;
array ns(*) ns1-ns5;        * Nonrecipiency starts (# of spells + 1, potentialy bef and after);
array ne(*) ne1-ne5;        * Nonrecipiency ends;
* The fully known cases;
do s = 1 to last;
    if starts(s) > 0 then do;
        ne(s) = starts(s)-1;
        rs(s) = starts(s);
    end;
    if ends(s) > 0 then do;
        re(s)   = ends(s);
        ns(s+1) = ends(s)+1;
    end;
end;

* The unknown and partially known cases;
do s = 1 to last;
    if starts(s) = . & startYr(s) > 0 then do; 
		check=301; 
        xne = startYr(s)*12; 
        if ( s = 1 & lint < =xne ) |
           ( s > 1 & 0 < ends(s-1) < xne) |
           ( s > 1 & 0 < endYr(s-1) + 1 < startYr(s) )
            then do; ne(s) = xne; check=302; end;  
        if startYr(s) < endYr(s) then do;
            rs(s) = startYr(s)*12+12;  
			check=303; 
		end;
    end;
    
    if (ends(s) = . & endYr(s) > 0) then do; 
		check=401;  
        if 0 < startYr(s) < endYr(s) then do;
            re(s) = endYr(s)*12+1;        
            ns(s+1) = endYr(s)*12+13;     
			check=402; 
		end;
		if 0 < startYr(s) = endYr(s) then do; 
          ns(s+1)=endYr(s)*12+13; 		
		  if rs(s)>0 then do;
			re(s)=rs(s); 
		  end;
		  check=403;
		end;
    end;
end;
if (ne(1) > 0) then ns(1) = lint;               * There was a nonrecipiency at the begining;
if (ns(last+1) > 0) then ne(last+1) = intDt;    * There was a nonrecipiency at the end;

if last>=2 then do s = 2 to last;
	if 0 < rs(s) & rs(s) = re(s-1) then check = 7;
end;
 if rs1 > 0 & rs1<lint then rs1=lint;
 
/* -----------------------------------------------------------------------------
 * Fill the recipiency status (rec) and amounts (dol).
 * ----------------------------------------------------------------------------- */
* First, assume we know nothing;
do m = lint to intDt;
    rec(m) = -2;
    dol(m) = -2;
end;

* Fill in info we know (any of the periods below can be empty);
do s = 1 to last;
    * nonrecipiency period;
    if (0 < ns(s) <= ne(s)) then
        do m = max(ns(s),lint) to min(ne(s),intDt);
            rec(m) = -4;
            dol(m) = -4;
        end;
    * recipiency period;
    if (0 < rs(s) <= re(s)) then
        do m = max(rs(s),lint) to min(re(s),intDt);
            rec(m) = 1;
            dol(m) = howMuch(s);
			if howmuch(s) in (. -4) then do; dol(m)=-2; check5=s+0.1; end; 
        end;
    * we know only start - we are sure R received smth that month;
    else if (lint <= rs(s) <= intDt & re(s) = .) then do;  
        rec(rs(s)) = 1;
        dol(rs(s)) = howMuch(s);
        if howmuch(s) in (. -4) then do; dol(rs(s))=-2; check5=s+0.2; end; 
    end;
    * we know only end - we are sure R received smth that month (can be even intDt if continuous);
    else if (rs(s) = . & lint <= re(s) <= intDt) then do;
        rec(re(s)) = 1;
        dol(re(s)) = howMuch(s);
		if howmuch(s) in (. -4) then do; dol(re(s))=-2; check5=s+0.3; end;
    end;
end;

* The last period of nonrecipiency;
if (0 < ns(last+1) <= ne(last+1)) then
    do m = ns(last+1) to min(ne(last+1),intDt);
        rec(m) = -4;
        dol(m) = -4;
    end;
end;

/* ========================================================================== */
/* End if recip = .... (getting details for people who got something)
/* ========================================================================== */
do m=intDt+1 to hbound(rec);
    rec(m)=-4;
    dol(m)=-4;
end;

endDt=intDt-936;
noInt=0;

if q44c01m in (-1,-2) & q44c01y in (-1,-2) then q43b = 2;
if noInt=0 then
    do i=lbound(rec) to hbound(rec);
        if rec(i)=. then rec(i)=-4;
        if dol(i)=. then dol(i)=-4;
    end;
if noInt=1 then
    do i=lbound(rec) to hbound(rec);
        if rec(i)=. then rec(i)=-5;
        if dol(i)=. then dol(i)=-5;
    end;
    
/* ==========================================================================
 * Calculating yearly variables.
 * ========================================================================== */
array recYM(&cYrs,12) rec1-rec&cMos;
array dolYM(&cYrs,12) dol1-dol&cMos;
array yRec(*)   yRec78-yRec&cEndYr;
array yDol(*)   yDol78-yDol&cEndYr;
array yRecMo(*) yRecMo78-yRecMo&cEndYr;

* Calculate yRec (any recipiency in a yr?);
do y=1 to dim(yRec);
    do m=1 to 12;
        if recYM(y,m) ~= . then    yRec(y)=0;
        if recYM(y,m) > 0 then do; yRec(y)=1; leave; end;
    end;
	if yRec(y)=0 then do m=1 to 12;
	    if recYM(y,m) in (-1 -2 -3) then do;  yRec(y)=recYM(y,m); leave; end;
	end;
	if yRec(y)=0 then yRec(y)=-4; 	
end;

* Calculate yDol ($ in yr), yRecMo (# of months of recipiency in each y);
do y=1 to dim(yRec);
    if yRec(y) > 0 then do;
		rrefuse = 0; drefuse = 0;
		rdk = 0;     ddk = 0;
        do m=1 to 12;
			if dolYM(y,m) = -1 then drefuse = 1;
			else if dolYM(y,m) in (-2 -3) then ddk = 1;
            if recYM(y,m) = -1 then rrefuse = 1;
			else if recYM(y,m) = -2 then rdk = 1;
		end;
		if rrefuse = 1 then yRecMo(y) = -1;
		else if rdk = 1 then yRecMo(y) = -2;
		else do;
	        yRecMo(y) = 0;
	        do m=1 to 12;
	            if recYM(y,m) = 1 then yRecMo(y)=yRecMo(y)+1;
	        end;
	    end;
 	    if rrefuse = 1 or drefuse = 1 then yDol(y) = -3;
		else if rdk = 1 or ddk = 1 then yDol(y) = -3;   
		else do;
	        yDol(y) = 0;
	        do m=1 to 12;
	            if 0 <= dolYM(y,m) then yDol(y)=yDol(y)+dolYM(y,m); 
	        end;
	    end;
	end;
	else do;
		yDol(y)=yRec(y);
		yRecMo(y)=yRec(y);
	end;
end;

/* ==========================================================================
 *  Flag yearly amount that are not within possible bounds.
  * ========================================================================== */
array yFlg(*)   yFlg78-yFlg&cEndYr; 
array cMinDol (&cYrs) _temporary_
    (36 40 43 48 50 49 51 54 56 58 61 65 68 70 70 72 74 76 76 88 88 98 98 23*109);
array cMaxDol (&cYrs) _temporary_
    (158 173 187 208 217 215 222 236 245 254 266 284 298 305 305 315 322 333 333 383 383 433 433 7*483 3*525 550 575 11*653);
    
do y=1 to dim(yRec);
    yFlg(y) = 0; 
    if yRec(y) = -5 then yFlg(y)=-5;
    if yDol(y) < 0 then continue; 
	avgDol = yDol(y) / yRecMo(y);
    if avgDol < cMinDol(y)*4.3 then
		yFlg(y)=1;
	else if cMaxDol(y)*4.3 < avgDol then do;
        yFlg(y)=2;
		if cMinDol(y)*4.3 <= avgDol / yRecMo(y) <= cMaxDol(y)*4.3 then yFlg(y)=3;
    end;
end;

/* ==========================================================================
 *     DOL yearly amount
 * ========================================================================== */
do y=1 to dim(yRec);
    if yDol(y) = .  then yDol(y)=-4;
    if yRec(y) = -4 then yRec(y)=0;
end;
yflag=0;
array ydolo(*)   uyDolo78-uyDolo&cEndYr;
do i=1 to dim(ydol);
 if lintyr=i+77 & 0<lintmo<13 then do;
  if ydol(i)=-4 then do; ydol(i)=ydolo(i); yflag=1; end;
  else if ydol(i)>=0 & ydolo(i) not in (-4 0) then do;
    if ydolo(i)>0 then do; ydol(i)=ydol(i)+ydolo(i); yflag=2; end;
	else if ydolo(i) in (-1 -2 -3) then do; ydol(i)=ydolo(i); yflag=3; end;
  end;
 end;
end;

Spouse/Partner Unemployment Benefits Receipt

%let cEndYr = 123;		*(2023-1900);
%let cEndMo = 1488;     *(123+1)*12;
%let cYrs = 46;			*(2023-1978)+1;
%let cMos = 552;		*46*12;
%let cFillFlag = 2022;
data Q13;
 set prg2022.in_inc_2022;
  
VARIABLE NAMES IN PUBLIC RELEASE		VARIABLE NAMES IN PROGRAM
  Q13RECIP-2-SP_2022'n  					RECIP2
  Q13S-43B_2022         					q43B
  Q13S-43C~M_2022       					q43CM
  Q13S-43C~Y_2022       					q43CY
  Q13S-44A1_CHK1.01_2022					q44A1CHK101
  Q13S-44A1_CHK1.02_2022					q44A1CHK102
  Q13S-44A1_CHK1.03_2022					q44A1CHK103
  Q13S-44A1_CHK1.04_2022					q44A1CHK104
  Q13S-44A3.01_2022     					q44A301
  Q13S-44A3.02_2022     					q44A302
  Q13S-44A3.03_2022     					q44A303
  Q13S-44A3.04_2022     					q44A304
  Q13S-44A5.01_2022     					q44A501
  Q13S-44A6.01_2022    						q44A601
  Q13S-44B.01_2022      					q44B01
  Q13S-44B.02_2022      					q44B02
  Q13S-44B.03_2022      					q44B03
  Q13S-44B.04_2022      					q44B04
  Q13S-44C.02~M_2022    					q44C02M
  Q13S-44C.02~Y_2022    					q44C02Y
  Q13S-44C.03~M_2022   						q44C03M
  Q13S-44C.03~Y_2022    					q44C03Y
  Q13S-44C.04~M_2022    					q44C04M
  Q13S-44C.04~Y_2022    					q44C04Y
  Q13S-44D.01_2022      					q44D01
  Q13S-44D.02_2022      					q44D02
  Q13S-44D.03_2022      					q44D03
  Q13S-44D.04_2022     						q44D04
  Q13S-45A.01_2022      					q45A01
  Q13S-45A.02_2022      					q45A02
  Q13S-45A.03_2022      					q45A03
  Q13S-45A.04_2022      					q45A04
  Q13S-45B.01~M_2022    					q45B01M
  Q13S-45B.01~Y_2022    					q45B01Y
  Q13S-45B.02~M_2022   						q45B02M
  Q13S-45B.02~Y_2022    					q45B02Y
  Q13S-45B.03~M_2022    					q45B03M
  Q13S-45B.03~Y_2022    					q45B03Y
  Q13S-45B.04~M_2022    					q45B04M
  Q13S-45B.04~Y_2022    					q45B04Y
  Q13S-47E.01_2022     						q47E01
  Q13S-47E.02_2022      					q47E02
  Q13S-47E.03_2022      					q47E03
  Q13S-47E.04_2022      					q47E04
  Q13S-62.01_2022       					q6201
  Q13S-62.02_2022       					q6202
  Q13S-62.03_2022       					q6203
  Q13S-62.04_2022      						q6204
  Q13S-62A.01_2022     						q62A01
  Q13S-62A.02_2022     						q62A02
  Q13S-62A.03_2022     						q62A03
  Q13S-62A.04_2022     						q62A04
  Q13S-63.01_2022      						q6301
  Q13S-63A.01_2022     						q63A01
  Q13S-63AB.01_2022    						q63AB01
  Q13S-63B.01_2022     						q63B01
  Q13S-64.01_2022      						q6401
  Q13S-64.02_2022      						q6402
  Q13S-64.03_2022      						q6403
  Q13S-64.04_2022      						q6404
  CURDATE~D_2022       						intDay      
  CURDATE~M_2022       						intMo 
  CURDATE~Y_2022       						intYr 
  Q13-10_2022  	       		  			    q10				
;

data recip1;
 merge R30.lintdate2022 Q13 prg2022.oldin2022 (keep=norcid pydolo:);
 by norcid;
 if (intMo > 0);
recip = RECIP2;
data recip;
 set recip1;
 
* convert Y2K changes to simple years;
array yrs (*) intYr q43cy
              q44c01y q44c02y q44c03y q44c04y
              q45b01y q45b02y q45b03y q45b04y;
do i=1 to dim(yrs);
    if yrs(i) > 0 then yrs(i)=yrs(i)-1900;
end;
* date of this interview;
intDt=intYr*12+intMo;

/* ==========================================================================
  Merge info from several sources + convert weeks to months.
  ========================================================================== */
dummy = -4;

if q44c01y>-4 and q43cy>-4 and q44c01y<q43cy then do;
	q44c01y=q43cy;
	q44c01m=q43cm;
end;
else if q44c01y>-4 and q43cy>-4 and q44c01y>q43cy then do;
	q44c01y=q44c01y;
	q44c01m=q44c01m;
end;
else if q44c01y>-4 and q43cy>-4 and q44c01y=q43cy then do;
	q44c01m = max(q44c01m, q43cm);
	q44c01y = max(q44c01y, q43cy);
end;
else if q44c01y<=-4 and q43cy>-4 then do;
	q44c01m = max(q44c01m, q43cm);
	q44c01y = max(q44c01y, q43cy);
end;

/* nrOfRMo - # of months x was received (used when start/stop dates are not known).*/
array nrOfRMos  (*) q63a01 dummy dummy dummy;
nrOfRMos(1) = max(nrOfRMos(1), q63aa01, q63ab01);
do s = 1 to dim(nrOfRMos);
    if nrOfRMos(s) > 0 then
        nrOfRMos(s) = max(round(nrOfRMos(s)/4.3),1);
end;

* How much did R get during each month (by spells);
array howMuch (*) q47e01-q47e04;  
array q63b    (*) q63b01-q63b04;  
array q63    (*) q6301-q6304;     
do s=1 to dim(howMuch);
    if howMuch(s) = -4 then howMuch(s) = q63b(s);  
    if howMuch(s) > 0 then howMuch(s)=round(howMuch(s)*4.3,1); 
end;

/* ==========================================================================
   Recipiency status (rec) and amounts (dol) by months
   All months from 1978jan till dec of current year, indexed from 1900jan.
   78*12+1 = 937
   ========================================================================== */
array rec (937:&cEndMo);      * Did they receive x in that month?;
array dol (937:&cEndMo);      * How much?;

/* ========================================================================== 
 * No/unknown recipiency
 * ========================================================================== */
if recip=0 or q10=0 then do;
    do m=lint to intDt;
        rec(m)=-4;
        dol(m)=-4;
    end;
end;
else if recip in (-1,-2) then do;
    do m=lint to intDt;
        rec(m) = recip;
        dol(m) = recip;
    end;
end;
else if recip=-3 then
    do m=lint to intDt;
        rec(m) = -3;
        dol(m) = -3;
    end;
    
/* ========================================================================== 
 * There was some recipiency, get details
 * ========================================================================== */
else do;
check = 0;	
array startMo (*) q44c01m q44c02m q44c03m q44c04m;
array startYr (*) q44c01y q44c02y q44c03y q44c04y;
array endMo (*) q45b01m q45b02m q45b03m q45b04m;
array endYr (*) q45b01y q45b02y q45b03y q45b04y;
array cont    (*) q44d01-q44d04; 
array q44b    (*) q44b01-q44b04; 
array q64     (*) q6401-q6404;  

/* -----------------------------------------------------------------------------
 * Calculate starts and ends of spells if possible
 * -----------------------------------------------------------------------------*/
array starts (*) starts1-starts4;   
array ends   (*) ends1-ends4;    
 
* Continuous recipiency "ends" at interview date;
do s = 1 to dim(cont);
    if cont(s) = 1 then do;
        endYr(s) = intYr;
        endMo(s) = intMo;
    end;
end;

* Compute starts/stops/last for basic cases (dates are known);
do s = 1 to dim(starts);
    if startMo(s) > 0 & startYr(s) > 0 then
        starts(s) = startYr(s)*12+startMo(s);
    if endMo(s) > 0 & endYr(s) > 0 then
        ends(s) = endYr(s)*12+endMo(s);
end;

seamFlag=0;
do s = 1 to dim(starts);
	if starts(s) > 0 then do;
        if starts(s)=lint   then seamFlag=1; 
        if starts(s)=lint-1 then seamFlag=2; 
		if 0<starts(s)<lint-1 then seamFlag=3;
	end;
end;

lint_real=lint-936-1;
do s=1 to dim(starts);
 if starts(s) > 0 & starts(s)<lint then starts(s)=lint;
end;

* Try to deduce starts/ends from # of mos;
do s = 1 to dim(starts);
    if nrOfRMos(s) > 0 then do; 
       if ends(s) > 0 then do;
            starts(s) = ends(s) - nrOfRMos(s)+1;
			check=5.1;  
            if (starts(s) < lint) then check = 5;   
        end;
        else if starts(s) > 0 then do;
            ends(s) = starts(s) + nrOfRMos(s) - 1;
			check=100+s+0.1; 
			if ends(s)>intDt then check=100+s; 
        end;
		else if starts(s) = .  & ends(s) = .  then do; 
            if s = 1 then do; starts(s) = lint; check=201+0.1; end;   
			else do; starts(s) = ends(s-1) + 1; check=200+s+0.1; end; 
            ends(s) = starts(s) + nrOfRMos(s) - 1;
			if ends(s)>intDt then check=200+s;   
        end;
    end;
end;

last = 0;
if q44b01=2 or q43B = 2 then last = 1; 
do s = 1 to dim(starts);
    if ((startMo(s) > 0 | startYr(s) > 0 | endMo(s) > 0 | endYr(s) > 0 |nrOfRMos (s) > 0) & last < s) then last = s;
end;

/* -----------------------------------------------------------------------------
 * Edges of recipiency spells and periods between: n1 r1 n2 r2 ...  (NNNRRR???NRRR)
 * Under ideal conditions they touch, but often there is an unclear zone between them (-2)
 * ----------------------------------------------------------------------------- */
array rs(*) rs1-rs4;        * Recipiency starts;
array re(*) re1-re4;        * Recipiency ends;
array ns(*) ns1-ns5;        * Nonrecipiency starts (# of spells + 1, potentialy bef and after);
array ne(*) ne1-ne5;        * Nonrecipiency ends;

* The fully known cases;
do s = 1 to last;
    if starts(s) > 0 then do;
        ne(s) = starts(s)-1;
        rs(s) = starts(s);
    end;
    if ends(s) > 0 then do;
        re(s)   = ends(s);
        ns(s+1) = ends(s)+1;
    end;
end;

* The unknown and partially known cases;
do s = 1 to last;
    if starts(s) = . & startYr(s) > 0 then do; 
		check=301;  
        xne = startYr(s)*12;    
        if ( s = 1 & lint < =xne ) |
           ( s > 1 & 0 < ends(s-1) < xne) |
           ( s > 1 & 0 < endYr(s-1) + 1 < startYr(s) )
            then do; ne(s) = xne; check=302; end;  
        if startYr(s) < endYr(s) then do;
            rs(s) = startYr(s)*12+12;   
			check=303;   
		end;
    end;
    
    if (ends(s) = . & endYr(s) > 0) then do; 
		check=401; 
        if 0 < startYr(s) < endYr(s) then do;
            re(s) = endYr(s)*12+1;      
            ns(s+1) = endYr(s)*12+13;   
			check=402; 
		end;
		if 0 < startYr(s) = endYr(s) then do; 
          ns(s+1)=endYr(s)*12+13;		
		  if rs(s)>0 then do;
			re(s)=rs(s);
		  end;
		  check=403;
		end;
    end;
end;

if (ne(1) > 0) then ns(1) = lint;               * There was a nonrecipiency at the begining;
if (ns(last+1) > 0) then ne(last+1) = intDt;    * There was a nonrecipiency at the end;

if last>=2 then do s = 2 to last; 
	if 0 < rs(s) & rs(s) = re(s-1) then check = 7;
end;
  if rs1 > 0 & rs1<lint then rs1=lint;
  
/* -----------------------------------------------------------------------------
 * Fill the recipiency status (rec) and amounts (dol).
 * ----------------------------------------------------------------------------- */
* First, assume we know nothing;
do m = lint to intDt;
    rec(m) = -2;
    dol(m) = -2;
end;

* Fill in info we know (any of the periods below can be empty);
do s = 1 to last;
    * nonrecipiency period;
    if (0 < ns(s) <= ne(s)) then
        do m = max(ns(s),lint) to min(ne(s),intDt);
            rec(m) = -4;
            dol(m) = -4;
        end;
    * recipiency period;
    if (0 < rs(s) <= re(s)) then
        do m = max(rs(s),lint) to min(re(s),intDt);
            rec(m) = 1;
            dol(m) = howMuch(s);
			if howmuch(s) in (. -4) then do; dol(m)=-2; check5=s+0.1; end; 
        end;
    * we know only start - we are sure R received smth that month;
    else if (lint <= rs(s) <= intDt & re(s) = .) then do;       
        rec(rs(s)) = 1;
        dol(rs(s)) = howMuch(s);
        if howmuch(s) in (. -4) then do; dol(rs(s))=-2; check5=s+0.2; end;
    end;
    * we know only end - we are sure R received smth that month (can be even intDt if continuous);
    else if (rs(s) = . & lint <= re(s) <= intDt) then do;
        rec(re(s)) = 1;
        dol(re(s)) = howMuch(s);
		if howmuch(s) in (. -4) then do; dol(re(s))=-2; check5=s+0.3; end;
    end;
end;

* The last period of nonrecipiency;
if (0 < ns(last+1) <= ne(last+1)) then
    do m = ns(last+1) to min(ne(last+1),intDt);
        rec(m) = -4;
        dol(m) = -4;
    end;
end;

/* ========================================================================== */
/* End if recip = .... (getting details for people who got something)
/* ========================================================================== */
* Fill in space after the interview;
do m=intDt+1 to hbound(rec);
    rec(m)=-4;
    dol(m)=-4;
end;
endDt=intDt-936;
noInt=0;
if q44c01m in (-1,-2) & q44c01y in (-1,-2) then q43b = 2;
if noInt=0 then
    do i=lbound(rec) to hbound(rec);
        if rec(i)=. then rec(i)=-4;
        if dol(i)=. then dol(i)=-4;
    end;
if noInt=1 then
    do i=lbound(rec) to hbound(rec);
        if rec(i)=. then rec(i)=-5;
        if dol(i)=. then dol(i)=-5;
    end;
    
/* ==========================================================================
 * Calculating yearly variables.
 * ========================================================================== */
array recYM(&cYrs,12) rec1-rec&cMos;
array dolYM(&cYrs,12) dol1-dol&cMos;
array yRec(*)   yRec78-yRec&cEndYr;
array yDol(*)   yDol78-yDol&cEndYr;
array yRecMo(*) yRecMo78-yRecMo&cEndYr;

* Calculate yRec (any recipiency in a yr?);
do y=1 to dim(yRec);
    do m=1 to 12;
        if recYM(y,m) ~= . then    yRec(y)=0;
        if recYM(y,m) > 0 then do; yRec(y)=1; leave; end;
    end;
	if yRec(y)=0 then do m=1 to 12;
	    if recYM(y,m) in (-1 -2 -3) then do;  yRec(y)=recYM(y,m); leave; end;
	end;
	if yRec(y)=0 then yRec(y)=-4;	
end;

* Calculate yDol ($ in yr), yRecMo (# of months of recipiency in each y);
do y=1 to dim(yRec);
    if yRec(y) > 0 then do;	
		rrefuse = 0; drefuse = 0;
		rdk = 0;     ddk = 0;
        do m=1 to 12;
			if dolYM(y,m) = -1 then drefuse = 1;
			else if dolYM(y,m) = -2 then ddk = 1;
            if recYM(y,m) = -1 then rrefuse = 1;
			else if recYM(y,m) = -2 then rdk = 1;
		end;
		if rrefuse = 1 then yRecMo(y) = -1;
		else if rdk = 1 then yRecMo(y) = -2;
		else do;
	        yRecMo(y) = 0;
	        do m=1 to 12;
	            if recYM(y,m) = 1 then yRecMo(y)=yRecMo(y)+1;
	        end;
	    end;				
	    if rrefuse = 1 or drefuse = 1 then yDol(y) = -1;
		else if rdk = 1 or ddk = 1 then yDol(y) = -2;
		else do;
	        yDol(y) = 0;
	        do m=1 to 12;
	            if 0 <= dolYM(y,m) then yDol(y)=yDol(y)+dolYM(y,m); 
	        end;
	    end;
	end;
	else do;
		yDol(y)=yRec(y);
		yRecMo(y)=yRec(y); 
	end;
end;

/* ==========================================================================
 *  Flag yearly amount that are not within possible bounds.
 * ========================================================================== */
array yFlg(*)   yFlg78-yFlg&cEndYr;   
array cMinDol (&cYrs) _temporary_
    (36 40 43 48 50 49 51 54 56 58 61 65 68 70 70 72 74 29*76);
array cMaxDol (&cYrs) _temporary_
    (158 173 187 208 217 215 222 236 245 254 266 284 298 305 305 315 322 333 333 383 383 425 425 7*471 3*525 550 575 11*653);
    
do y=1 to dim(yRec);
    yFlg(y) = 0;    
    if yRec(y) = -5 then yFlg(y)=-5;
    if yDol(y) < 0 then continue; 
	avgDol = yDol(y) / yRecMo(y);
    if avgDol < cMinDol(y)*4.3 then
		yFlg(y)=1;
	else if cMaxDol(y)*4.3 < avgDol then do;
        yFlg(y)=2;
		if cMinDol(y)*4.3 <= avgDol / yRecMo(y) <= cMaxDol(y)*4.3 then yFlg(y)=3;
    end;
end;

/* ==========================================================================
 *   DOL yearly amount
 * ========================================================================== */
do y=1 to dim(yRec);
    if yDol(y) = .  then yDol(y)=-4;
    if yRec(y) = -4 then yRec(y)=0;
end;

yflag=0;
array ydolo(*)   pyDolo78-pyDolo&cEndYr;
do i=1 to dim(ydol);
 if lintyr=i+77 & 0<lintmo<13 then do;
  if ydol(i)=-4 then do; ydol(i)=ydolo(i); yflag=1; end;
  else if ydol(i)>=0 & ydolo(i) not in (-4 0) then do;
    if ydolo(i)>0 then do; ydol(i)=ydol(i)+ydolo(i); yflag=2; end;
	else if ydolo(i) in (-1 -2 -3) then do; ydol(i)=ydolo(i); yflag=3; end;
  end;
 end;
end;

AFDC/TANF Receipt

%let cEndYr = 123;		*(2023-1900);
%let cEndMo = 1488;     *(123+1)*12;
%let cYrs = 46;			*(2023-1978)+1;
%let cMos = 552;		*46*12;
%let cFillFlag = 2022;

data Q13;
 set prg2022.in_inc_2022;
 
VARIABLE NAMES IN PUBLIC RELEASE		VARIABLE NAMES IN PROGRAM
  Q13RECIP-4~000001_2022					 	recip41                      
  Q13RECIP-4~000002_2022						recip42                      
  Q13RECIP-4~000003_2022						recip43                      
  Q13RECIP-4~000004_2022						recip44                      
  Q13A-BB_2022          						QBB          
  Q13A-43B_2022         						Q43B         
  Q13A-43C~M_2022       						Q43CM       
  Q13A-43C~Y_2022       						Q43CY       
  Q13A-44A1_CHK1.01_2022					    Q44A1_CHK11
  Q13A-44A3.01_2022     						Q44A301     
  Q13A-44A5.01_2022     						Q44A501     
  Q13A-44A6.01_2022     						Q44A601     
  Q13A-44B.01_2022      						Q44B01      
  Q13A-44D.01_2022      						Q44D01     
  Q13A-45A.01_2022      						Q45A01      
  Q13A-45B.01~M_2022    						Q45B01M    
  Q13A-45B.01~Y_2022    						Q45B01Y    
  Q13A-47E.01_2022      						Q47E01       
  Q13A-62.01_2022       						Q6201      
  Q13A-62A.01_2022      						Q62A01    
  Q13A-63A.01_2022      						Q63A01    
  Q13A-63AA.01_2022     						Q63AA01    
  Q13A-63B.01_2022      						Q63B01     
  Q13A-63C.01_2022      						Q63C01      
  Q13A-63D.01_2022      						Q63D01 
  Q13A-64.01_2022       						Q6401       
  Q13A-64A.01_2022      						Q64A01      
  CURDATE~D_2022        						intDay      
  CURDATE~M_2022        						intMo       
  CURDATE~Y_2022        						intYr;
        
data recip1;
 merge R30.lintdate2022 Q13 prg2022.oldin2022 (keep=norcid aydolo:);
 by norcid;
 if (intMo > 0);
 
recip = RECIP41;
*************************************************************************************************************;
data recip;
 set recip1;
 
* convert Y2K changes to simple years;
array yrs (*) intYr q43cy q45b01y; 
do i=1 to dim(yrs);
    if yrs(i) > 0 then yrs(i)=yrs(i)-1900;
end;

* date of this interview;
intDt=intYr*12+intMo;

/* ==========================================================================
  Merge info from several sources + convert weeks to months.
  ========================================================================== */
dummy = -4;    
if q44c01y>-4 and q43cy>-4 and q44c01y<q43cy then do;
	q44c01y=q43cy;
	q44c01m=q43cm;
end;
else if q44c01y>-4 and q43cy>-4 and q44c01y>q43cy then do;
	q44c01y=q44c01y;
	q44c01m=q44c01m;
end;
else if q44c01y>-4 and q43cy>-4 and q44c01y=q43cy then do;
	q44c01m = max(q44c01m, q43cm);
	q44c01y = max(q44c01y, q43cy);
end;
else if q44c01y<=-4 and q43cy>-4 then do;
	q44c01m = max(q44c01m, q43cm);
	q44c01y = max(q44c01y, q43cy);
end;

/* nrOfRMos - # of months x was received (used when start/stop dates are not known).*/
array nrOfRMos (*) q63aa01; 
nrOfRMos(1) = max(nrOfRMos(1), q63a01, q63ab01);

* How much did R receive in each spell;
array howMuch (*)  q47e01; 
array q63b    (*)  q63b01; 
do s=1 to dim(howMuch);
    if howMuch(s) = -4 then howMuch(s) = q63b(s); 
end;

/* ==========================================================================
   Recipiency status (rec) and amounts (dol) by months
   All months from 1978jan till dec of current year, indexed from 1900jan.
   78*12+1 = 937
   ========================================================================== */
array rec (937:&cEndMo);      * Did they receive x in that month?;
array dol (937:&cEndMo);      * How much?;

/* ========================================================================== 
 * No/unknown recipiency
 * ========================================================================== */
if recip=0 then do;
    do m=lint to intDt;
        rec(m)=-4;
        dol(m)=-4;
    end;
end;
else if recip in (-1,-2) then do;
    do m=lint to intDt;
        rec(m) = recip;
        dol(m) = recip;
    end;
end;
else if recip in (-3) then do;
    do m=lint to intDt;
        rec(m) = recip;
        dol(m) = recip;
    end;
end;

/* ========================================================================== */
/* There was some recipiency, get details */
/* ========================================================================== */
else do;
check = 0;	
array startMo (*) q44c01m;
array startYr (*) q44c01y 
array endMo (*) q45b01m; 
array endYr (*) q45b01y; 
array cont (*) q44d01;
array q44b (*) q44b01;
array q64  (*) q6401;
array q63c (*) q63c01; 

/* -----------------------------------------------------------------------------
 * Calculate starts and ends of spells if possible
 * -----------------------------------------------------------------------------*/
array starts (*) starts1;
array ends   (*) ends1; 

* Continuous recipiency "ends" at interview date;
do s = 1 to dim(cont);
	if cont(s) = 1 or q63c(s)=12 then do; 
		endYr(s) = intYr;
		endMo(s) = intMo;
	end;
end;

* Compute starts/stops/last for basic cases (dates are known);
do s = 1 to dim(starts);
    if startMo(s) > 0 & startYr(s) > 0 then
        starts(s) = startYr(s)*12+startMo(s);
    if endMo(s) > 0 & endYr(s) > 0 then
        ends(s) = endYr(s)*12+endMo(s);
end;

seamFlag=0;
do s = 1 to dim(starts);
	if starts(s) > 0 then do;
        if starts(s)=lint   then seamFlag=1; 
        if starts(s)=lint-1 then seamFlag=2; 
		if 0<starts(s)<lint-1 then seamFlag=3;
	end;
end;

lint_real=lint-936-1;
do s=1 to dim(starts);
 if starts(s) > 0 & starts(s)<lint then starts(s)=lint;
end;

* Try to deduce starts/ends from # of mos;
do s = 1 to dim(starts);
    if nrOfRMos(s) > 0 then do;  
	    if ends(s) > 0 & starts(s) = .  then do;         
            starts(s) = ends(s) - nrOfRMos(s)+1;
			check=5.1;  
            if (starts(s) < lint) then check = 5;  
        end;
		else if starts(s) > 0 & ends(s) = .  then do; 
            ends(s) = starts(s) + nrOfRMos(s) - 1;
			check=100+s+0.1; 
			if ends(s)>intDt then check=100+s; 
        end;
		else if starts(s) = .  & ends(s) = .  then do; 
            if s = 1 then do; starts(s) = lint; check=201+0.1; end;  
			else do; starts(s) = ends(s-1) + 1; check=200+s+0.1; end; 
            ends(s) = starts(s) + nrOfRMos(s) - 1;
			if ends(s)>intDt then check=200+s;  
        end;
    end;
end;

/* Determine what was the last spell (0 - there was no spell) */
last = 0;
if q44b01=2 or q43B = 2 then last = 1; 
do s = 1 to dim(starts);
	if ((startMo(s) > 0 | startYr(s) > 0 | endMo(s) > 0 | endYr(s) > 0 |nrOfRMos (s) > 0) & last < s) then last = s;
end;

/* -----------------------------------------------------------------------------
 * Edges of recipiency spells and periods between: n1 r1 n2 r2 ...  (NNNRRR???NRRR)
 * Under ideal conditions they touch, but often there is an unclear zone between them (-2)
 * ----------------------------------------------------------------------------- */
array rs(*) rs1;      		* Recipiency starts;
array re(*) re1;      		* Recipiency ends;
array ns(*) ns1-ns2;		* Nonrecipiency starts (# of spells + 1, potentialy bef and after);
array ne(*) ne1-ne2;		* Nonrecipiency ends;

* The fully known cases;
do s = 1 to last;
    if starts(s) > 0 then do;
        ne(s) = starts(s)-1;
        rs(s) = starts(s);
    end;
    if ends(s) > 0 then do;
        re(s)   = ends(s);
        ns(s+1) = ends(s)+1;
    end;
end;

* The unknown and partially known cases;
do s = 1 to last;
    if starts(s) = . & startYr(s) > 0 then do; 
		check=301; 
        xne = startYr(s)*12;  
        if ( s = 1 & lint <= xne ) |
           ( s > 1 & 0 < ends(s-1) < xne) |
           ( s > 1 & 0 < endYr(s-1) + 1 < startYr(s) )
            then do; ne(s) = xne; check=302; end; 
        if startYr(s) < endYr(s) then do;
            rs(s) = startYr(s)*12+12;     
			check=303;     
		end;
    end;
    
    if (ends(s) = . & endYr(s) > 0) then do; 
		check=401;
        if 0 < startYr(s) < endYr(s) then do;
            re(s) = endYr(s)*12+1;      
            ns(s+1) = endYr(s)*12+13;   
			check=402; 
		end;
		if 0 < startYr(s) = endYr(s) then do; 
		  ns(s+1)=endYr(s)*12+13; 
		  if rs(s)>0 then do;
			re(s)=rs(s);
		  end;
		  check=403;
		end;
    end;
end;

if (ne(1) > 0) then ns(1) = lint;				* There was a nonrecipiency at the begining;
if (ns(last+1) > 0) then ne(last+1) = intDt;	* There was a nonrecipiency at the end;

if last>=2 then do s = 2 to last;
	if 0 < rs(s) & rs(s) = re(s-1) then check = 7;
end;
 if rs1 > 0 & rs1<lint then rs1=lint;
 
/* -----------------------------------------------------------------------------
 * Fill the recipiency status (rec) and amounts (dol).
 * ----------------------------------------------------------------------------- */
* First, assume we know nothing;
do m = lint to intDt;
	rec(m) = -2;
	dol(m) = -2;
end;

* Fill in info we know (any of the periods below can be empty);
do s = 1 to last;
	* nonrecipiency period;
	if (0 < ns(s) <= ne(s)) then
		do m = max(ns(s),lint) to min(ne(s),intDt);
			rec(m) = -4;
			dol(m) = -4;
		end;
	* recipiency period;
	if (0 < rs(s) <= re(s)) then
		do m = max(rs(s),lint) to min(re(s),intDt); 
			rec(m) = 1;
			dol(m) = howMuch(s);
			if howmuch(s) in (. -4) then do; dol(m)=-2; check5=s+0.1; end; 
		end;
	    * we know only start - we are sure R received smth that month;
        else if (lint <= rs(s) <= intDt & re(s) = .) then do;     
            rec(rs(s)) = 1;
            dol(rs(s)) = howMuch(s);
		if howmuch(s) in (. -4) then do;  dol(rs(s))=-2; check5=s+0.2; end; 
        end;
        * we know only end - we are sure R received smth that month (can be even intDt if continuous);
        else if (rs(s) = . & lint <= re(s) <= intDt) then do;
            rec(re(s)) = 1;
            dol(re(s)) = howMuch(s);
		if howmuch(s) in (. -4) then do; dol(re(s))=-2; check5=s+0.3; end; 
        end;
end;

* The last period of nonrecipiency;
if (0 < ns(last+1) <= ne(last+1)) then
	do m = ns(last+1) to min(ne(last+1),intDt);
		rec(m) = -4;
		dol(m) = -4;
	end;
end;

/* ========================================================================== */
/* End if recip = .... (getting details for people who got something)
/* ========================================================================== */
* Fill in space at the end of year after the interview;
do m=intDt+1 to hbound(rec);
    rec(m)=-4;
    dol(m)=-4;
end;

endDt=intDt-936;
noInt=0;
if q44c01m in (-1,-2) & q44c01y in (-1,-2) then q43b = 2;

/* ========================================================================== */
array flag(937:&cEndMo) flag1-flag&cMos;
array recYM(&cYrs,12) rec1-rec&cMos;
array dolYM(&cYrs,12) dol1-dol&cMos;

* Set fill flag for ints/non-ints;
if noInt in (0,1) then
	do i=lint to intDt;
		flag(i)=&cFillFlag;
	end;
	
/* ==========================================================================
 * fill in missings: -4 if int, -5 if non-int
 * ========================================================================== */
if noInt=0 then
	do i=lbound(rec) to hbound(rec);
 		if rec(i)=. then rec(i)=-4;
 		if dol(i)=. then dol(i)=-4;
 		if flag(i)=. then flag(i)=-4;
 	end;
if noInt=1 then
	do i=lbound(rec) to hbound(rec);
 		if rec(i)=. then rec(i)=-5;
 		if dol(i)=. then dol(i)=-5;
 		if flag(i)=. then flag(i)=-4;
 	end;
 	
/* ==========================================================================
 * Calculating yearly variables.
 * ========================================================================== */
array yRec(*)   yRec78-yRec&cEndYr;
array yDol(*)   yDol78-yDol&cEndYr;
array yRecMo(*) yRecMo78-yRecMo&cEndYr;

* Calculate yRec (any recipiency in a yr?);
do y=1 to dim(yRec);
    do m=1 to 12;
        if recYM(y,m) ~= . then    yRec(y)=0;
        if recYM(y,m) > 0 then do; yRec(y)=1; leave; end;
    end;
	if yRec(y)=0 then do m=1 to 12;
	    if recYM(y,m) in (-1 -2 -3) then do;  yRec(y)=recYM(y,m); leave; end;
	end;
	if yRec(y)=0 then yRec(y)=-4; 
end;

* Calculate yDol ($ in yr), yRecMo (# of months of recipiency in each y);
do y=1 to dim(yRec);
    if yRec(y) > 0 then do;
		rrefuse = 0; drefuse = 0;
		rdk = 0;     ddk = 0;
        do m=1 to 12;
			if dolYM(y,m) = -1 then drefuse = 1;
			else if dolYM(y,m) = -2 then ddk = 1;
            if recYM(y,m) = -1 then rrefuse = 1;
			else if recYM(y,m) = -2 then rdk = 1;
		end;
		if rrefuse = 1 then yRecMo(y) = -1;
		else if rdk = 1 then yRecMo(y) = -2;
		else do; 
	        yRecMo(y) = 0;
	        do m=1 to 12;
	            if recYM(y,m) = 1 then yRecMo(y)=yRecMo(y)+1;
	        end;
	    end;		
	    if rrefuse = 1 or drefuse = 1 then yDol(y) = -1;
		else if rdk = 1 or ddk = 1 then yDol(y) = -2;
		else do;
	        yDol(y) = 0;
	        do m=1 to 12;
	            if 0 <= dolYM(y,m) then yDol(y)=yDol(y)+dolYM(y,m); 
	        end;
	    end;
	end;
	else do;
		yDol(y)=yRec(y);
		yRecMo(y)=yRec(y); 
	end;
end;

/* ==========================================================================
 *  Flag yearly amount that are not within possible bounds.
 * ========================================================================== */
 
array yFlg(*)   yFlg78-yFlg&cEndYr;   
array cMinDol (&cYrs) _temporary_ (&cYrs*46);
array cMaxDol (&cYrs) _temporary_
	(537 579 586 622 643 670 700 726 742 771 794 815 809 786 875 844 827 808 808
	949 949 1025 1025 1134 1134 21*1134);
	
do y=1 to dim(yRec);
    yFlg(y) = 0;  
    if yRec(y) = -5 then yFlg(y)=-5;
    if yDol(y) < 0 then continue;   
	avgDol = yDol(y) / yRecMo(y);
    if avgDol < cMinDol(y) then
		yFlg(y)=1;
	else if cMaxDol(y) < avgDol then do;
        yFlg(y)=2;
        if cMinDol(y) <= avgDol / yRecMo(y) <= cMaxDol(y) then yFlg(y)=3;
    end;
end;

/* ==========================================================================
 *   DOL yearly amount
 * ========================================================================== */
do y=1 to dim(yRec);
    if yDol(y) = .  then yDol(y)=-4;
    if yRec(y) = -4 then yRec(y)=0;
end;

yflag=0;
array ydolo(*)   ayDolo78-ayDolo&cEndYr;
do i=1 to dim(ydol);
 if lintyr=i+77 & 0<lintmo<13 then do;
  if ydol(i)=-4 then do; ydol(i)=ydolo(i); yflag=1; end;
  else if ydol(i)>=0 & ydolo(i) not in (-4 0) then do;
    if ydolo(i)>0 then do; ydol(i)=ydol(i)+ydolo(i); yflag=2; end;
	else if ydolo(i) in (-1 -2 -3) then do; ydol(i)=ydolo(i); yflag=3; end;
  end;
 end;
end;

Food Stamps/SNAP Receipt

%let cEndYr = 123;		*(2023-1900);
%let cEndMo = 1488;     *(123+1)*12;
%let cYrs = 46;			*(2023-1978)+1;
%let cMos = 552;		*46*12;
%let cFillFlag = 2022;

data Q13;
 set prg2022.in_inc_2022;
 
VARIABLE NAMES IN PUBLIC RELEASE		VARIABLE NAMES IN PROGRAM
Q13RECIP-4~000001_2022 					RECIP41                    
Q13RECIP-4~000002_2022 					RECIP42                    
Q13RECIP-4~000003_2022 					RECIP43                    
Q13RECIP-4~000004_2022 					RECIP44                    
Q13RECIP-4B_2022       					RECIP4B                          
Q13F-B_2022            					qB                                    
Q13F-43B_2022          					q43B                                
Q13F-43C~M_2022        					q43CM                             
Q13F-43C~Y_2022        					q43CY                             
Q13F-44A1_CHK1.01_2022 					q44A1CHK101                
Q13F-44A1_CHK1.02_2022 					q44A1CHK102                
Q13F-44A3.01_2022      					q44A301                         
Q13F-44A3.02_2022      					q44A302                         
Q13F-44A5.01_2022      					q44A501                         
Q13F-44A6.01_2022      					q44A601                         
Q13F-44B.01_2022       					q44B01                           
Q13F-44B.02_2022       					q44B02                           
Q13F-44C.01~M_2022     					q44C01M                        
Q13F-44C.01~Y_2022     					q44C01Y                        
Q13F-44C.02~M_2022     					q44C02M                        
Q13F-44C.02~Y_2022     					q44C02Y                        
Q13F-44D.01_2022       					q44D01                           
Q13F-44D.02_2022       					q44D02                           
Q13F-45A.01_2022       					q45A01                           
Q13F-45A.02_2022       					q45A02                           
Q13F-45B.01~M_2022     					q45B01M                        
Q13F-45B.01~Y_2022     					q45B01Y                        
Q13F-45B.02~M_2022     					q45B02M                        
Q13F-45B.02~Y_2022     					q45B02Y                        
Q13F-47E.01_2022       					q47E01                           
Q13F-47E.02_2022       					q47E02                           
Q13F-62.01_2022        					q6201                             
Q13F-62.02_2022        					q6202                             
Q13F-62A.01_2022       					q62A01                           
Q13F-62A.02_2022       					q62A02                           
Q13F-63.01_2022        					q6301                             
Q13F-63A.01_2022       					q63A01                           
Q13F-63AA.01_2022      					q63AA01                         
Q13F-63AB.01_2022      					q63AB01                         
Q13F-63AB.02_2022      					q63AB02                         
Q13F-63B.01_2022       					q63B01                           
Q13F-63B.02_2022       					q63B02                           
Q13F-63C.01_2022       					q63C01                           
Q13F-63C.02_2022       					q63C02                           
Q13F-63D.01_2022       					q63D01                           
Q13F-63D.02_2022       					q63D02                           
Q13F-64.01_2022        					q6401
Q13F-64.02_2022        					q6402
Q13F-64A.01_2022       					q64A01
Q13F-64A.02_2022       					q64A02
CURDATE~D_2022         					intDay 
CURDATE~M_2022         					intMo
CURDATE~Y_2022         					intYr 
;          
       
data recip1;
 merge R30.lintdate2022 Q13 prg2022.oldin2022 (keep=norcid fydolo:);
 by norcid;
 if (intMo > 0); 
recip = RECIP42;
                      
data recip;
 set recip1;
 
* convert Y2K changes to simple years;
array yrs (*) intYr q43cy
              q44c01y q44c02y
              q45b01y q45b02y;
do i=1 to dim(yrs);
    if yrs(i) > 0 then yrs(i)=yrs(i)-1900;
end;

* date of this interview;
intDt=intYr*12+intMo;

/* ==========================================================================
  Merge info from several sources + convert weeks to months.
  ========================================================================== */
dummy = -4; 
if q44c01y>-4 and q43cy>-4 and q44c01y<q43cy then do;
	q44c01y=q43cy;
	q44c01m=q43cm;
end;
else if q44c01y>-4 and q43cy>-4 and q44c01y>q43cy then do;
	q44c01y=q44c01y;
	q44c01m=q44c01m;
end;
else if q44c01y>-4 and q43cy>-4 and q44c01y=q43cy then do;
	q44c01m = max(q44c01m, q43cm);
	q44c01y = max(q44c01y, q43cy);
end;
else if q44c01y<=-4 and q43cy>-4 then do;
	q44c01m = max(q44c01m, q43cm);
	q44c01y = max(q44c01y, q43cy);
end;

/* nrOfRMos - # of months x was received (used when start/stop dates are not known).*/
array nrOfRMos  (*) q63ab01 q63ab02;
nrOfRMos(1) = max(nrOfRMos(1), q63a01, q63aa01);

* How much did R receive in each spell ;
array howMuch (*) q47e01-q47e02;  
array q63b    (*) q63b01-q63b02;  
do s=1 to dim(howMuch);
    if howMuch(s) = -4 then howMuch(s) = q63b(s);  
end;

/* ==========================================================================
   Recipiency status (rec) and amounts (dol) by months
   All months from 1978jan till dec of current year, indexed from 1900jan.
   78*12+1 = 937
   ========================================================================== */
array rec (937:&cEndMo);      * Did they receive x in that month?;
array dol (937:&cEndMo);      * How much?;

/* ========================================================================== 
 * No/unknown recipiency
 * ========================================================================== */
if recip=0 then do;
    do m=lint to intDt;
        rec(m)=-4;
        dol(m)=-4;
    end;
end;
else if recip in (-1,-2) then do;
    do m=lint to intDt;
        rec(m) = recip;
        dol(m) = recip;
    end;
end;
else if recip in (-3) then do;
    do m=lint to intDt;
        rec(m) = recip;
        dol(m) = recip;
    end;
end;

/* ==========================================================================
 * There was some recipiency, get details
 * ========================================================================== */
else do;
check = 0;	
array startMo (*) q44c01m q44c02m; 
array startYr (*) q44c01y q44c02y; 
array endMo (*) q45b01m q45b02m;
array endYr (*) q45b01y q45b02y;
array cont   (*) q44d01-q44d02; 
array q44b (*) q44b01 q44b02;
array q64  (*) q6401 Q6402;   
array q63c    (*) q63c01 Q63c02;

/* -----------------------------------------------------------------------------
 * Calculate starts and ends of spells if possible
 * -----------------------------------------------------------------------------*/
array starts (*) starts1-starts2; 
array ends   (*) ends1-ends2;     

* Continuous recipiency "ends" at interview date;
do s = 1 to dim(cont);
	if cont(s) = 1 or q63c(s)=9 then do; 
        endYr(s) = intYr;	
        endMo(s) = intMo;
    end;
end;

* Compute starts/stops/last for basic cases (dates are known);
do s = 1 to dim(starts);
    if startMo(s) > 0 & startYr(s) > 0 then
        starts(s) = startYr(s)*12+startMo(s);
    if endMo(s) > 0 & endYr(s) > 0 then
        ends(s) = endYr(s)*12+endMo(s);
end;

seamFlag=0;
do s = 1 to dim(starts);
	if starts(s) > 0 then do;
        if starts(s)=lint   then seamFlag=1; 
        if starts(s)=lint-1 then seamFlag=2; 
		if 0<starts(s)<lint-1 then seamFlag=3;
	end;
end;

lint_real=lint-936-1;
do s=1 to dim(starts);
  if starts(s) > 0 & starts(s)<lint then starts(s)=lint;
end;

* Try to deduce starts/ends from # of mos;
do s = 1 to dim(starts);
    if nrOfRMos(s) > 0 then do;
	    if ends(s) > 0 & starts(s) = .  then do;         
            starts(s) = ends(s) - nrOfRMos(s)+1;
			check=5.1;  
            if (starts(s) < lint) then check = 5;  
        end;
		else if starts(s) > 0 & ends(s) = .  then do;
            ends(s) = starts(s) + nrOfRMos(s) - 1;
			check=100+s+0.1; 
			if ends(s)>intDt then check=100+s;
        end;
		else if starts(s) = .  & ends(s) = .  then do; 
            if s = 1 then do; starts(s) = lint; check=201+0.1; end;   
			else do; starts(s) = ends(s-1) + 1; check=200+s+0.1; end; 
            ends(s) = starts(s) + nrOfRMos(s) - 1;
			if ends(s)>intDt then check=200+s; 
        end;
    end;
end;

/* Determine what was the last spell (0 - there was no spell)*/
last = 0;
if q44b01=2 or q43B = 2 then last = 1; 
do s = 1 to dim(starts);
    if ((startMo(s) > 0 | startYr(s) > 0 | endMo(s) > 0 | endYr(s) > 0 |nrOfRMos (s) > 0) & last < s) then last = s;
end;

/* -----------------------------------------------------------------------------
 * Edges of recipiency spells and periods between: n1 r1 n2 r2 ...  (NNNRRR???NRRR)
 * Under ideal conditions they touch, but often there is an unclear zone between them (-2)
 * ----------------------------------------------------------------------------- */
array rs(*) rs1-rs2;        * Recipiency starts;
array re(*) re1-re2;        * Recipiency ends;
array ns(*) ns1-ns3;        * Nonrecipiency starts (# of spells + 1, potentialy bef and after);
array ne(*) ne1-ne3;        * Nonrecipiency ends;

* The fully known cases;
do s = 1 to last;
    if starts(s) > 0 then do;
        ne(s) = starts(s)-1;
        rs(s) = starts(s);
    end;
    if ends(s) > 0 then do;
        re(s)   = ends(s);
        ns(s+1) = ends(s)+1;
    end;
end;

* The unknown and partially known cases;
do s = 1 to last;
    if starts(s) = . & startYr(s) > 0 then do;
		check=301;  
        xne = startYr(s)*12;  
        if ( s = 1 & lint <= xne ) |
           ( s > 1 & 0 < ends(s-1) < xne) |
           ( s > 1 & 0 < endYr(s-1) + 1 < startYr(s) )
            then do; ne(s) = xne; check=302; end;  
        if startYr(s) < endYr(s) then do;
            rs(s) = startYr(s)*12+12;  
			check=303;  
		end;
    end;
    if (ends(s) = . & endYr(s) > 0) then do; 
		check=401; 
        if 0 < startYr(s) < endYr(s) then do;
            re(s) = endYr(s)*12+1;   
            ns(s+1) = endYr(s)*12+13;
			check=402;
		end;
		if 0 < startYr(s) = endYr(s) then do; 
		  ns(s+1)=endYr(s)*12+13;
		  if rs(s)>0 then do;
			re(s)=rs(s);
		  end;
		  check=403;
		end;
    end;
end;

if (ne(1) > 0) then ns(1) = lint;               * There was a nonrecipiency at the begining;
if (ns(last+1) > 0) then ne(last+1) = intDt;    * There was a nonrecipiency at the end;

if last>=2 then do s = 2 to last; 
	if 0 < rs(s) & rs(s) = re(s-1) then check = 7;
end;
 if rs1 > 0 & rs1<lint then rs1=lint;
 
/* -----------------------------------------------------------------------------
 * Fill the recipiency status (rec) and amounts (dol).
 * ----------------------------------------------------------------------------- */
* First, assume we know nothing;
do m = lint to intDt;
    rec(m) = -2;
    dol(m) = -2;
end;

* Fill in info we know (any of the periods below can be empty);
do s = 1 to last;
    * nonrecipiency period;
    if (0 < ns(s) <= ne(s)) then
        do m = max(ns(s),lint) to min(ne(s),intDt);
            rec(m) = -4;
            dol(m) = -4;
        end;
    * recipiency period;
    if (0 < rs(s) <= re(s)) then
        do m = max(rs(s),lint) to min(re(s),intDt); 
            rec(m) = 1;
            dol(m) = howMuch(s);
			if howmuch(s) in (. -4) then do; dol(m)=-2; check5=s+0.1; end; 
        end;
    * we know only start - we are sure R received smth that month;
    else if (lint <= rs(s) <= intDt & re(s) = .) then do;  
        rec(rs(s)) = 1;
        dol(rs(s)) = howMuch(s);
        if howmuch(s) in (. -4) then do; dol(rs(s))=-2; check5=s+0.2; end; 
    end;
    * we know only end - we are sure R received smth that month (can be even intDt if continuous);
    else if (rs(s) = . & lint <= re(s) <= intDt) then do;
        rec(re(s)) = 1;
        dol(re(s)) = howMuch(s);
		if howmuch(s) in (. -4) then do;  dol(re(s))=-2; check5=s+0.3; end; 
    end;
end;

* The last period of nonrecipiency;
if (0 < ns(last+1) <= ne(last+1)) then
    do m = ns(last+1) to min(ne(last+1),intDt);
        rec(m) = -4;
        dol(m) = -4;
    end;
end;

/* ========================================================================== */
/* End if recip = .... (getting details for people who got something)
/* ========================================================================== */
* Fill in space at the end of year after the interview;
do m=intDt+1 to hbound(rec);
    rec(m)=-4;
    dol(m)=-4;
end;
endDt=intDt-936;
noInt=0;
if q44c01m in (-1,-2) & q44c01y in (-1,-2) then q43b = 2;

if noInt=0 then
    do i=lbound(rec) to hbound(rec);
        if rec(i)=. then rec(i)=-4;
        if dol(i)=. then dol(i)=-4;
    end;
if noInt=1 then
    do i=lbound(rec) to hbound(rec);
        if rec(i)=. then rec(i)=-5;
        if dol(i)=. then dol(i)=-5;
    end;
    
/* ==========================================================================
 * Calculating yearly variables.
 * ========================================================================== */
array recYM(&cYrs,12) rec1-rec&cMos;
array dolYM(&cYrs,12) dol1-dol&cMos;
array yRec(*)   yRec78-yRec&cEndYr;
array yDol(*)   yDol78-yDol&cEndYr;
array yRecMo(*) yRecMo78-yRecMo&cEndYr;

* Calculate yRec (any recipiency in a yr?);
do y=1 to dim(yRec);
    do m=1 to 12;
        if recYM(y,m) ~= . then    yRec(y)=0;
        if recYM(y,m) > 0 then do; yRec(y)=1; leave; end;
    end;
	if yRec(y)=0 then do m=1 to 12;
	    if recYM(y,m) in (-1 -2 -3) then do;  yRec(y)=recYM(y,m); leave; end;
	end;
	if yRec(y)=0 then yRec(y)=-4;
end;

* Calculate yDol ($ in yr), yRecMo (# of months of recipiency in each y);
do y=1 to dim(yRec);
    if yRec(y) > 0 then do;
		rrefuse = 0; drefuse = 0;
		rdk = 0;     ddk = 0; 
        do m=1 to 12;
			if dolYM(y,m) = -1 then drefuse = 1;
			else if dolYM(y,m) in (-2 -3) then ddk = 1; 
            if recYM(y,m) = -1 then rrefuse = 1;
			else if recYM(y,m) = -2 then rdk = 1;
		end;
		if rrefuse = 1 then yRecMo(y) = -1;
		else if rdk = 1 then yRecMo(y) = -2;
		else do; 
	        yRecMo(y) = 0;
	        do m=1 to 12;
	            if recYM(y,m) = 1 then yRecMo(y)=yRecMo(y)+1;
	        end;
	    end;		
	    if rrefuse = 1 or drefuse = 1 then yDol(y) = -3;
		else if rdk = 1 or ddk = 1 then yDol(y) = -3;   
		else do;
	        yDol(y) = 0;
	        do m=1 to 12;
	            if 0 <= dolYM(y,m) then yDol(y)=yDol(y)+dolYM(y,m); 
	        end;
	    end;
	end;
	else do;
		yDol(y)=yRec(y);
		yRecMo(y)=yRec(y);
	end;
end;

/* ==========================================================================
 *  Flag yearly amount that are not within possible bounds.
 * ========================================================================== */
array yFlg(*)   yFlg78-yFlg&cEndYr; 
array cMinDol (&cYrs) _temporary_ (&cYrs*46);  
array cMaxDol (&cYrs) _temporary_
    (244 275 316 344 342 360 364 366 400 415 472 511 549 544 550 555 560 568 568 735 735 835 835 969 22*814);
    
do y=1 to dim(yRec);
    yFlg(y) = 0;    
    if yRec(y) = -5 then yFlg(y)=-5;
    if yDol(y) < 0 then continue; 
	avgDol = yDol(y) / yRecMo(y);
    if avgDol < cMinDol(y) then
		yFlg(y)=1;
	else if cMaxDol(y) < avgDol then do;
        yFlg(y)=2;
        if cMinDol(y) <= avgDol / yRecMo(y) <= cMaxDol(y) then yFlg(y)=3;
    end;
end;

/* ==========================================================================
 *   DLI yearly amount  
 * ========================================================================== */
do y=1 to dim(yRec);
    if yDol(y) = .  then yDol(y)=-4;
    if yRec(y) = -4 then yRec(y)=0;
end;

yflag=0;
array ydolo(*)   fyDolo78-fyDolo&cEndYr;
do i=1 to dim(ydol);
 if lintyr=i+77 & 0<lintmo<13 then do;
  if ydol(i)=-4 then do; ydol(i)=ydolo(i); yflag=1; end;
  else if ydol(i)>=0 & ydolo(i) not in (-4 0) then do;
    if ydolo(i)>0 then do; ydol(i)=ydol(i)+ydolo(i); yflag=2; end;
	else if ydolo(i) in (-1 -2 -3) then do; ydol(i)=ydolo(i); yflag=3; end;
  end;
 end;
end;

Respondent/Dependent Child/Spouse/Partner SSI/SSDI Receipt

%let cEndYr = 123;		*(2023-1900);
%let cEndMo = 1488;     *(123+1)*12;
%let cYrs = 46;			*(2023-1978)+1;
%let cMos = 552;		*46*12;
%let cFillFlag = 2022;

data Q13;
 set prg2022.in_inc_2022;
 
VARIABLE NAMES IN PUBLIC RELEASE		VARIABLE NAMES IN PROGRAM
 Q13-37_2022                   				Q37                            		  
 Q13SSI-43A~000001_2022        				qr43A1                 
 Q13SSI-43A~000002_2022        				qr43A2                 
 Q13SSI-43A~000003_2022        				qr43A3                 
 Q13SSI-43A-NOKIDS_2022        				qr43ANOKIDS                 
 Q13SSI-A_2022                 				qrA                          
 Q13SSI-B1_2022                				qrB1                         
 Q13SSI-B2_2022                				qrB2                         
 Q13SSI-B3_2022                				qrB3                         
 Q13SSI-43D~000001_2022        				qr43D1                 
 Q13SSI-43D~000002_2022        				qr43D2                 
 Q13SSI-43D~000003_2022        				qr43D3                 
 Q13SSI-43C.01~M_2022          				qr43C1m                   
 Q13SSI-43C.01~Y_2022          				qr43C1y                   
 Q13SSI-43C.02~M_2022          				qr43C2m                   
 Q13SSI-43C.02~Y_2022          				qr43C2y                   
 Q13SSI-43C.03~M_2022          				qr43C3m                   
 Q13SSI-43C.03~Y_2022          				qr43C3y                   
 Q13SSI-44A1_CHK1.01.01_2022   				qr44A1CHK111            
 Q13SSI-44A1_CHK1.01.02_2022   				qr44A1CHK112            
 Q13SSI-44A1_CHK1.01.03_2022   				qr44A1CHK113            
 Q13SSI-44A1_CHK1.02.01_2022   				qr44A1CHK121            
 Q13SSI-44A1_CHK1.03.01_2022   				qr44A1CHK131            
 Q13SSI-44A1_CHK1.03.02_2022   				qr44A1CHK132            
 Q13SSI-44A3.01.01_2022        				qr44A311                 
 Q13SSI-44A3.01.02_2022        				qr44A312                 
 Q13SSI-44A3.01.03_2022        				qr44A313                 
 Q13SSI-44A3.02.01_2022        				qr44A321                 
 Q13SSI-44A3.03.01_2022        				qr44A331                 
 Q13SSI-44A3.03.02_2022        				qr44A332                 
 Q13SSI-44A5.01.01_2022        				qr44A511                 
 Q13SSI-44A5.02.01_2022        				qr44A521                 
 Q13SSI-44A5.03.01_2022        				qr44A531                 
 Q13SSI-44A6.01.01_2022        				qr44A611                 
 Q13SSI-44A6.02.01_2022        				qr44A621                 
 Q13SSI-44A6.03.01_2022        				qr44A631                 
 Q13SSI-44B.01.02_2022         				qr44B12                  
 Q13SSI-44B.01.03_2022         				qr44B13                  
 Q13SSI-44B.03.02_2022         				qr44B32                  
 Q13SSI-44C.01.02~M_2022       				qr44C12m                
 Q13SSI-44C.01.02~Y_2022       				qr44C12y                
 Q13SSI-44C.01.03~M_2022       				qr44C13m                
 Q13SSI-44C.01.03~Y_2022       				qr44C13y                
 Q13SSI-44C.03.02~M_2022       				qr44C32m                
 Q13SSI-44C.03.02~Y_2022       				qr44C32y                
 Q13SSI-44D.01.01_2022         				qr44D11                  
 Q13SSI-44D.01.02_2022         				qr44D12                  
 Q13SSI-44D.01.03_2022         				qr44D13                  
 Q13SSI-44D.02.01_2022         				qr44D21                  
 Q13SSI-44D.03.01_2022         				qr44D31                  
 Q13SSI-44D.03.02_2022         				qr44D32                  
 Q13SSI-45A.01.01_2022         				qr45A11                         
 Q13SSI-45A.01.02_2022         				qr45A12                         
 Q13SSI-45A.02.01_2022         				qr45A21                         
 Q13SSI-45A.03.01_2022         				qr45A31                         
 Q13SSI-45B.01.01~M_2022       				qr45B11m                       
 Q13SSI-45B.01.01~Y_2022       				qr45B11y                       
 Q13SSI-45B.01.02~M_2022       				qr45B12m                       
 Q13SSI-45B.01.02~Y_2022       				qr45B12y                       
 Q13SSI-45B.02.01~M_2022       				qr45B21m                       
 Q13SSI-45B.02.01~Y_2022       				qr45B21y                       
 Q13SSI-45B.03.01~M_2022       				qr45B31m                       
 Q13SSI-45B.03.01~Y_2022       				qr45B31y                       
 Q13SSI-47E.01.01_2022         				qr47E11                         
 Q13SSI-47E.01.02_2022         				qr47E12                         
 Q13SSI-47E.01.03_2022         				qr47E13                         
 Q13SSI-47E.02.01_2022         				qr47E21                         
 Q13SSI-47E.03.01_2022         				qr47E31                         
 Q13SSI-47E.03.02_2022         				qr47E32                         
 Q13SSI-62.01.01_2022          				qr6211                          
 Q13SSI-62.01.02_2022          				qr6212                          
 Q13SSI-62.01.03_2022          				qr6213                          
 Q13SSI-62.02.01_2022          				qr6221                          
 Q13SSI-62.03.01_2022          				qr6231                          
 Q13SSI-62.03.02_2022          				qr6232                          
 Q13SSI-62A.01.01_2022         				qr62A11                         
 Q13SSI-62A.01.02_2022         				qr62A12                         
 Q13SSI-62A.02.01_2022         				qr62A21                         
 Q13SSI-62A.03.01_2022         				qr62A31                         
 Q13SSI-63.02.01_2022          				qr6321                          
 Q13SSI-63.03.01_2022          				qr6331                          
 Q13SSI-63AB.01.01_2022        				qr63AB11                        
 Q13SSI-63B.01.01_2022         				qr63B11                         
 Q13SSI-63C.01.01_2022         				qr63C11                         
 Q13SSI-63C.01.02_2022         				qr63C12                         
 Q13SSI-63C.02.01_2022         				qr63C21                         
 Q13SSI-63C.03.01_2022         				qr63C31                     
 Q13SSI-63D.01.01_2022         				qr63D11                     
 Q13SSI-63D.01.02_2022         				qr63D12                     
 Q13SSI-63D.02.01_2022         				qr63D21                     
 Q13SSI-63D.03.01_2022         				qr63D31                     
 Q13SSI-64.01.01_2022          				qr6411                      
 Q13SSI-64.01.02_2022          				qr6412                      
 Q13SSI-64.02.01_2022          				qr6421                      
 Q13SSI-64.03.01_2022          				qr6431                      
 Q13SSI-64A.01.01_2022         				qr64A11                     
 Q13SSI-64A.01.02_2022         				qr64A12                     
 Q13SSI-64A.01.03_2022         				qr64A13                     
 Q13SSI-64A.02.01_2022         				qr64A21                     
 Q13SSI-64A.03.01_2022         				qr64A31                     
 Q13SSI-64A.03.02_2022         				qr64A32                      
 Q13SSI-SP-43A_2022            				qs43A                        
 Q13SSI-SP-43D~000001_2022     				qs43D1                 
 Q13SSI-SP-43D~000002_2022     				qs43D2                 
 Q13SSI-SP-43D~000003_2022     				qs43D3                 
 Q13SSI-SP-44A1_CHK1.01.01_2022				qs44A1CHK111            
 Q13SSI-SP-44A1_CHK1.01.02_2022				qs44A1CHK112            
 Q13SSI-SP-44A1_CHK1.01.03_2022				qs44A1CHK113            
 Q13SSI-SP-44A1_CHK1.02.01_2022				qs44A1CHK121            
 Q13SSI-SP-44A1_CHK1.02.02_2022				qs44A1CHK122            
 Q13SSI-SP-44A1_CHK1.02.03_2022				qs44A1CHK123            
 Q13SSI-SP-44A1_CHK1.03.01_2022				qs44A1CHK131            
 Q13SSI-SP-44A1_CHK1.03.02_2022				qs44A1CHK132            
 Q13SSI-SP-44A1_CHK1.03.03_2022				qs44A1CHK133  
 Q13SSI-SP-44A3.01.01_2022     				qs44A311                 
 Q13SSI-SP-44A3.01.02_2022     				qs44A312                 
 Q13SSI-SP-44A3.01.03_2022     				qs44A313                 
 Q13SSI-SP-44A3.02.01_2022     				qs44A321                 
 Q13SSI-SP-44A3.02.02_2022     				qs44A322                 
 Q13SSI-SP-44A3.02.03_2022     				qs44A323                 
 Q13SSI-SP-44A3.03.01_2022     				qs44A331                 
 Q13SSI-SP-44A3.03.02_2022     				qs44A332                 
 Q13SSI-SP-44A3.03.03_2022     				qs44A333  
 Q13SSI-SP-44A5.01.01_2022     				qs44A511                 
 Q13SSI-SP-44A5.02.01_2022     				qs44A521                 
 Q13SSI-SP-44A5.03.01_2022     				qs44A531                 
 Q13SSI-SP-44A6.01.01_2022     				qs44A611                 
 Q13SSI-SP-44A6.02.01_2022     				qs44A621                 
 Q13SSI-SP-44A6.03.01_2022     				qs44A631                 
 Q13SSI-SP-44B.01.01_2022      				qs44B11                  
 Q13SSI-SP-44B.01.02_2022      				qs44B12                  
 Q13SSI-SP-44B.01.03_2022      				qs44B13                  
 Q13SSI-SP-44B.02.01_2022      				qs44B21                  
 Q13SSI-SP-44B.02.02_2022      				qs44B22                  
 Q13SSI-SP-44B.02.03_2022      				qs44B23                  
 Q13SSI-SP-44B.03.01_2022      				qs44B31                  
 Q13SSI-SP-44B.03.02_2022      				qs44B32                  
 Q13SSI-SP-44B.03.03_2022      				qs44B33   
 Q13SSI-SP-44C.01.01~M_2022    				qs44C11m                
 Q13SSI-SP-44C.01.01~Y_2022    				qs44C11y                
 Q13SSI-SP-44C.01.02~M_2022    				qs44C12m                
 Q13SSI-SP-44C.01.02~Y_2022    				qs44C12y                
 Q13SSI-SP-44C.01.03~M_2022    				qs44C13m                
 Q13SSI-SP-44C.01.03~Y_2022    				qs44C13y                
 Q13SSI-SP-44C.02.01~M_2022    				qs44C21m                
 Q13SSI-SP-44C.02.01~Y_2022    				qs44C21y                
 Q13SSI-SP-44C.02.02~M_2022    				qs44C22m                
 Q13SSI-SP-44C.02.02~Y_2022    				qs44C22y                
 Q13SSI-SP-44C.03.01~M_2022    				qs44C31m                
 Q13SSI-SP-44C.03.01~Y_2022    				qs44C31y                
 Q13SSI-SP-44C.03.03~M_2022    				qs44C33m  
 Q13SSI-SP-44C.03.03~Y_2022    				qs44C33y  
 Q13SSI-SP-44D.01.01_2022      				qs44D11                  
 Q13SSI-SP-44D.01.02_2022      				qs44D12                  
 Q13SSI-SP-44D.01.03_2022      				qs44D13                  
 Q13SSI-SP-44D.02.01_2022      				qs44D21                  
 Q13SSI-SP-44D.02.02_2022      				qs44D22                  
 Q13SSI-SP-44D.03.01_2022      				qs44D31                  
 Q13SSI-SP-44D.03.03_2022      				qs44D33   
 Q13SSI-SP-45A.02.01_2022      				qs45A21                  
 Q13SSI-SP-45B.02.01~M_2022    				qs45B21m                
 Q13SSI-SP-45B.02.01~Y_2022    				qs45B21y                
 Q13SSI-SP-47E.01.01_2022      				qs47E11                  
 Q13SSI-SP-47E.01.02_2022      				qs47E12                  
 Q13SSI-SP-47E.01.03_2022      				qs47E13                  
 Q13SSI-SP-47E.02.01_2022      				qs47E21                  
 Q13SSI-SP-47E.02.02_2022      				qs47E22                  
 Q13SSI-SP-47E.03.01_2022      				qs47E31                  
 Q13SSI-SP-47E.03.03_2022      				qs47E33   
 Q13SSI-SP-62.01.01_2022       				qs6211                   
 Q13SSI-SP-62.01.02_2022       				qs6212                   
 Q13SSI-SP-62.01.03_2022       				qs6213                   
 Q13SSI-SP-62.02.01_2022       				qs6221                   
 Q13SSI-SP-62.02.02_2022       				qs6222                   
 Q13SSI-SP-62.03.01_2022       				qs6231                   
 Q13SSI-SP-62.03.03_2022       				qs6233    
 Q13SSI-SP-62A.02.01_2022      				qs62A21                 
 Q13SSI-SP-63AA.01.01_2022     				qs63AA11                 
 Q13SSI-SP-63AA.01.02_2022     				qs63AA12                 
 Q13SSI-SP-63AA.02.01_2022     				qs63AA21                 
 Q13SSI-SP-63AA.02.02_2022     				qs63AA22                 
 Q13SSI-SP-63AA.02.03_2022     				qs63AA23                 
 Q13SSI-SP-63AA.03.01_2022     				qs63AA31                 
 Q13SSI-SP-63AA.03.02_2022     				qs63AA32  
 Q13SSI-SP-63B.01.01_2022      				qs63B11                  
 Q13SSI-SP-63B.01.02_2022      				qs63B12                  
 Q13SSI-SP-63B.02.01_2022      				qs63B21                  
 Q13SSI-SP-63B.02.02_2022      				qs63B22                  
 Q13SSI-SP-63B.02.03_2022      				qs63B23                  
 Q13SSI-SP-63B.03.01_2022      				qs63B31                  
 Q13SSI-SP-63B.03.02_2022      				qs63B32   
 Q13SSI-SP-63C.01.01_2022      				qs63C11                  
 Q13SSI-SP-63C.01.02_2022      				qs63C12                  
 Q13SSI-SP-63C.02.01_2022      				qs63C21                  
 Q13SSI-SP-63C.02.02_2022      				qs63C22                  
 Q13SSI-SP-63C.02.03_2022      				qs63C23                  
 Q13SSI-SP-63C.03.01_2022      				qs63C31                  
 Q13SSI-SP-63C.03.02_2022      				qs63C32   
 Q13SSI-SP-63D.01.01_2022      				qs63D11                  
 Q13SSI-SP-63D.01.02_2022      				qs63D12                  
 Q13SSI-SP-63D.02.01_2022      				qs63D21                  
 Q13SSI-SP-63D.02.02_2022      				qs63D22                  
 Q13SSI-SP-63D.02.03_2022      				qs63D23                  
 Q13SSI-SP-63D.03.01_2022      				qs63D31                  
 Q13SSI-SP-63D.03.02_2022      				qs63D32   
 Q13SSI-SP-64.01.01_2022       				qs6411                   
 Q13SSI-SP-64.01.02_2022       				qs6412                   
 Q13SSI-SP-64.02.01_2022       				qs6421                   
 Q13SSI-SP-64.02.02_2022       				qs6422                   
 Q13SSI-SP-64.02.03_2022       				qs6423                   
 Q13SSI-SP-64.03.01_2022       				qs6431                   
 Q13SSI-SP-64.03.02_2022       				qs6432    
 Q13SSI-SP-64A.01.01_2022      				qs64A11                  
 Q13SSI-SP-64A.01.02_2022      				qs64A12                  
 Q13SSI-SP-64A.01.03_2022      				qs64A13                  
 Q13SSI-SP-64A.02.01_2022      				qs64A21                  
 Q13SSI-SP-64A.02.02_2022      				qs64A22                  
 Q13SSI-SP-64A.03.01_2022      				qs64A31                  
 Q13SSI-SP-64A.03.03_2022      				qs64A33   
 Q13-10_2022   	               				Q10
 CURDATE~D_2022   	           				intDay	
 CURDATE~M_2022                				intMo 
 CURDATE~Y_2022                				intYr;
                                          
data recip1;
 merge R30.lintdate2022 Q13 
 prg2022.oldin2022 (keep=norcid sydolo: dydolo: r30bydolo:);
 by norcid;
 if (intMo > 0);
noInt=0;
                      
data recip;
 set recip1;
 
* convert Y2K changes to simple years;
array yrs (*) intYr 
              qr43C1y qr43C2y qr43C3y
			  qr44C12y qr44C13y qr44C32y
			  qr45B11y qr45B12y qr45B21y qr45B31y
			  qs44C11y qs44C12y qs44C13y qs44C21y qs44C22y qs44C31y qs44C33y
			  qs45B21y;
do i=1 to dim(yrs);
    if yrs(i) > 0 then yrs(i)=yrs(i)-1900;
end;

* date of this interview;
intDt=intYr*12+intMo;

/* ==========================================================================
  Merge info from several sources + convert weeks to months.
  ========================================================================== */
dummy = -4;   
	qr44c11y=qr43c1y; qr44c21y=qr43c2y; qr44c31y=qr43c3y;
	qr44c11m=qr43c1m; qr44c21m=qr43c2m; qr44c31m=qr43c3m;
array nrOfRMos (6,3) qr63ab11 dummy dummy
					 dummy dummy dummy
					 dummy dummy dummy
					 qs63aa11 qs63aa12 dummy
					 qs63aa21 qs63aa22 qs63aa23 
					 qs63aa31 qs63aa32 dummy;
array howMuch (6,3) qr47e11 qr47e12 qr47e13
					qr47e21 dummy dummy
					qr47e31 qr47e32 dummy
					qs47e11 qs47e12 qs47e13
					qs47e21 qs47e22 dummy
					qs47e31 dummy  qs47e33;
array q63b  (6,3)   qr63b11 dummy dummy
					dummy dummy dummy
					dummy dummy dummy
					qs63b11 qs63b12 dummy
					qs63b21 qs63b22 qs63b23
					qs63b31 qs63b32 dummy;
					
do i=1 to 6;
  do j=1 to 3;
    if howMuch(i,j) = -4 then howMuch(i,j) = q63b(i,j);  
  end;
end;
array check (*) check1-check6;	
do i=1 to 6; check(i)=0; end;

array startMo (6,3) qr44c11m qr44c12m qr44c13m
					qr44c21m qr44c22m qr44c23m
					qr44c31m qr44c32m qr44c33m
					qs44c11m qs44c12m qs44c13m
					qs44c21m qs44c22m qs44c23m
					qs44c31m qs44c32m qs44c33m;					
array startYr (6,3) qr44c11y qr44c12y qr44c13y
					qr44c21y qr44c22y qr44c23y
					qr44c31y qr44c32y qr44c33y
					qs44c11y qs44c12y qs44c13y
					qs44c21y qs44c22y qs44c23y
					qs44c31y qs44c32y qs44c33y;						         
array endMo   (6,3) qr45B11m qr45B12m qr45B13m
					qr45B21m qr45B22m qr45B23m
					qr45B31m qr45B32m qr45B33m
					qs45B11m qs45B12m qs45B13m
					qs45B21m qs45B22m qs45B23m
					qs45B31m qs45B32m qs45B33m;
array endYr   (6,3) qr45B11y qr45B12y qr45B13y
					qr45B21y qr45B22y qr45B23y
					qr45B31y qr45B32y qr45B33y
					qs45B11y qs45B12y qs45B13y
					qs45B21y qs45B22y qs45B23y
					qs45B31y qs45B32y qs45B33y;
array cont    (6,3) qr44d11 qr44d12 qr44d13
					qr44d21 qr44d22 qr44d23
					qr44d31 qr44d32 qr44d33
					qs44d11 qs44d12 qs44d13
					qs44d21 qs44d22 qs44d23
					qs44d31 qs44d32 qs44d33;
array q44b    (6,3) qr44b11 qr44b12 qr44b13 
					qr44b21 qr44b22 qr44b23
					qr44b31 qr44b32 qr44b33
					qs44b11 qs44b12 qs44b13
					qs44b21 qs44b22 qs44b23
					qs44b31 qs44b32 qs44b33;
array q43b    (*) qr43b1-qr43b3 qs43b1-qs43b3; 
array q63c    (6,3) qr63c11 qr63c12 qr63c13
					qr63c21 qr63c22 qr63c23
					qr63c31 qr63c32 qr63c33
					qs63c11 qs63c12 qs63c13
					qs63c21 qs63c22 qs63c23
					qs63c31 qs63c32 qs63c33;
array starts (6,3) starts11-starts13 starts21-starts23 starts31-starts33
				   starts41-starts43 starts51-starts53 starts61-starts63;  
array ends   (6,3) ends11-ends13 ends21-ends23 ends31-ends33
				   ends41-ends43 ends51-ends53 ends61-ends63;  
array seamFlag (*) seamFlag1-seamFlag6;
array last (*) last1-last6;
array rs(6,3) rs11-rs13 rs21-rs23 rs31-rs33
              rs41-rs43 rs51-rs53 rs61-rs63;  * Recipiency starts; 
array re(6,3) re11-re13 re21-re23 re31-re33
              re41-re43 re51-re53 re61-re63;  * Recipiency ends;
array ns(6,4) ns11-ns14 ns21-ns24 ns31-ns34
              ns41-ns44 ns51-ns54 ns61-ns64;  * Nonrecipiency starts (# of spells + 1, potentialy bef and after);
array ne(6,4) ne11-ne14 ne21-ne24 ne31-ne34
              ne41-ne44 ne51-ne54 ne61-ne64;  * Nonrecipiency ends; 
array chk5 (*) chk51-chk56;
				
/* ==========================================================================
   Recipiency status (rec) and amounts (dol) by months
   All months from 1978jan till dec of current year, indexed from 1900jan.
   78*12+1 = 937
   ========================================================================== */
array rec (6, 937:&cEndMo) rec1_1-rec1_&cMos rec2_1-rec2_&cMos rec3_1-rec3_&cMos
						   rec4_1-rec4_&cMos rec5_1-rec5_&cMos rec6_1-rec6_&cMos;    
array dol (6, 937:&cEndMo) dol1_1-dol1_&cMos dol2_1-dol2_&cMos dol3_1-dol3_&cMos
						   dol4_1-dol4_&cMos dol5_1-dol5_&cMos dol6_1-dol6_&cMos;    
  
array q43d (*) qr43d1-qr43d3 qs43d1-qs43d3;

/* ========================================================================== 
 * No/unknown recipiency
 * ========================================================================== */
if qr43a3=1 or qr43ANOKIDS=0 then do i=1 to 3;
 do m=lint to intDt;
   rec(i,m)=-4;
   dol(i,m)=-4;
 end;
end;
if q10=0 or qs43a=0 then do i=4 to 6;
 do m=lint to intDt;
   rec(i,m)=-4;
   dol(i,m)=-4;
 end;
end;
if qr43a1 in (-1 -2 -3) or qr43ANOKIDS in (-1 -2 -3) or qr43D1 in (-1 -2 -3) then do i=1 to 3;
 do m=lint to intDt;
   rec(i,m)=-3;
   dol(i,m)=-3;
 end;
end;
if qs43a in (-1 -2 -3) or qs43d1 in (-1 -2 -3) then do i=4 to 6;
 do m=lint to intDt;
   rec(i,m)=-3;
   dol(i,m)=-3;
 end;
end;

/* ==========================================================================
 * There was some recipiency, get details
 * ========================================================================== */
do i=1 to 6;
if q43d(i)=0 then do m=lint to intDt;
  rec(i,m)=-4;
  dol(i,m)=-4;
end;
else if q43d(i)=1 then do;

/* -----------------------------------------------------------------------------
 * Calculate starts and ends of spells if possible
 * -----------------------------------------------------------------------------*/
* Continuous recipiency "ends" at interview date;
 do s = 1 to dim2(cont);
	if cont(i,s) = 1 or q63c(i,s)=13 then do; 
        endYr(i,s) = intYr;
        endMo(i,s) = intMo;
    end;
 end;
 
* Compute starts/stops/last for basic cases (dates are known);
 do s = 1 to dim2(starts);
    if startMo(i,s) > 0 & startYr(i,s) > 0 then
        starts(i,s) = startYr(i,s)*12+startMo(i,s);
    if endMo(i,s) > 0 & endYr(i,s) > 0 then
        ends(i,s) = endYr(i,s)*12+endMo(i,s);
 end;
 
 seamFlag(i)=0;
 do s = 1 to dim2(starts);
	if starts(i,s) > 0 then do;
        if starts(i,s)=lint   then seamFlag(i)=1; 
        if starts(i,s)=lint-1 then seamFlag(i)=2; 
		if 0<starts(i,s)<lint-1 then seamFlag(i)=3;
	end;
 end;
 
lint_real=lint-936-1;  
 do s=1 to dim2(starts);
  if starts(i,s) > 0 & starts(i,s)<lint then starts(i,s)=lint;
 end;
 
* Try to deduce starts/ends from # of mos;
 do s = 1 to dim2(starts);
    if nrOfRMos(i,s) > 0 then do;  
	    if ends(i,s) > 0 & starts(i,s) = .  then do;         
            starts(i,s) = ends(i,s) - nrOfRMos(i,s)+1;
			check(i)=5.1; 
            if (starts(i,s) < lint) then check(i) = 5;
        end;
		else if starts(i,s) > 0 & ends(i,s) = .  then do;
            ends(i,s) = starts(i,s) + nrOfRMos(i,s) - 1;
			check(i)=100+s+0.1;
			if ends(i,s)>intDt then check(i)=100+s;  
        end;
		else if starts(i,s) = .  & ends(i,s) = .  then do; 
            if s = 1 then do; starts(i,s) = lint; check(i)=201+0.1; end;    
			else do; starts(i,s) = ends(i,s-1) + 1; check(i)=200+s+0.1; end;
            ends(i,s) = starts(i,s) + nrOfRMos(i,s) - 1;
			if ends(i,s)>intDt then check(i)=200+s; 
        end;
    end;
 end;	
 
/* Determine what was the last spell */
 last(i) = 0;
 if q44b(i,1)=2 or q43B(i) = 2 then last(i) = 1; 
 do s = 1 to dim2(starts);
    if ((startMo(i,s) > 0 | startYr(i,s) > 0 | endMo(i,s) > 0 | endYr(i,s) > 0 |nrOfRMos (i,s) > 0) & last(i) < s) then last(i) = s;
 end;
 
/* -----------------------------------------------------------------------------
 * Edges of recipiency spells and periods between: n1 r1 n2 r2 ...  (NNNRRR???NRRR)
 * Under ideal conditions they touch, but often there is an unclear zone between them (-2)
 * ----------------------------------------------------------------------------- */
* The fully known cases;
 if last(i) ge 1 then do s = 1 to last(i);
    if starts(i,s) > 0 then do;
        ne(i,s) = starts(i,s)-1;
        rs(i,s) = starts(i,s);
    end;
    if ends(i,s) > 0 then do;
        re(i,s)   = ends(i,s);
        ns(i,s+1) = ends(i,s)+1;
    end;
 end;
 
* The unknown and partially known cases;
  if last(i) ge 1 then do s = 1 to last(i);
    if starts(i,s) = . & startYr(i,s) > 0 then do;
		check(i)=301; 
        xne = startYr(i,s)*12;  
        if ( s = 1 & lint < =xne ) |
           ( s > 1 & 0 < ends(i,s-1) < xne) |
           ( s > 1 & 0 < endYr(i,s-1) + 1 < startYr(i,s) )
            then do; ne(i,s) = xne; check(i)=302; end; 
        if startYr(i,s) < endYr(i,s) then do;
            rs(i,s) = startYr(i,s)*12+12;   
			check(i)=303;  
		end;
    end;
    if (ends(i,s) = . & endYr(i,s) > 0) then do;
		check(i)=401;
        if 0 < startYr(i,s) < endYr(i,s) then do;
            re(i,s) = endYr(i,s)*12+1;    
            ns(i,s+1) = endYr(i,s)*12+13; 
			check(i)=402;
		end;
		if 0 < startYr(i,s) = endYr(i,s) then do; 
		  ns(i,s+1)=endYr(i,s)*12+13;
		  if rs(i,s)>0 then do;
			re(i,s)=rs(i,s); 
		  end;
		  check(i)=403;
		end;
    end;
 end;
 if (ne(i,1) > 0) then ns(i,1) = lint;                  
 if (ns(i,last(i)+1) > 0) then ne(i,last(i)+1) = intDt; 
 if last(i)>1 then do s = 2 t o last(i);
	if 0 < rs(i,s) & rs(i,s) = re(i,s-1) then check(i) = 7;
 end;
 
/* -----------------------------------------------------------------------------
 * Fill the recipiency status (rec) and amounts (dol).
 * ----------------------------------------------------------------------------- */
* First, assume we know nothing;
 do m = lint to intDt;
    rec(i,m) = -2;
    dol(i,m) = -2;
 end;
 
* Fill in info we know (any of the periods below can be empty);
  if last(i) ge 1 then do s = 1 to last(i);
    * nonrecipiency period;
    if (0 < ns(i,s) <= ne(i,s)) then
        do m = max(ns(i,s),lint) to min(ne(i,s),intDt);
            rec(i,m) = -4;
            dol(i,m) = -4;
        end;
    * recipiency period;
    if (0 < rs(i,s) <= re(i,s)) then
        do m = max(rs(i,s),lint) to min(re(i,s),intDt);
            rec(i,m) = 1;
            dol(i,m) = howMuch(i,s);
			if howmuch(i,s) in (. -4) then do; dol(i,m)=-2; chk5(i)=s+0.1; end; 
        end;
    * we know only start - we are sure R received smth that month;
    else if (lint <= rs(i,s) <= intDt & re(i,s) = .) then do; 
        rec(i, rs(i,s)) = 1;
        dol(i, rs(i,s)) = howMuch(i,s);
		if howmuch(i,s) in (. -4) then do; dol(i, rs(i,s))=-2; chk5(i)=s+0.2; end;
    end;
    * we know only end - we are sure R received smth that month (can be even intDt if continuous);
    else if (rs(i,s) = . & lint <= re(i,s) <= intDt) then do;
        rec(i,re(i,s)) = 1;
        dol(i,re(i,s)) = howMuch(i,s);
		if howmuch(i,s) in (. -4) then do; dol(i, re(i,s))=-2; chk5(i)=s+0.3; end;
    end;
 end;
 
* The last period of nonrecipiency;
  if last(i) ge 1 then do;
    if (0 < ns(i, last(i)+1) <= ne(i, last(i)+1)) then
    do m = max(lint,ns(i, last(i)+1)) to min(ne(i,last(i)+1),intDt);
        rec(i,m) = -4;
        dol(i,m) = -4;
    end;
  end;
  
end;
end;

/* ========================================================================== */
/* End if recip = .... (getting details for people who got something)
/* ========================================================================== */
do i=1 to dim1(starts);
 do m=intDt+1 to hbound2(rec);
    rec(i,m)=-4;
    dol(i,m)=-4;
 end;
end;
endDt=intDt-936;
do i=1 to dim1(starts);
 if startMo(i,1) in (-1,-2) & startYr(i,1) in (-1,-2) then q43b(i) = 2;
end;
if noInt=0 then
    do i=1 to dim1(rec);
	  do m=lbound2(rec) to hbound2(rec);
        if rec(i,m)=. then rec(i,m)=-4;
        if dol(i,m)=. then dol(i,m)=-4;
	  end;
    end;
if noInt=1 then
    do i=1 to dim1(rec);
	 do m=lbound2(rec) to hbound2(rec);
        if rec(i,m)=. then rec(i,m)=-5;
        if dol(i,m)=. then dol(i,m)=-5;
	 end;
    end;
    
********** the respondent and spouse monthly amount;
***** respondent ssb (the sum of ssi and ssdi);
array rssb (937:&cEndMo) rssb1-rssb&cMos;
do m = lbound(rssb) to hbound(rssb);
   rssb(m)=-4;
end;   
if qr43d3=1 then do m = lint to intdt;
 if dol(3,m)>-4 then do;
  dol(1,m)=-3;
  dol(2,m)=-3;
  rssb(m)=dol(3,m);
 end;
end;
else do m = lint to intDt;
 if dol(1,m)=-4 & dol(2,m)=-4 then rssb(m)=-4;
 else if dol(1,m) in (-1 -2 -3) or dol(2,m) in (-1 -2 -3) then rssb(m)=-3;
 else rssb(m)=max(dol(1,m),0) + max(dol(2,m),0);
end;
***** spouse ssb (both ssi and ssdi);
array sssb (937:&cEndMo) sssb1-sssb&cMos;
do m = lbound(sssb) to hbound(sssb);
   sssb(m)=-4;
end;   
if qs43d3=1 then do m = lint to intdt;
 if dol(6,m)>-4 then do;
  dol(4,m)=-3;
  dol(5,m)=-3;
  sssb(m)=dol(6,m);
 end;
end;
else do m = lint to intDt;
 if dol(4,m)=-4 & dol(5,m)=-4 then sssb(m)=-4;
 else if dol(4,m) in (-1 -2 -3) or dol(5,m) in (-1 -2 -3) then sssb(m)=-3;
 else sssb(m)=max(dol(4,m),0) + max(dol(5,m),0);
end;
********** add up the respondent and the spouse monthly amount to come up with ssi, ssd and ssb;
***** ssi;
array ssi (937:&cEndMo) ssi1-ssi&cMos;
do m = lbound(ssi) to hbound(ssi);
   ssi(m)=-4;
end;   
do m = lint to intDt;
 if dol(1,m)=-4 & dol(4,m)=-4 then ssi(m)=-4;
 else if dol(1,m) in (-1 -2 -3) or dol(4,m) in (-1 -2 -3) then ssi(m)=-3;
 else ssi(m)=max(dol(1,m),0) + max(dol(4,m),0);
end;
***** ssd;
array ssd (937:&cEndMo) ssd1-ssd&cMos;
do m = lbound(ssd) to hbound(ssd);
   ssd(m)=-4;
end;   
do m = lint to intDt;
 if dol(2,m)=-4 & dol(5,m)=-4 then ssd(m)=-4;
 else if dol(2,m) in (-1 -2 -3) or dol(5,m) in (-1 -2 -3) then ssd(m)=-3;
 else ssd(m)=max(dol(2,m),0) + max(dol(5,m),0);
end;
***** ssb;
array ssb (937:&cEndMo) ssb1-ssb&cMos;
do m = lbound(ssb) to hbound(ssb);
   ssb(m)=-4;
end;   
do m = lint to intDt;
 if rssb(m)=-4 & sssb(m)=-4 then ssb(m)=-4;
 else if rssb(m) in (-1 -2 -3) or sssb(m) in (-1 -2 -3) then ssb(m)=-3;
 else ssb(m)=max(rssb(m),0) + max(sssb(m),0);
end;
********** ssd & ssb array starting in Jan 2017;
if lintyr=117 then do; 
 if qr43d2 in (1 -1 -2 -3) or qr43d3=1 or qs43d2 in (1 -1 -2 -3) or qs43d3=1 then do;
  if lint>469 then do m=(469+936) to (lint-1);
   if ssd(m) in (-4 .) then ssd(m)=99999;
   if ssb(m) in (-4 .) then ssb(m)=99999;
  end;
 end;
 else do;
  if lint>469 then do m=(469+936) to (lint-1);
   if ssd(m) in (-4 .) then ssd(m)=99998;
   if ssb(m) in (-4 .) then ssb(m)=99998;
  end;
 end;
 *when we calculate the annual amount, we equate 99999 to -3 and 99998 to -4;
 
 if  ssd469=99999 and (qr43d2=1 or qs43d2=1) and (cases found in the comments) then do; 
  do m=(469+936) to &cEndMo;
   if -4<ssd(m)<99998 then do; ssd_1=ssd(m); ssd_1_m=m; m=&cEndMo;end;
  end;
  do m=(469+936) to &cEndMo;
   if ssd(m)=99999 then do; ssd(m)=ssd_1; ssb(m)=ssd_1; end; 
   else if ssd(m) ne 99999 then m=&cEndMo;
  end;
 end; 
end;

if  ssd469=99999 and ((qr43d2=1 & qr44c21m=1 & qr44c21y=2017) or
					  (qs43d2=1 & qs44c21m=1 & qs44c21y=2017)) then do;
 flag=1;
 do m=(469+936) to &cEndMo;
  if -4<ssd(m)<99998 then do; ssd_1a=ssd(m);ssd_1a_m=m; m=&cEndMo;end;
 end;
 do m=(469+936) to &cEndMo;
  if ssd(m)=99999 then do; ssd(m)=ssd_1a; ssb(m)=ssd_1a; end; 
  else if ssd(m) ne 99999 then m=&cEndMo;
 end;
end;
  
********** create the current round yearly ssi, ssd & ssb;
array ssiYM(&cYrs,12) ssi1-ssi&cMos;
array ssdYM(&cYrs,12) ssd1-ssd&cMos;
array ssbYM(&cYrs,12) ssb1-ssb&cMos;
array yssi(*)   yssi78-yssi&cEndYr;
array yssd1(*)   yssd78-yssd&cEndYr;
array yssb1(*)   yssb78-yssb&cEndYr;
%macro mty (vym, vy);
do y=1 to dim(&vy);
   m4=0; m3=0; mp=0;
   do m=1 to 12;
      if &vym(y,m) in (-4 . 99998) then m4=m4+1;
	  else if &vym(y,m) in (-1 -2 -3 99999) then m3=m3+1;
	  else if &vym(y,m)>=0 then mp=mp+1;
   end;
   if m3>0 then &vy(y)=-3;
   else if m4=12 then &vy(y)=-4;
   else do;
     &vy(y)=0;
	 do m=1 to 12;
	   if 0<&vym(y,m)<99998 then &vy(y)=&vy(y)+&vym(y,m);
	 end;
   end;
end;
%mend mty;

%mty(ssiYM, yssi);
%mty(ssdYM, yssd1);
%mty(ssbYM, yssb1);
********** connect the current round yearly variables to the old yearly variables;
array yssio(*)   syDolo78-syDolo&cEndYr;
array yssdo(*)   dyDolo117-dyDolo&cEndYr;
array yssbo(*)   r30byDolo117-r30byDolo&cEndYr;
array yssd(*)   yssd117-yssd&cEndYr; 
array yssb(*)   yssb117-yssb&cEndYr;

yiflag=0;
do Y=1 to dim(yssio);
 if lintyr=y+77 & 0<lintmo<13 then do;
  if yssi(y)=-4 then do; yssi(y)=yssio(y); yiflag=1; end;
  else if yssi(y)>=0 & yssio(y) not in (-4 0) then do;
    if yssio(y)>0 then do; yssi(y)=yssi(y)+yssio(y); yiflag=2; end;
	else if yssio(y) in (-1 -2 -3) then do; yssi(y)=yssio(y); yiflag=3; end;
  end;
 end;
end;
ydflag=0;
do Y=1 to dim(yssdo);
 if lintyr=y+116 & 0<lintmo<13 then do;
  if yssd(y)=-4 then do; yssd(y)=yssdo(y); ydflag=1; end;
  else if yssd(y)>=0 & yssdo(y) not in (-4 0) then do;
    if yssdo(y)>0 then do; yssd(y)=yssd(y)+yssdo(y); ydflag=2; end;
	else if yssdo(y) in (-1 -2 -3) then do; yssd(y)=yssdo(y); ydflag=3; end;
  end;
 end;
end;
ybflag=0;
do Y=1 to dim(yssbo);
 if lintyr=y+116 & 0<lintmo<13 then do;
  if yssb(y)=-4 then do; yssb(y)=yssbo(y); ybflag=1; end;
  else if yssb(y)>=0 & yssbo(y) not in (-4 0) then do;
    if yssbo(y)>0 then do; yssb(y)=yssb(y)+yssbo(y); ybflag=2; end;
	else if yssbo(y) in (-1 -2 -3) then do; yssb(y)=yssbo(y); ybflag=3; end;
  end;
 end;
end;

Summary Welfare (AFDC/TANF, Food Stamps/SNAP, SSI/SSDI) Receipt

%let cEndYr = 123;		*(2023-1900);
%let cEndMo = 1488;     *(123+1)*12;
%let cYrs = 46;			*(2023-1978)+1;
%let cMos = 552;		*46*12;
%let cFillFlag = 2022;

******* The following recipiency data includes the updates we had done in the current round; 
data one;
input  ;

VARIABLE NAMES IN PUBLIC RELEASE		VARIABLE NAMES IN PROGRAM
   CASEID_XRND                    			NORCID
   AFDC-TOTAL-1978_REVISED_XRND   			ay78
   FDSTMPS-TOTAL-1978_REVISED_XRND			fy78
   SSI-TOTAL-1978_REVISED_XRND    			siy78
   WELFARE-AMT-1978_REVISED_XRND  			wiyo78
   AFDC-TOTAL-1979_REVISED_XRND   			ay79
   FDSTMPS-TOTAL-1979_REVISED_XRND			fy79
   SSI-TOTAL-1979_REVISED_XRND    			siy79
   WELFARE-AMT-1979_REVISED_XRND  			wiyo79
   AFDC-TOTAL-1980_REVISED_XRND   			ay80
   FDSTMPS-TOTAL-1980_REVISED_XRND			fy80
   SSI-TOTAL-1980_REVISED_XRND    			siy80
   WELFARE-AMT-1980_REVISED_XRND  			wiyo80
   AFDC-TOTAL-1981_REVISED_XRND   			ay81
   FDSTMPS-TOTAL-1981_REVISED_XRND			fy81
   SSI-TOTAL-1981_REVISED_XRND    			siy81
   WELFARE-AMT-1981_REVISED_XRND  			wiyo81
   AFDC-TOTAL-1982_REVISED_XRND   			ay82
   FDSTMPS-TOTAL-1982_REVISED_XRND			fy82
   SSI-TOTAL-1982_REVISED_XRND    			siy82
   WELFARE-AMT-1982_REVISED_XRND  			wiyo82
   AFDC-TOTAL-1983_REVISED_XRND   			ay83
   FDSTMPS-TOTAL-1983_REVISED_XRND			fy83
   SSI-TOTAL-1983_REVISED_XRND    			siy83
   WELFARE-AMT-1983_REVISED_XRND  			wiyo83
   AFDC-TOTAL-1984_REVISED_XRND   			ay84
   FDSTMPS-TOTAL-1984_REVISED_XRND			fy84
   SSI-TOTAL-1984_REVISED_XRND    			siy84
   WELFARE-AMT-1984_REVISED_XRND  			wiyo84
   AFDC-TOTAL-1985_REVISED_XRND   			ay85
   FDSTMPS-TOTAL-1985_REVISED_XRND			fy85
   SSI-TOTAL-1985_REVISED_XRND    			siy85
   WELFARE-AMT-1985_REVISED_XRND  			wiyo85
   AFDC-TOTAL-1986_REVISED_XRND   			ay86
   FDSTMPS-TOTAL-1986_REVISED_XRND			fy86
   SSI-TOTAL-1986_REVISED_XRND    			siy86
   WELFARE-AMT-1986_REVISED_XRND  			wiyo86
   AFDC-TOTAL-1987_REVISED_XRND   			ay87
   FDSTMPS-TOTAL-1987_REVISED_XRND			fy87
   SSI-TOTAL-1987_REVISED_XRND    			siy87
   WELFARE-AMT-1987_REVISED_XRND  			wiyo87
   AFDC-TOTAL-1988_REVISED_XRND   			ay88
   FDSTMPS-TOTAL-1988_REVISED_XRND			fy88
   SSI-TOTAL-1988_REVISED_XRND    			siy88
   WELFARE-AMT-1988_REVISED_XRND  			wiyo88
   AFDC-TOTAL-1989_REVISED_XRND   			ay89
   FDSTMPS-TOTAL-1989_REVISED_XRND			fy89
   SSI-TOTAL-1989_REVISED_XRND    			siy89
   WELFARE-AMT-1989_REVISED_XRND  			wiyo89
   AFDC-TOTAL-1990_REVISED_XRND   			ay90
   FDSTMPS-TOTAL-1990_REVISED_XRND			fy90
   SSI-TOTAL-1990_REVISED_XRND    			siy90
   WELFARE-AMT-1990_REVISED_XRND  			wiyo90
   AFDC-TOTAL-1991_REVISED_XRND   			ay91
   FDSTMPS-TOTAL-1991_REVISED_XRND			fy91
   SSI-TOTAL-1991_REVISED_XRND    			siy91
   WELFARE-AMT-1991_REVISED_XRND  			wiyo91
   AFDC-TOTAL-1992_REVISED_XRND   			ay92
   FDSTMPS-TOTAL-1992_REVISED_XRND			fy92
   SSI-TOTAL-1992_REVISED_XRND    			siy92
   WELFARE-AMT-1992_REVISED_XRND  			wiyo92
   AFDC-TOTAL-1993_REVISED_XRND   			ay93
   AFDC-TOTAL-1994_REVISED_XRND   			ay94
   FDSTMPS-TOTAL-1993_REVISED_XRND			fy93
   FDSTMPS-TOTAL-1994_REVISED_XRND			fy94
   SSI-TOTAL-1993_REVISED_XRND    			siy93
   SSI-TOTAL-1994_REVISED_XRND    			siy94
   WELFARE-AMT-1993_REVISED_XRND  			wiyo93
   WELFARE-AMT-1994_REVISED_XRND  			wiyo94
   AFDC-TOTAL-1995_REVISED_XRND   			ay95
   AFDC-TOTAL-1996_REVISED_XRND   			ay96
   FDSTMPS-TOTAL-1995_REVISED_XRND			fy95
   FDSTMPS-TOTAL-1996_REVISED_XRND			fy96
   SSI-TOTAL-1995_REVISED_XRND    			siy95
   SSI-TOTAL-1996_REVISED_XRND    			siy96
   WELFARE-AMT-1995_REVISED_XRND  			wiyo95
   WELFARE-AMT-1996_REVISED_XRND  			wiyo96
   AFDC-TOTAL-1997_REVISED_XRND   			ay97
   AFDC-TOTAL-1998_REVISED_XRND   			ay98
   FDSTMPS-TOTAL-1997_REVISED_XRND			fy97
   FDSTMPS-TOTAL-1998_REVISED_XRND			fy98
   SSI-TOTAL-1997_REVISED_XRND    			siy97
   SSI-TOTAL-1998_REVISED_XRND    			siy98
   WELFARE-AMT-1997_REVISED_XRND  			wiyo97
   WELFARE-AMT-1998_REVISED_XRND  			wiyo98
   AFDC-TOTAL-1999_REVISED_XRND   			ay99
   AFDC-TOTAL-2000_REVISED_XRND   			ay100
   FDSTMPS-TOTAL-1999_REVISED_XRND			fy99
   FDSTMPS-TOTAL-2000_REVISED_XRND			fy100
   SSI-TOTAL-1999_REVISED_XRND    			siy99
   SSI-TOTAL-2000_REVISED_XRND    			siy100
   WELFARE-AMT-1999_REVISED_XRND  			wiyo99
   WELFARE-AMT-2000_REVISED_XRND  			wiyo100
   AFDC-TOTAL-2001_REVISED_XRND   			ay101
   AFDC-TOTAL-2002_REVISED_XRND   			ay102
   FDSTMPS-TOTAL-2001_REVISED_XRND			fy101
   FDSTMPS-TOTAL-2002_REVISED_XRND			fy102
   SSI-TOTAL-2001_REVISED_XRND    			siy101
   SSI-TOTAL-2002_REVISED_XRND    			siy102
   WELFARE-AMT-2001_REVISED_XRND  			wiyo101
   WELFARE-AMT-2002_REVISED_XRND  			wiyo102
   AFDC-TOTAL-2003_XRND           			ay103
   AFDC-TOTAL-2004_XRND           			ay104
   FDSTMPS-TOTAL-2003_XRND        			fy103
   FDSTMPS-TOTAL-2004_XRND        			fy104
   SSI-TOTAL-2003_XRND            			siy103
   SSI-TOTAL-2004_XRND            			siy104
   WELFARE-AMT-2003_XRND          			wiyo103
   WELFARE-AMT-2004_XRND          			wiyo104
   AFDC-TOTAL-2005_XRND           			ay105
   AFDC-TOTAL-2006_XRND           			ay106
   FDSTMPS-TOTAL-2005_XRND        			fy105
   FDSTMPS-TOTAL-2006_XRND        			fy106
   SSI-TOTAL-2005_XRND            			siy105
   SSI-TOTAL-2006_XRND            			siy106
   WELFARE-AMT-2005_XRND          			wiyo105
   WELFARE-AMT-2006_XRND          			wiyo106
   AFDC-TOTAL-2007_XRND           			ay107
   FDSTMPS-TOTAL-2007_XRND        			fy107
   SSI-TOTAL-2007_XRND            			siy107
   WELFARE-AMT-2007_XRND          			wiyo107
   AFDC-TOTAL-2008_XRND           			ay108
   FDSTMPS-TOTAL-2008_XRND        			fy108
   SSI-TOTAL-2008_XRND            			siy108
   WELFARE-AMT-2008_XRND          			wiyo108
   FDSTMPS-TOTAL-2009_XRND        			fy109
   SSI-TOTAL-2009_XRND            			siy109
   WELFARE-AMT-2009_XRND          			wiyo109
   AFDC-TOTAL-2009_XRND           			ay109
   AFDC-TOTAL-2010_XRND           			ay110
   FDSTMPS-TOTAL-2010_XRND        			fy110
   SSI-TOTAL-2010_XRND            			siy110
   WELFARE-AMT-2010_XRND          			wiyo110
   FDSTMPS-TOTAL-2011_XRND        			fy111
   WELFARE-AMT-2011_XRND          			wiyo111
   AFDC-TOTAL-2011_XRND           			ay111
   SSI-TOTAL-2011_XRND            			siy111
   AFDC-TOTAL-2012_XRND           			ay112
   FDSTMPS-TOTAL-2012_XRND        			fy112
   SSI-TOTAL-2012_XRND            			siy112
   WELFARE-AMT-2012_XRND          			wiyo112
   AFDC-TOTAL-2013_XRND           			ay113
   FDSTMPS-TOTAL-2013_XRND        			fy113
   SSI-TOTAL-2013_XRND            			siy113
   WELFARE-AMT-2013_XRND          			wiyo113
   AFDC-TOTAL-2014_XRND           			ay114
   FDSTMPS-TOTAL-2014_XRND        			fy114
   SSI-TOTAL-2014_XRND            			siy114
   WELFARE-AMT-2014_XRND          			wiyo114
   AFDC-TOTAL-2015_XRND           			ay115
   FDSTMPS-TOTAL-2015_XRND        			fy115
   SSI-TOTAL-2015_XRND            			siy115
   WELFARE-AMT-2015_XRND          			wiyo115
   AFDC-TOTAL-2016_XRND           			ay116
   AFDC-TOTAL-2017_XRND           			ay117
   FDSTMPS-TOTAL-2016_XRND        			fy116
   FDSTMPS-TOTAL-2017_XRND        			fy117
   SSI-TOTAL-2016_XRND            			siy116
   SSI-TOTAL-2017_XRND            			siy117
   WELFARE-AMT-2016_XRND          			wiyo116
   WELFARE-AMT-2017_XRND          			wiyo117
   AFDC-TOTAL-2018_XRND           			ay118
   AFDC-TOTAL-2019_XRND           			ay119
   FDSTMPS-TOTAL-2018_XRND        			fy118
   FDSTMPS-TOTAL-2019_XRND        			fy119
   SSI-TOTAL-2018_XRND            			siy118
   SSI-TOTAL-2019_XRND            			siy119
   WELFARE-AMT-2018_XRND          			wiyo118
   WELFARE-AMT-2019_XRND          			wiyo119
   AFDC-TOTAL-2020_XRND           			ay120
   AFDC-TOTAL-2021_XRND           			ay121
   FDSTMPS-TOTAL-2020_XRND        			fy120
   FDSTMPS-TOTAL-2021_XRND        			fy121
   SSI-TOTAL-2020_XRND            			siy120
   SSI-TOTAL-2021_XRND            			siy121
   WELFARE-AMT-2020_XRND          			wiyo120
   WELFARE-AMT-2021_XRND          			wiyo121
   AFDC-TOTAL-2022_XRND           			ay122
   AFDC-TOTAL-2023_XRND           			ay123
   FDSTMPS-TOTAL-2022_XRND        			fy122
   FDSTMPS-TOTAL-2023_XRND        			fy123
   SSI-TOTAL-2022_XRND            			siy122
   SSI-TOTAL-2023_XRND            			siy123
   
data Q13;
 set Q13a; 
 if intYr>0 then intYr=intYr-1900;
 
***** create afdc + food stamps + ssi as wiy( );
data two;
 merge one aupdate fupdate siupdate 
       R30.lintdate2022 (keep=norcid lIntYr lIntMo) Q13;
 by norcid;
 if aupdate=1 or fupdate=1 or siupdate=1;
 
array ay (*) ay78-ay123;
array fy (*) fy78-fy123;
array siy (*) siy78-siy123;
array wiy (*) wiy78-wiy123; 

do i=(lintyr-77) to (intyr-77);
 if ay(i)=-4 & siy(i)=-4 & fy(i)=-4 then wiy(i)=-4;
 else if ay(i) in (-1 -2 -3) or siy(i) in (-1 -2 -3) or fy(i) in (-1 -2 -3) then wiy(i)=-3;
 else wiy(i)=max(ay(i), 0)+max(siy(i), 0)+max(fy(i), 0);
end;