%let name=recession_job_losses;
filename odsout '.';

/*
SAS version of chart 2 from:
http://www.datasciencecentral.com/profiles/blogs/visualizations-comparing-tableau-spss-r-excel-matlab
http://api.ning.com/files/LGdhCycw6D89g6weijGnsZrCmUmes9sVNTRUqMC5240685Sd9U*7Z5LcEtxhb3kBISvCxmQG72x9d7hAASYwbJB520wLfzld/untitled4.png
*/

/*
Got the data from here:
http://data.bls.gov/timeseries/lns14000000
Selected 1948 to 2015
Clicked 'Go'
Scrolled down & "Download xls"
Save-as
SeriesReport-20161215080537_793823.xlsx
*/

PROC IMPORT OUT=unemp
 DATAFILE="../democd53/SeriesReport-20131210135510.xls"
 DBMS=EXCEL REPLACE;
 RANGE="BLS Data Series$A12:M81";
 GETNAMES=YES;
 MIXED=NO;
 SCANTEXT=YES;
 USEDATE=YES;
 SCANTIME=YES;
RUN;

proc transpose data=unemp (where=(year^=.)) out=unemp;
by year;
run;

data unemp; set unemp;
format date date9.;
date=input('15'||trim(left(_name_))||trim(left(year)),date9.);
format unemployment percent8.2;
unemployment=col1/100;
if date^=. and unemployment^=. then output;
run;



/*
Using dates from:
http://en.wikipedia.org/wiki/List_of_recessions_in_the_United_States
*/
data recessions; 
format start end next date9.;
informat start end next date9.;
input start end next;
datalines;
15nov1948 15oct1949 15jul1953
15jul1953 15may1954 15aug1957
15aug1957 15apr1958 15apr1960
15apr1960 15feb1961 15dec1969
15dec1969 15nov1970 15nov1973
15nov1973 15mar1975 15jan1980
15jan1980 15jul1980 15jul1981
15jul1981 15nov1982 15jul1990
15jul1990 15mar1991 15mar2001
15mar2001 15nov2001 15dec2007
15dec2007 15jun2009 15dec2015
;
run;


/* initialize all_data (create blank data set), so you can append to it later */
data all_data; 
run;


/* Get the data for one recession */
%macro get_info(start, end, next);

/* strategy 2 - assume the first month of the recession was the min unemployment */
proc sql;
select unemployment format comma10.4 into :min 
from unemp
where date=&start;
quit; run;

data foo; set unemp (where=(date>=&start and date<=&next));
format delta percentn7.0;
start=&start;
delta=&min-unemployment;
recession_year=put(&start,year4.);
run;

data foo; set foo;
if _n_=1 then num_months=0;
else num_months+1;
run;

/* Append data for this recession to the all_data data set */
data all_data; set all_data foo;
num_years=num_months/12;
run;

%mend;


/* Loop through all recessions, and get the info for each one */
data _null_;
 set recessions;
   call execute('%get_info('|| start ||','|| end ||','|| next ||');');
   call execute('run;');
run;

/* only keep the good data */
data all_data; set all_data;
if recession_year^=. and num_months^=. then output;
run;


goptions device=png;
goptions xpixels=1000 ypixels=600;
goptions noborder;
 
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm" 
 (title="Job Losses & Recovery During Recent U.S. Recessions") style=htmlblue;

goptions gunit=pct htitle=3.7 ftitle="albany amt/bold" htext=2.5 ftext="albany amt/bold" ctext=gray33;

axis1 label=(angle=90 j=c 'Percent Job Loss' j=c font="albany amt" 'Relative to Beginning of Recession')
  order=(-.06 to 0 by .01) major=none minor=none offset=(0,0) value=(t=7 ' ');

axis2 /* label=('Years after start of recession') */ label=none
 style=0 order=(0 to 7 by 1) major=none minor=none offset=(0,0);

footnote1  
 'Years after start of recession    '
 link="http://data.bls.gov/timeseries/lns14000000"
 c=gray font="albany amt"
 ls=1.0 "Data Source: http://data.bls.gov/timeseries/lns14000000";

legend1 label=none position=(top) offset=(4,0);

symbol1  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cx3635e0;
symbol2  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cx077909;
symbol3  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cxd8701e;
symbol4  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cx605076;
symbol5  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cxf2be1a;
symbol6  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cx40879e;
symbol7  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cyan;
symbol8  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cxb70c8f;
symbol9  value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cx010101;
symbol10 value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cx8d3506;
symbol11 value=circle c=gray h=1.7 interpol=join mode=include width=2 ci=cxfe0202;

title1 ls=1.5 "Percent Job Losses in Post WWII U.S. Recessions";

data all_data; set all_data;
length my_html $200;
my_html='title='||quote(
 'Recession starting in '||lowcase(trim(left(put(start,monyy7.))))||': '||'0d'x
 ||lowcase(put(date,monyy7.))||' unemployment rate = '||trim(left(put(unemployment,percent8.1)))||'0d'x
 ||'which is '||trim(left(put(-1*delta,percentn8.1)))||' worse than'||'0d'x
 ||'at the beginning of recession')
 ||' href="recession_job_losses_info.htm"';
run;

proc gplot data=all_data;
plot delta*num_years=recession_year /
 vaxis=axis1 haxis=axis2 noframe
 autovref cvref=graydd
 autohref chref=graydd
 legend=legend1
 html=my_html
 des='' name="&name";
run;

data recessions; set recessions;
year=year(start);
run;
title1 "Recession Start & End Dates";
title2 "(from " 
 link='https://en.wikipedia.org/wiki/List_of_recessions_in_the_United_States' 
 "Wikipedia)";
footnote;
proc print data=recessions noobs; 
format start end monyy7.;
var year start end;
run;

quit;
ODS HTML CLOSE;
ODS LISTING;
