/*Program: Summarize permanent, temporary, and mitigation acres/LF by state; number of permits with mitigation credits by state */
/*Author: Jessica Balukas, ICF */
/*Created: 07/20/2018 */
/*Last Edited: 12/05/2019 */


/* Impacts and Mitigation (Acres and LF) by State */
* NOTE: Dataset prefiltered to remove mitigation type permits, permits with "Ocean/Harbor" or "Tidal Wetland" impact resource types, and permits issued outside the 2011-2015 period;
PROC IMPORT OUT= Permits404 
            DATAFILE= "C:\Users\40179\ICF\Step 2 Waters of the United States Final Rule - Average Annual 404 Permit Numbers and Impacts By State\SAS\Input\WOTUS_22_JUN_2018_2011-2015_NoOceanTidal_NoMitigation.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

proc means data=Permits404 N Mean Median Sum;
class STATE;
output out=ProcMeansTable(drop=_type_ _freq_) Sum= / autoname;
run;

proc export data=ProcMeansTable outfile='C:\Users\40179\ICF\Step 2 Waters of the United States Final Rule - Average Annual 404 Permit Numbers and Impacts By State\SAS\Output\Permits404_ProcMeans.csv' dbms = csv replace;
run;


/* Mitigation Credits and Permit Numbers (General, Individual) by State, Duplicates Removed */
proc sort data=Permits404;
by PermitID_Final;
run;

data Permits404_Mitigation;
set Permits404;
by PermitID_Final;
if first.PermitID_Final;
run;

data Permits404_Mitigation2;
set Permits404_Mitigation;
if (ACTION_TYPE='LOP') OR (ACTION_TYPE='SP') then Individual=1; else Individual=.;
if (ACTION_TYPE='NWP') OR (ACTION_TYPE='PGP') OR (ACTION_TYPE='RGP') then General=1; else General=.;
if (MIT_REQ_CREDITS > 0) then Credits = 1; else Credits = .;
run;

proc means data=Permits404_Mitigation2 N Mean Median Sum;
class STATE;
output out=ProcMeansTable_Mitigation(drop=_type_ _freq_) N= / autoname;
run;

proc export data=ProcMeansTable_Mitigation outfile='C:\Users\40179\ICF\Step 2 Waters of the United States Final Rule - Average Annual 404 Permit Numbers and Impacts By State\SAS\Output\Permits404_MitigationCredits_PermitNumbers_ByState.csv' dbms = csv replace;
run;


/* Impacts and Mitigation (Acres and LF) by Permit Type */
PROC IMPORT OUT= PermitType 
            DATAFILE= "C:\Users\40179\ICF\Step 2 Waters of the United States Final Rule - Average Annual 404 Permit Numbers and Impacts By State\SAS\Input\WOTUS_22_JUN_2018_2011-2015.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

proc print data=PermitType (obs=20);
run;

proc means data=PermitType N Mean Median Sum;
class PermitType;
output out=PermitTypeTable(drop=_type_ _freq_) Sum= / autoname;
run;

proc export data=PermitTypeTable outfile='C:\Users\40179\ICF\Step 2 Waters of the United States Final Rule - Average Annual 404 Permit Numbers and Impacts By State\SAS\Output\404Permits_PermitType.csv' dbms = csv replace;
run;


/* Mitigation Credits by Permit Type, Duplicates Removed */
proc sort data=PermitType;
by PermitID_Final;
run;

data PermitType_Mitigation;
set PermitType;
by PermitID_Final;
if first.PermitID_Final;
run;

data PermitType_Mitigation2;
set Permits404_Mitigation;
if (MIT_REQ_CREDITS > 0) then Credits = 1; else Credits = .;
run;

proc means data=PermitType_Mitigation2 N Mean Median Sum;
class PermitType;
output out=PermitTypeTable_Mitigation (drop=_type_ _freq_) N= / autoname;
run;

proc export data=PermitTypeTable_Mitigation outfile='C:\Users\40179\ICF\Step 2 Waters of the United States Final Rule - Average Annual 404 Permit Numbers and Impacts By State\SAS\Output\404Permits_MitigationCreditsByPermitType.csv' dbms = csv replace;
run; 



