%SCFTAB  Create summary tables for data in Survey of Current Finances, 2004.;
%        Use create_inp.f90 and read_scf.f90 to extract variables of interest
%        from the SCF ascii data files.

%        Ellen McGrattan, 1-25-07
%        Revised, ERM, 2-3-07


%------------------------------------------------------------------------------
% LOAD DATA FROM SCF AND EQUATE TO VARIABLE NAMES
%------------------------------------------------------------------------------
iyear = input(['Year of survey? ', ...
               '(1=1989,2=1992,3=1995,4=1998,5=2001,6=2004,7=2007) ']);
iload = input('Load the data? (1=yes,0=no) ');
if (iyear==1); 
  if (iload==1); load scf89.dat; end;
  names89;
  l = length(scf89);
end;
if (iyear==2); 
  if (iload==1); load scf92.dat; end;
  names92;
  l = length(scf92);
end;
if (iyear==3); 
  if (iload==1); load scf95.dat; end;
  names95;
  l = length(scf95);
end;
if (iyear==4); 
  if (iload==1); load scf98.dat; end;
  names98;
  l = length(scf98);
end;
if (iyear==5); 
  if (iload==1); load scf01.dat; end;
  names01;
  l = length(scf01);
end;
if (iyear==6); 
  if (iload==1); load scf04.dat; end;
  names04;
  l = length(scf04);
end;
if (iyear==7); 
  if (iload==1); load scf07.dat; end;
  names07;
  l = length(scf07);
end;
o = ones(l,1);

%------------------------------------------------------------------------------
% WEIGHT
%------------------------------------------------------------------------------
    wgt = X42001/5;

%------------------------------------------------------------------------------
% DEMOGRAPHIC VARIABLES 
%------------------------------------------------------------------------------
%   Age of respondent and spouse
%       1:<35, 2:35-44, 3:45-54, 4:55-64, 5:65-74, 6:>=75;
%
    AGEr   = X14;
    AGECLr = 1+(AGEr>=35)+(AGEr>=45)+(AGEr>=55)+(AGEr>=65)+(AGEr>=75);
    AGEs   = X19;
    AGECLs = 1+(AGEs>=35)+(AGEs>=45)+(AGEs>=55)+(AGEs>=65)+(AGEs>=75);

%   Sex of respondent and spouse
%       1=male; 2=female
%
    SEXr   = X8021;
    SEXs   = X103;

%   Education 
%       1=no high school diploma/GED
%       2=high school diploma or GED
%       3=some college 
%       4=college degree
%
    EDUCr=X5901;
    EDCLr=1*o;
    i = find(X5902==1 | X5902==2);
    j = find(X5901>=13);
    k = find(X5904==1);
    EDCLr(i) = 2*o(i);
    EDCLr(j) = 3*o(j);
    EDCLr(k) = 4*o(k);

    EDUCs=X6101;
    EDCLs=1*o;
    i = find(X6102==1 | X6102==2);
    j = find(X6101>=13);
    k = find(X6104==1);
    EDCLs(i) = 2*o(i);
    EDCLs(j) = 3*o(j);
    EDCLs(k) = 4*o(k);


%   Marital status 
%       1=married
%       2=living with a partner,
%       3=separated
%       4=divorced
%       5=widowed
%       6=never married
%       0=person age 17 or less or not appropriate
%
    MSTATr= X8023;
    MSTATs= X105;
    SPpeu = X100;
    SPrel = X102;
    SPwith= X106;
    SPfdep= X107; 

%   Number of children (including natural, step, and foster)
%
    REL = [X108,X114,X120,X126,X132,X202,X208,X214,X220];
    KIDS=0;
    for i=1:9;
      KIDS=KIDS+(REL(:,i)==4 | REL(:,i)==13 | REL(:,i)==36);
    end;

%   Labor force participation 
%       1=working in some way 
%       0=not working at all
%
    LFr=1*o;
    i  = find((X4100>=50 & X4100<=80) | X4100==97);
    LFr(i)=0*o(i);

    LFs=1*o;
    i  = find((X4700>=50 & X4700<=80) | X4700==97);
    LFs(i)=0*o(i);


%   Detailed occupation categories for respondent
%       1=work for someone else 
%       2=self-employed/partnership,
%       3=retired/disabled 
%         + (student/homemaker/misc. not working and age 65 or older)
%       4=other groups not working (mainly those under 65 and out of LF);

    OCCATr = NaN*o;
    i = find(X4106==1);
    j = find(X4106==2 | X4106==3);
    k = find((X4100==50|X4100==52) |  ...
            ((X4100==21 | X4100==23 | X4100==30 | X4100==70 | X4100==80 | ...
              X4100==97 | X4100==85 | X4100==-7) & (AGEr>=65)));
    l = find(LFr==0 & X14<65);
    OCCATr(i) = 1*o(i);
    OCCATr(j) = 2*o(j);
    OCCATr(k) = 3*o(k);
    OCCATr(l) = 4*o(l);
    

%------------------------------------------------------------------------------
% INCOME VARIABLES 
%------------------------------------------------------------------------------

%
%   HH income in previous calendar year;
%

    INCOME  = max(0,X5729);
    PENSION = X6558+X6566+X6574+max(0,X6464)+max(0,X6469)+ ...
          max(0,X6474)+max(0,X6479)+max(0,X6484)+max(0,X6489)+ ...
          max(0,X6965)+max(0,X6971)+max(0,X6977)+max(0,X6983)+ ...
          max(0,X6989)+max(0,X6995);
    INCOME  = sumnan([INCOME,PENSION]')';

%
%   Normal income
%
    NORMINC = X7362.*(X7650~=3)+X5729.*(X7650==3);

%------------------------------------------------------------------------------
% ASSETS, DEBTS, AND NET WORTH 
%------------------------------------------------------------------------------

%
%   Checking accounts other than money market
%
    CHECKING= max(0,X3506).*(X3507==5)+max(0,X3510).*(X3511==5)+ ...
              max(0,X3514).*(X3515==5)+max(0,X3518).*(X3519==5)+ ...
              max(0,X3522).*(X3523==5)+max(0,X3526).*(X3527==5)+ ...
              max(0,X3529).*(X3527==5); 
    HCHECK  = (((X3507==5)+(X3511==5)+(X3515==5)+(X3519==5)+(X3523==5)+ ...
               (X3527==5)+(X3527==5))>0);

%
%   Savings accounts
%
    SAVING1  = max(0,X3730.*(X3732~=4 & X3732~=30)) ...
              +max(0,X3736.*(X3738~=4 & X3738~=30)) ...
              +max(0,X3742.*(X3744~=4 & X3744~=30)) ...
              +max(0,X3748.*(X3750~=4 & X3750~=30)) ...
              +max(0,X3754.*(X3756~=4 & X3756~=30)) ...
              +max(0,X3760.*(X3762~=4 & X3762~=30))+max(0,X3765);
    SAVING2  = max(0,X3804)+max(0,X3807)+max(0,X3810)+max(0,X3813) ...
              +max(0,X3816)+max(0,X3818);
    SAVING   = sumnan([SAVING1,SAVING2]')';
    HSAVING  = (SAVING>0);

%
%   Money market accounts (incl. checking)
%
    MMA =max(0,X3506).*(X3507==1)+max(0,X3510).*(X3511==1) ...
        +max(0,X3514).*(X3515==1)+max(0,X3518).*(X3519==1) ...
        +max(0,X3522).*(X3523==1)+max(0,X3526).*(X3527==1) ...
        +max(0,X3529).*(X3527==1) ...
        +max(0,X3730.*(X3732==4 | X3732==30)) ...
        +max(0,X3736.*(X3738==4 | X3738==30)) ...
        +max(0,X3742.*(X3744==4 | X3744==30)) ...
        +max(0,X3748.*(X3750==4 | X3750==30)) ...
        +max(0,X3754.*(X3756==4 | X3756==30)) ...
        +max(0,X3760.*(X3762==4 | X3762==30)) ...
        +max(0,X3706)+max(0,X3711)+max(0,X3716)+max(0,X3718);

    HMMA=(MMA>0);

%
%   Call accounts at brokerages
%
    CALL=max(0,X3930);
    HCALL=(CALL>0);

%
%   All transaction accounts (liquid assets)
%
    LIQ  = CHECKING+SAVING+MMA+CALL;
    HLIQ = (LIQ>0 | X3501==1 | X3701==1 | X3727==1 | X3801==1 | X3929==1);
    LIQ  = max(HLIQ,LIQ);
                                                                               
%
%   Certificates of deposit
%
    CDS=max(0,X3721);
    HCDS=(CDS>0);
                                                                               
%
%   Savings bonds
%
    SAVBND=X3902;
    HSAVBND=(SAVBND>0);

%
%   Bond face values (not including bond funds or savings bonds)
%     Tax-exempt, state and local
%     Mortgage-backed bonds
%     US government and government agency bonds and bills
%     Corporate bonds
%     Foreign bonds
%
    NOTXBNDFV=X3910;
    MORTBNDFV=X3906;
    GOVTBNDFV=X3908;
    CORPBNDFV=X7634;
    FORNBNDFV=X7633;
    OTHBNDFV =sumnan([X7633,X7634,X3912]')'; 
    BONDFV=NOTXBNDFV+MORTBNDFV+GOVTBNDFV+OTHBNDFV;
    BONDFVCHK=X6705;
    HBONDFV=(BONDFV>0);

%
%   Bond market values (not including bond funds or savings bonds)
%     Tax-exempt, state and local
%     Mortgage-backed bonds
%     US government and government agency bonds and bills
%     Corporate bonds
%     Foreign bonds
%
    NOTXBND=X7637;
    MORTBND=X7635;
    GOVTBND=X7636;
    CORPBND=X7639;
    FORNBND=X7638;
    BOND=NOTXBND+MORTBND+GOVTBND+CORPBND+FORNBND;
    BONDCHK=X6706;
    HBOND=(BOND>0);

%
%   Stocks (and number of different companies)
%
    STOCKS  =max(0,X3915);
    HSTOCKS =(STOCKS>0);
    NSTOCKS =X3914;

%
%   Mutual funds
%      Stock funds
%      Tax-free funds
%      Government bond funds
%      Other bond funds
%      Combination funds
%      Other funds
%
    STMUTF =(X3821==1).*max(0,X3822);
    TFBMUTF=(X3823==1).*max(0,X3824);
    GBMUTF =(X3825==1).*max(0,X3826);
    OBMUTF =(X3827==1).*max(0,X3828);
    COMUTF =(X3829==1).*max(0,X3830);
    OMUTF  =(X7785==1).*max(0,X7787);
    NMMF   =STMUTF+TFBMUTF+GBMUTF+OBMUTF+COMUTF+OMUTF;
    NMMFCHK=X6704;
    HNMMF  =(NMMF>0);
                                                                               
%
%   IRAs/Keoughs
%
    if (iyear<6) 
      IRAKH = max(0,X3610)+max(0,X3620)+max(0,X3630);
    else
      IRAKH = sum([X6551,X6559,X6567,X6552,X6560,X6568,X6553,X6561, ...
                   X6569,X6554,X6562,X6570]')';
    end
    HIRAKH = (IRAKH>0);
                                                                               
%
%   Future pensions (accumulated in an account for the R/S);
%
    FUTPEN = max(0,X5604)+max(0,X5612)+max(0,X5620)+max(0,X5628)+ ...
             max(0,X5636)+max(0,X5644);

%
%   Current pensions
%
    CURRPEN  = 0*o;
    if (iyear>=6);
      CURRPEN  = sumnan([X6462,X6467,X6472,X6477,X6482,X6487,X6957]')';
    end;
    if (iyear==5);
      CURRPEN  = sumnan([X6462,X6467,X6472,X6477,X6482,X6487]')';
    end;

%
%   Thrift plans
%
    THRIFT   = 0*o; 
    RTHRIFT  = 0*o; 
    STHRIFT  = 0*o; 
    PENEQ    = 0*o; 
    REQ      = 0*o; 
    SEQ      = 0*o; 
    HOLD     = 0*o;
    if (iyear<6);
      PTYPE =[X4216 X4316 X4416 X4816 X4916 X5016];
      PAMT  =[X4226 X4326 X4426 X4826 X4926 X5026];
      PBOR  =[X4227 X4327 X4427 X4827 X4927 X5027];
      PWIT  =[X4231 X4331 X4431 X4831 X4931 X5031];
      PALL  =[X4234 X4334 X4434 X4834 X4934 X5034];
      for i=1:6;
        HOLD=max(0,PAMT(:,i)).* ...
             ((PTYPE(:,i)==1 | PTYPE(:,i)==2) | PBOR(:,i)==1 | PWIT(:,i)==1);
        if (i<=3); 
          RTHRIFT = RTHRIFT+HOLD;
        else
          STHRIFT = STHRIFT+HOLD;
        end;  
        THRIFT = THRIFT+HOLD;
        PENEQ  = PENEQ+HOLD.*((PALL(:,i)==1)+.5*(PALL(:,i)==3));
        if (i<=3); 
          REQ = PENEQ;
        else
          SEQ = PENEQ-REQ;
        end;
      end;
      PMOP    = max(0,X4436);
      i       = find(PTYPE(:,1)~=0 & PTYPE(:,2)~=0 & PTYPE(:,3)~=0 & ...
                     PWIT(:,1)~=0   & PWIT(:,2)~=0   & PWIT(:,3)~=0);
      PMOP(i) = 0*ones(length(i),1);
      THRIFT  = THRIFT+PMOP;
      if (REQ>0);
        PENEQ = PENEQ+PMOP.*(REQ./RTHRIFT);
      else
        PENEQ = PENEQ+PMOP/2;
      end;

      PMOP    = max(0,X5036);
      i       = find(PTYPE(:,4)~=0 & PTYPE(:,5)~=0 & PTYPE(:,6)~=0 & ...
                     PWIT(:,4)~=0   & PWIT(:,5)~=0   & PWIT(:,6)~=0);
      PMOP(i) = 0*ones(length(i),1);
      THRIFT  = THRIFT+PMOP;
      if (SEQ>0);
        PENEQ = PENEQ+PMOP.*(SEQ./STHRIFT);
      else 
        PENEQ = PENEQ+PMOP/2;
      end;

    else
      PTYPE1=[X11000,X11100,X11200,X11300,X11400,X11500];
      PTYPE2=[X11001,X11101,X11201,X11301,X11401,X11501];
      PAMT  =[X11032,X11132,X11232,X11332,X11432,X11532];
      PBOR  =[X11025,X11125,X11225,X11325,X11425,X11525];
      PWIT  =[X11031,X11131,X11231,X11331,X11431,X11531];
      PALL  =[X11036 X11136 X11236 X11336 X11436 X11536];
      PPCT  =[X11037 X11137 X11237 X11337 X11437 X11537];
      for i=1:6;
        HOLD=max(0,PAMT(:,i)).* ...
             ((PTYPE1(:,i)==5 | PTYPE1(:,i)==6 | PTYPE1(:,i)==10) | ...
             ((PTYPE2(:,i)>=2 & PTYPE2(:,i)<=4)| PTYPE2(:,i)==6) | ...
               PBOR(:,i)==1 | PWIT(:,i)==1);
        if (i<=3); 
          RTHRIFT = RTHRIFT+HOLD;
        else
          STHRIFT = STHRIFT+HOLD;
        end;  
        THRIFT = THRIFT+HOLD;
        PENEQ  = PENEQ+HOLD.*((PALL(:,i)==1)+(PALL(:,i)==3).* ...
                 PPCT(:,i)/10000);
        if (i<=3);
          REQ  = PENEQ;
        else
          SEQ  = PENEQ-REQ;
        end;
      end;
      PMOP    = max(0,X11259);
      i       = find(PTYPE1(:,1)~=0 & PTYPE1(:,2)~=0 & PTYPE1(:,3)~=0 & ...
                     PWIT(:,1)~=0   & PWIT(:,2)~=0   & PWIT(:,3)~=0);
      PMOP(i) = 0*ones(length(i),1);
      THRIFT  = THRIFT+PMOP;
      PMOP    = max(0,X11559);
      i       = find(PTYPE1(:,4)~=0 & PTYPE1(:,5)~=0 & PTYPE1(:,6)~=0 & ...
                     PWIT(:,4)~=0   & PWIT(:,5)~=0   & PWIT(:,6)~=0);
      PMOP(i) = 0*ones(length(i),1);
      THRIFT  = THRIFT+PMOP;
    end;
    HTHRIFT =(THRIFT>0);

    RETQLIQ = sumnan([IRAKH,FUTPEN,CURRPEN,THRIFT]')';
    HRETQLIQ = (RETQLIQ>0);

%
%   Cash value of whole life insurance
%
    CASHLI=max(0,X4006);
    HCASHLI=(CASHLI>0);

%
%   Other managed assets (trusts, annuities and managed investment accounts)
%
    ANNUIT = sumnan([max(0,X6577),max(0,X6820)]')';
    TRUSTS = sumnan([max(0,X6587),max(0,X6835)]')';
    
    OTHMA  = sumnan([ANNUIT+TRUSTS,max(0,X3942)]')';
    HOTHMA = (OTHMA>0);

%
%   Other financial assets, includes loans from the household to someone
%     else,future proceeds, royalties, futures, non-public stock, deferred
%     compensation, oil/gas/mineral invest., cash n.e.c.
%
    OTHFIN=X4018+X4022.*((X4020>=61 & X4020<=66) | ...
                         (X4020>=71 & X4020<=74) | ...
                          X4020==77 | X4020==80 | X4020==81 | X4020==-7)+ ...
                 X4026.*((X4024>=61 & X4024<=66) | ...
                         (X4024>=71 & X4024<=74) | ...
                          X4024==77 | X4024==80 | X4024==81 | X4024==-7)+ ...
                 X4030.*((X4028>=61 & X4028<=66) | ...
                         (X4028>=71 & X4028<=74) | ...
                          X4028==77 | X4028==80 | X4028==81 | X4028==-7);
    %OTHFIN=X4018+X4022.*((X4020>=61 & X4020<=67) | ...
    %                     (X4020>=72 & X4020<=74) | ...
    %                     (X4020>=79 & X4020<=83) | X4020==77 )+ ...
    %             X4026.*((X4024>=61 & X4024<=67) | ...
    %                     (X4024>=72 & X4024<=74) | ...
    %                     (X4024>=79 & X4024<=83) | X4024==77 )+ ...
    %             X4030.*((X4028>=61 & X4028<=67) | ...
    %                     (X4028>=72 & X4028<=74) | ...
    %                     (X4028>=79 & X4028<=83) | X4028==77 );
    HOTHFIN=(OTHFIN>0);

%
%   Total financial assets 
%
    FIN=LIQ+CDS+SAVBND+BONDFV+STOCKS+NMMF+RETQLIQ+CASHLI+OTHMA+OTHFIN;
    HFIN=(FIN>0);

%
%   Vehicles
%   
    VEHIC=max(0,X8166)+max(0,X8167)+max(0,X8168)+max(0,X8188)+ ...
          max(0,X2422)+max(0,X2506)+max(0,X2606)+max(0,X2623);
    HVEHIC=(VEHIC>0);

%
%   Primary residence 
% 
    FARMBUS    = 0*o;
    X507       = min(9000,X507);
    FARMBUS    = max(0,X507/10000).*(X513+X526-X805-X905-X1005);
    X805       = X805.*((10000-max(0,X507))/10000);
    X905       = X905.*((10000-max(0,X507))/10000);
    X1005      = X1005.*((10000-max(0,X507))/10000);
    FARMBUS    = FARMBUS-(X1103==1).*X1108.*max(0,X507)/10000;
    X1108      = X1108-(X1103==1).*X1108.*max(0,X507)/10000;
    FARMBUS    = FARMBUS-(X1114==1).*X1119.*max(0,X507)/10000;
    X1119      = X1119-(X1114==1).*X1119.*max(0,X507)/10000;
    FARMBUS    = FARMBUS-(X1125==1).*X1130.*max(0,X507)/10000;
    X1130      = X1130-(X1125==1).*X1130.*max(0,X507)/10000;

    HOUSES=X604+X614+X623+X716+((10000-max(0,X507))/10000).*(X513+X526);
    HHOUSES=(HOUSES>0);

%
%   Other residential real estate 
%
    ORETYPE1   = (X1703==12|X1703==14|X1703==21|X1703==22|X1703==25| ...
                 (X1703>=40&X1703<=44)|X1703==49|X1703==50| X1703==52| ...
                  X1703==999);
    ORETYPE2   = (X1803==12|X1803==14|X1803==21|X1803==22|X1803==25| ...
                 (X1803>=40&X1803<=44)|X1803==49|X1803==50| X1803==52| ...
                  X1803==999);
    ORETYPE3   = (X1903==12|X1903==14|X1903==21|X1903==22|X1903==25| ...
                 (X1903>=40&X1903<=44)|X1903==49|X1903==50| X1903==52| ...
                  X1903==999);
    ORESRE     = max(X1405,X1409)+max(X1505,X1509)+max(X1605,X1609)+ ...
                 max(0,X1619)+max(0,X2002) ...
                 +ORETYPE1.*max(0,X1706).*(X1705/10000) ...
                 +ORETYPE2.*max(0,X1806).*(X1805/10000) ...
                 +ORETYPE3.*max(0,X1906).*(X1905/10000);
    HORESRE    = (ORESRE>0);

%
%   Net equity in nonresidential real estate
%
    NNRESRE =((X1703>=1 & X1703<=7) | X1703==10 | X1703==11 | X1703==13 | ...
               X1703==15 | X1703==24 | (X1703>=45 & X1703<=48) | ...
               X1703==51 | X1703==-7).* ...
               (max(0,X1706).*(X1705/10000)-X1715.*(X1705/10000)) ...
            +((X1803>=1 & X1803<=7) | X1803==10 | X1803==11 | X1803==13 | ...
               X1803==15 | X1803==24 | (X1803>=45 & X1803<=48) | ...
               X1803==51 | X1803==-7).* ...
               (max(0,X1806).*(X1805/10000)-X1815.*(X1805/10000)) ...
            +((X1903>=1 & X1903<=7) | X1903==10 | X1903==11 | X1903==13 | ...
               X1903==15 | X1903==24 | (X1903>=45 & X1903<=48) | ...
               X1903==51 | X1903==-7).* ...
               (max(0,X1906).*(X1905/10000)-X1915.*(X1905/10000)) ...
            +max(0,X2012)-X2016;

    tem78      = X2723.*(X2710==78)+X2740.*(X2727==78)+X2823.*(X2810==78)+ ...
                 X2840.*(X2827==78)+X2923.*(X2910==78)+X2940.*(X2927==78);
    FLAG781    = (NNRESRE~=0);
    i          = find(FLAG781);      
    NNRESRE(i) = NNRESRE(i)-tem78(i);
    HNNRESRE   = (NNRESRE~=0); 

%
%   Business interests;
%

    BUS=max(0,X3129)+max(0,X3124)-max(0,X3126).*(X3127==5)+ ...
        max(0,X3121).*(X3122==1 | X3122==6)+ ...
        max(0,X3229)+max(0,X3224)-max(0,X3226).*(X3227==5)+ ...
        max(0,X3221).*(X3222==1 | X3222==6)+ ...
        max(0,X3329)+max(0,X3324)-max(0,X3326).*(X3327==5)+ ...
        max(0,X3321).*(X3322==1 | X3322==6)+ ...
        max(0,X3335)+FARMBUS+ ...
        max(0,X3408)+max(0,X3412)+max(0,X3416)+max(0,X3420)+ ...
        max(0,X3424)+max(0,X3428);
    HBUS=(X3103==1 | X3401==1);
    

%
%   Other nonfinancial assets
%
    OTHNFIN=X4022+X4026+X4030-OTHFIN+X4018;
    HOTHNFIN=(OTHNFIN>0);
    
%
%   Total nonfinancial assets
%
    NFIN=VEHIC+HOUSES+ORESRE+NNRESRE+BUS+OTHNFIN;
    HNFIN=(NFIN~=0);
    NHNFIN=NFIN-HOUSES;
  
%
%   Total assets
%
    ASSET=FIN+NFIN;
    HASSET=(ASSET~=0);


%
%   Debt on primary residence (mortgages and home equity loans)
%
    tem        = X1108.*(X1103==1)+X1119.*(X1114==1)+X1130.*(X1125==1);
    LOC        = X1108+X1119+X1130;
    i          = (LOC>=1);
    HELOC      = i.*tem.*(1+max(0,X1136)./max(.1,LOC));
    MRTHEL     = X805+X905+X1005+HELOC+.5*(1-i).*(HOUSES>0).*max(0,X1136);
    NH_MORT    = MRTHEL-HELOC;
    HHELOC     = (HELOC>0);
    HNH_MORT   = (NH_MORT>0);
    HMRTHEL    = (MRTHEL>0);

%
%   Debt for other residential property
%

    MORT1      = ORETYPE1.*X1715.*(X1705/10000);
    MORT2      = ORETYPE2.*X1815.*(X1805/10000);
    MORT3      = ORETYPE3.*X1915.*(X1905/10000);
    RESDBT     = X1417+X1517+X1617+X1621+X2006+MORT1+MORT2+MORT3;

    FLAG782    = 0*o;
    i          = (FLAG781~=1 & ORESRE>0);
    FLAG782(i) = o(i);
    tem        = X2723.*(X2710==78)+X2740.*(X2727==78)+X2823.*(X2810==78)+ ...
                 X2840.*(X2827==78)+X2923.*(X2910==78)+X2940.*(X2927==78);
    RESDBT(i)  = RESDBT(i)+tem(i);

    FLAG67     = 0*o;
    i          = (ORESRE>0);
    tem67      = X2723.*(X2710==67)+X2740.*(X2727==67)+X2823.*(X2810==67)+ ...
                 X2840.*(X2827==67)+X2923.*(X2910==67)+X2940.*(X2927==67);
    RESDBT(i)  = RESDBT(i)+tem67(i);
    HRESDBT    = (RESDBT>0);

%
%   Debt on other lines of credit
%

    OTHLOC = X1108.*(X1103~=1)+X1119.*(X1114~=1)+X1130.*(X1125~=1);
    i      = (LOC>=1);
    OTHLOC = i.*OTHLOC.*(1+max(0,X1136)./max(.1,LOC))+ ...
             (1-i).*((HOUSES<=0)+.5*(HOUSES>0)).*(max(0,X1136));
    HOTHLOC=(OTHLOC>0);

%
%   Installment loans not classified elsewhere
%

    VEH_INST   = sumnan([X2218,X2318,X2418,X7169,X2424,X2519,X2619,X2625]')';
    EDN_INST1  = sumnan([X7824,X7847,X7870,X7924,X7947,X7970,X7179]')';
    EDN_INST   = sumnan([EDN_INST1, ...
                         X2723.*(X2710==83)+X2740.*(X2727==83)+ ...
                         X2823.*(X2810==83)+X2840.*(X2827==83)+ ...
                         X2923.*(X2910==83)+X2940.*(X2927==83)]')';
    INSTALL    = sumnan([VEH_INST,EDN_INST1,X7183,X1044,X1215,X1219]')';
    i          = find(FLAG781==0 & FLAG782==0);
    INSTALL(i) = INSTALL(i) + tem78(i); 
    i          = find(FLAG67==0);
    INSTALL(i) = INSTALL(i) + tem67(i); 
    INSTALL    = INSTALL +X2723.*(X2710~=67 & X2710~=78)+ ...
                          X2740.*(X2727~=67 & X2727~=78)+ ...
                          X2823.*(X2810~=67 & X2810~=78)+ ...
                          X2840.*(X2827~=67 & X2827~=78)+ ...
                          X2923.*(X2910~=67 & X2910~=78)+ ...
                          X2940.*(X2927~=67 & X2927~=78);
    OTH_INST   = sumnan([INSTALL,-VEH_INST,-EDN_INST]')';
    HVEH_INST  = (VEH_INST>0);
    HEDN_INST  = (EDN_INST>0);
    HOTH_INST  = (OTH_INST>0);
    HINSTALL   = (INSTALL>0);

%    
%   Credit card debt
%
    CCBAL = max(0,X413)+max(0,X421)+max(0,X424)+max(0,X427)+ ...
            max(0,X430)+max(0,X7575);
    HCCBAL=(CCBAL>0);


%
%   Other Debt (e.g., margin loans and pension loans not reported earlier)
%

    OUTMARG = max(0,X3932);
    OUTPEN1 = max(0,X11027).*(X11070==5);
    OUTPEN2 = max(0,X11127).*(X11170==5);
    OUTPEN3 = max(0,X11227).*(X11270==5);
    OUTPEN4 = max(0,X11327).*(X11370==5);
    OUTPEN5 = max(0,X11427).*(X11470==5);
    OUTPEN6 = max(0,X11527).*(X11570==5);
    ODEBT   = OUTMARG+OUTPEN1+OUTPEN2+OUTPEN3+OUTPEN4+OUTPEN5+ ...
              max(0,X4010)+max(0,X4032);
    HODEBT  = (ODEBT>0);
 
%
%   Total debt
%
    DEBT    = MRTHEL+RESDBT+OTHLOC+INSTALL+CCBAL+ODEBT;
    HDEBT   = (DEBT>0);

%
%   Total net worth
%
    NETWORTH  = ASSET-DEBT;
    HNETWORTH = o;

%
%   Capital gains
%
    KGINC     = X5712;
    KGHOUSE   = max([X513,X526,X604,X614,X623,X716]')'- ...
                max([X607,X617,X627+X631,X635,X717]')'-X1202;
    KGFARMBUS = (max(0,X507)/10000).*KGHOUSE;
    KGHOUSE   = KGHOUSE-KGFARMBUS;
    KGORE     = (X1705/10000).*(X1706-X1709)+(X1805/10000).*(X1806-X1809)+ ...
                (X1905/10000).*(X1906-X1909);
    KGBUS     = (X3129-X3130)+(X3229-X3230)+(X3329-X3330)+(X3408-X3409)+ ...
                (X3412-X3413)+(X3416-X3417)+(X3420-X3421)+(X3424-X3425)+ ...
                (X3428-X3429);
    KGBUS     = KGBUS+KGFARMBUS;
    KGSTMF    = (X3918-X3920)+(X3833-X3835);
    KGTOTAL   = KGHOUSE+KGORE+KGBUS+KGSTMF;
    HKGHOUSE  = (KGHOUSE~=0);
    HKGORE    = (KGORE~=0);
    HKGBUS    = (KGBUS~=0);
    HKGSTMF   = (KGSTMF~=0);
    HKGTOTAL  = (KGTOTAL~=0);

%------------------------------------------------------------------------------
% REPORT RESULTS
%------------------------------------------------------------------------------


    assets  = [ASSET,FIN,LIQ,CDS,SAVBND,BONDFV,STOCKS,NMMF,RETQLIQ, ...
               CASHLI,OTHMA,OTHFIN,NFIN,VEHIC,HOUSES,ORESRE,NNRESRE, ...
               BUS,OTHNFIN]/1000;
    aholders= [HASSET,HFIN,HLIQ,HCDS,HSAVBND,HBONDFV,HSTOCKS,HNMMF, ...
               HRETQLIQ,HCASHLI,HOTHMA,HOTHFIN,HNFIN,HVEHIC,HHOUSES, ...
               HORESRE,HNNRESRE,HBUS,HOTHNFIN];
    debts   = [DEBT,MRTHEL,NH_MORT,HELOC,RESDBT,OTHLOC,INSTALL,VEH_INST, ...
               EDN_INST,OTH_INST,CCBAL,ODEBT]/1000;
    dholders= [HDEBT,HMRTHEL,HNH_MORT,HHELOC,HRESDBT,HOTHLOC,HINSTALL, ...
               HVEH_INST,HEDN_INST,HOTH_INST,HCCBAL,HODEBT];
    gains   = [KGTOTAL,KGHOUSE,KGORE,KGBUS,KGSTMF]/1000;
    gholders= [HKGTOTAL,HKGHOUSE,HKGORE,HKGBUS,HKGSTMF];
    values  = [assets,debts,NETWORTH/1000,gains];
    holders = [aholders,dholders,HNETWORTH,gholders];

    [i,J]   = size(values);
    swgt    = sum(wgt);
    tab0    = zeros(J,3);
    tab1    = zeros(J,3);
    tab2    = zeros(J,3);
    tab3    = zeros(J,3);
    tab4    = zeros(J,3);
    tab5    = zeros(J,3);
    tab6    = zeros(J,3);
    tab7    = zeros(J,3);
    tab8    = zeros(J,3);
    married = (MSTATr==1 | MSTATr==2);
    singlef = (MSTATr==6 & SEXr==2);
    singlem = (MSTATr==6 & SEXr==1);
    otherf  = (MSTATr>=3 & MSTATr<=5 & SEXr==2);
    otherm  = (MSTATr>=3 & MSTATr<=5 & SEXr==1);
    for j=1:J;
      i         = find(holders(:,j));
      tab1(j,:) = [sum(wgt(i))/swgt*100,medwgt(values(i,j),wgt(i)), ...
                                       meanwgt(values(i,j),wgt(i))];
      i         = find(holders(:,j) & married);
      tab2(j,:) = [sum(wgt(i))/swgt*100,medwgt(values(i,j),wgt(i)), ...
                                       meanwgt(values(i,j),wgt(i))];
      i         = find(holders(:,j) & singlef);
      if isempty(i)
        tab3(j,:) = NaN*ones(1,3);
      else
        tab3(j,:) = [sum(wgt(i))/swgt*100,medwgt(values(i,j),wgt(i)), ...
                                       meanwgt(values(i,j),wgt(i))];
      end;
      i         = find(holders(:,j) & singlem);
      if isempty(i)
        tab4(j,:) = NaN*ones(1,3);
      else;
        tab4(j,:) = [sum(wgt(i))/swgt*100,medwgt(values(i,j),wgt(i)), ...
                                       meanwgt(values(i,j),wgt(i))];
      end;
      tab0(j,1) = sum(wgt.*values(:,j))/10^6;
      tab5(j,:) = [sum(wgt)/swgt*100,   medwgt(values(:,j),wgt), ...
                                       meanwgt(values(:,j),wgt)];
      i         = find(married);
      tab0(j,2) = sum(wgt(i).*values(i,j))/10^6;
      tab6(j,:) = [sum(wgt(i))/swgt*100,medwgt(values(i,j),wgt(i)), ...
                                       meanwgt(values(i,j),wgt(i))];
      i         = find(singlef);
      tab0(j,3) = sum(wgt(i).*values(i,j))/10^6;
      tab7(j,:) = [sum(wgt(i))/swgt*100,medwgt(values(i,j),wgt(i)), ...
                                       meanwgt(values(i,j),wgt(i))];
      i         = find(singlem);
      tab0(j,4) = sum(wgt(i).*values(i,j))/10^6;
      tab8(j,:) = [sum(wgt(i))/swgt*100,medwgt(values(i,j),wgt(i)), ...
                                       meanwgt(values(i,j),wgt(i))];
      i         = find(otherf);
      tab0(j,5) = sum(wgt(i).*values(i,j))/10^6;
      i         = find(otherm);
      tab0(j,6) = sum(wgt(i).*values(i,j))/10^6;
    end;
 
    yrlabel = ['1989';
               '1992';
               '1995';
               '1998';
               '2001';
               '2004';
               '2007'];

    clabel1 = 0;
    clabel2 = 0;
    clabel3 = ['% Families';
               '  Median  ';
               '   Mean   '];
    clabel3b= ['All Families ';
               '  Married    ';
               'Single Female';
               ' Single Male ';
               'Other Female ';
               ' Other Male  '];
    rlabel1 = [' '];
    rlabel2 = ['Assets                ';
               '  Financial Assets    ';
               '    Transaction accts ';
               '    CDs               ';
               '    Saving bonds      ';
               '    Bonds (face value)';
               '    Stocks            ';
               '    Mutual funds (nm) ';
               '    Retirement accts  ';
               '    Cash value, life  ';
               '    Other managed     ';
               '    Other assets      ';
               '  Nonfinancial Assets ';
               '    Vehicles          ';
               '    Primary residence ';
               '    Other res. prop.  ';
               '    Nonres. property  ';
               '    Business equity   ';
               '    Other assets      ';
               'Debts                 ';
               '  Secured by home     ';
               '    Mortgages         ';
               '    Home equity LOC   ';
               '  Secured by other res';
               '  Lines of credit     ';
               '  Installment loans   ';
               '    Vehicles          ';
               '    Education         ';
               '    Other             ';
               '  Credit card balance ';
               '  Other               ';
               'SCF Net Worth         ';
               'Capital Gains         ';
               '  Principal residence ';
               '  Other real estate   ';
               '  Business equity     ';
               '  Stocks&Mutual funds '];
    clc
    disp(['RESULTS OF THE ',yrlabel(iyear,:),' SCF SURVEY'])
    disp('------------------------------')
    disp(' ')
    disp('TABLE 0. Aggregates (Billions)')
    disp(mattab1(tab0,1,0,clabel1,clabel2,clabel3b,rlabel1,rlabel2,0))
    disp(' ')
    disp('TABLE 1. Assets, Debts, and Net Worth for All Families')
    disp('         (Conditional on ownership)')
    disp(mattab1(tab1,1,0,clabel1,clabel2,clabel3,rlabel1,rlabel2,0))

return
    disp(' ')
    disp(' ')
    disp('TABLE 2. Assets, Debts, and Net Worth for Married ')
    disp('         (Conditional on ownership)')
    disp(mattab1(tab2,1,0,clabel1,clabel2,clabel3,rlabel1,rlabel2,0))
    disp(' ')
    disp(' ')
    disp('TABLE 3. Assets, Debts, and Net Worth for Single Females')
    disp('         (Conditional on ownership)')
    disp(mattab1(tab3,1,0,clabel1,clabel2,clabel3,rlabel1,rlabel2,0))
    disp(' ')
    disp(' ')
    disp('TABLE 4. Assets, Debts, and Net Worth for Single Males')
    disp('         (Conditional on ownership)')
    disp(mattab1(tab4,1,0,clabel1,clabel2,clabel3,rlabel1,rlabel2,0))

    disp(' ')
    disp(' ')
    disp(' ')
    disp('TABLE 5. Assets, Debts, and Net Worth for All Families')
    disp('         (Not conditional on ownership)')
    disp(mattab1(tab5,1,0,clabel1,clabel2,clabel3,rlabel1,rlabel2,0))
    disp(' ')
    disp(' ')
    disp('TABLE 6. Assets, Debts, and Net Worth for Married ')
    disp('         (Not conditional on ownership)')
    disp(mattab1(tab6,1,0,clabel1,clabel2,clabel3,rlabel1,rlabel2,0))
    disp(' ')
    disp(' ')
    disp('TABLE 7. Assets, Debts, and Net Worth for Single Females')
    disp('         (Not conditional on ownership)')
    disp(mattab1(tab7,1,0,clabel1,clabel2,clabel3,rlabel1,rlabel2,0))
    disp(' ')
    disp(' ')
    disp('TABLE 8. Assets, Debts, and Net Worth for Single Males')
    disp('         (Not conditional on ownership)')
    disp(mattab1(tab8,1,0,clabel1,clabel2,clabel3,rlabel1,rlabel2,0))

