REM Author: Jared Flake
@REM script to run code files 
setlocal enabledelayedexpansion
@echo off
(
set STARTTIME=%TIME%
set SAS_EXE=C:\Program Files\SASHome\SASFoundation\9.4\sas.exe
set CONFIG=C:\Program Files\SASHome\SASFoundation\9.4\nls\u8\sasv9.cfg

@REM echo Changing working directory to code directory. All paths are relative to the code directory...
@REM cd "../../file/path/if/needed"

echo Running 2_process_merge\0_wrds_data.sas...
start "0_wrds_data" "!SAS_EXE!" -CONFIG "!CONFIG!" -sysin 2_process_merge\0_wrds_data.sas -ICON -NOSPLASH -log ../logs/2_0_wrds_data.log -print ../logs/2_0_wrds_data.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

echo Running 0_classify_prep.py...
python 0_Classify_Q_A/0_classify_prep.py

echo Running 1_glz_classify_answers.py...
python 0_Classify_Q_A/1_glz_classify_answers.py

echo Running 2_combine_q_a.py...
python 0_Classify_Q_A/2_combine_q_a.py

echo Running 1_capiq_prep_files.py...
python 1_classify_analysts/ciq_ibes_merge/1_capiq_prep_files.py

echo Running 1_ibes_prep_files.sas...
"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin 1_classify_analysts\ciq_ibes_merge\1_ibes_prep_files.sas -ICON  -NOSPLASH -log ../logs/1_1_ibes_prep_files.log -print ../logs/1_1_ibes_prep_files.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

echo Running 2_ibes_capiq_format_names.py...
python 1_classify_analysts/ciq_ibes_merge/2_ibes_capiq_format_names.py

echo Running 1_classify_analysts steps...
"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "1_classify_analysts\ciq_ibes_merge\3_merge.sas" -log ../logs/1_3_merge.log -print ../logs/1_3_merge.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "1_classify_analysts\0_clean_IBES.sas" -log ../logs/1_0_clean_IBES.log -print ../logs/1_0_clean_IBES.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "1_classify_analysts\1_merge_ciq_analyst.sas" -log ../logs/1_1_merge_ciq_analyst.log -print ../logs/1_1_merge_ciq_analyst.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "1_classify_analysts\2a_var_classification.sas" -log ../logs/1_2a_var_classification.log -print ../logs/1_2a_var_classification.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "1_classify_analysts\2b_var_aggregation.sas" -log ../logs/1_2b_var_aggregation.log -print ../logs/1_2b_var_aggregation.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

echo Running SAS files in 2_process_merge directory...
echo Running 0_ files in parallel...
echo Running 2_process_merge\0_analyst_controls.sas...
start "0_analyst_controls" "!SAS_EXE!" -CONFIG "!CONFIG!" -sysin 2_process_merge\0_analyst_controls.sas -ICON -NOSPLASH -log ../logs/2_0_analyst_controls.log -print ../logs/2_0_analyst_controls.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

echo Running 2_process_merge\0_estimator_estimid_link.sas...
start "0_estimator_estimid_link" "!SAS_EXE!" -CONFIG "!CONFIG!" -sysin 2_process_merge\0_estimator_estimid_link.sas -ICON -NOSPLASH -log ../logs/2_0_estimator_estimid_link.log -print ../logs/2_0_estimator_estimid_link.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

echo Running 2_process_merge\0_fcst_errors.sas...
start "0_fcst_errors" "!SAS_EXE!" -CONFIG "!CONFIG!" -sysin 2_process_merge\0_fcst_errors.sas -ICON -NOSPLASH -log ../logs/2_0_fcst_errors.log -print ../logs/2_0_fcst_errors.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

echo Running 2_process_merge\0_mf_disclosure.sas...
start "0_mf_disclosure" "!SAS_EXE!" -CONFIG "!CONFIG!" -sysin 2_process_merge\0_mf_disclosure.sas -ICON -NOSPLASH -log ../logs/2_0_mf_disclosure.log -print ../logs/2_0_mf_disclosure.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)


@REM Wait for the previous 5 SAS jobs to finish before running the next one
:wait_for_jobs
echo Wait for the previous 5 SAS jobs to finish before running the next one...
timeout /t 5 >nul

tasklist /v | findstr /I "0_analyst_controls 0_estimator_estimid_link 0_fcst_errors 0_mf_disclosure 0_wrds_data" >nul
if %ERRORLEVEL%==0 (
    goto wait_for_jobs
)
echo Parallel SAS jobs completed.

echo Running 2_process_merge\01_bearish_ea.sas...
"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "2_process_merge\01_bearish_ea.sas" -log ../logs/2_01_bearish_ea.log -print ../logs/2_01_bearish_ea.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

echo Running 2_process_merge\1_files in parallel...
start "1_mf_mkt_response" "!SAS_EXE!" -CONFIG "!CONFIG!" -sysin 2_process_merge\1_mf_mkt_response.sas -ICON -NOSPLASH -log ../logs/2_1_mf_mkt_response.log -print ../logs/2_1_mf_mkt_response.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

start "1_restatements" "!SAS_EXE!" -CONFIG "!CONFIG!" -sysin 2_process_merge\1_restatements.sas -ICON -NOSPLASH -log ../logs/2_1_restatements.log -print ../logs/2_1_restatements.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

:wait_for_jobs_2
echo Wait for the previous 2 SAS jobs to finish before running the next one...
timeout /t 5 >nul

tasklist /v | findstr /I "1_mf_mkt_response 1_restatements" >nul
if %ERRORLEVEL%==0 (
    goto wait_for_jobs_2
)
echo Parallel SAS jobs completed.

@REM Wait for the previous 2 SAS jobs to finish before running the next files
"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "2_process_merge\2.0_det_of_engagement.sas" -log ../logs/2_20_det_of_engagement.log -print ../logs/2_20_det_of_engagement.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "2_process_merge\2.1_alt_classification.sas" -log ../logs/2_21_alt_classification.log -print ../logs/2_21_alt_classification.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "2_process_merge\3_patch_analyst_control_vars.sas" -log ../logs/2_3_patch_analyst_control_vars.log -print ../logs/2_3_patch_analyst_control_vars.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

"!SAS_EXE!" -CONFIG "!CONFIG!" -sysin "2_process_merge\4_compell_engage.sas" -log ../logs/2_4_compell_engage.log -print ../logs/2_4_compell_engage.lst
if %ERRORLEVEL% GEQ 2 (
    echo ERROR: SAS script failed.
    echo Errorlevel: [%ERRORLEVEL%]
)

echo Running Stata files...
"C:\Program Files\Stata17\StataBE-64.exe" /e do Run_Stata_Code.do -log ../logs/3_2_Stata.log

@REM Plot CAR for peers of Restating firms
echo creating Figure 3 for restatements
echo Running SAS Code to prepare data to plot ...
"C:\Program Files\SASHome\SASFoundation\9.4\sas.exe" -config "C:\Program Files\SASHome\SASFoundation\9.4\nls\u8\sasv9.cfg" -sysin 3_tables_figures/3_restatement_car_plots_prep.sas -log ../logs/3_3_restatement_car_plots_prep.log -print ../logs/3_3_restatement_car_plots_prep.lst
echo Running Python Code to plot ...
python 3_tables_figures/4_restatement_car_plots.py 


echo All scripts completed successfully.
set ENDTIME=%TIME%
echo Start time: !STARTTIME!, End time: !ENDTIME!
) > ../logs/run_all_files.log 2>&1
pause

