/* Program to accompany the article "Timeline of living US presidents" by Rick Wicklin, published 08MAY2017 on The DO Loop, http://blogs.sas.com/content/iml/2017/05/01/timeline-of-living-us-presidents.html Date from https://en.wikipedia.org/wiki/Living_Presidents_of_the_United_States */ data LivingPres; input @1 Date anydtdte12. @13 Inauguration $26. @40 Death $26.; format Date date9.; daySpan = intck('day', lag(Date), Date); NumLiving + ^cmiss(Inauguration) - ^cmiss(Death); datalines; Apr 30, 1789 George Washington Mar 4, 1797 John Adams Dec 14, 1799 George Washington Mar 4, 1801 Thomas Jefferson Mar 4, 1809 James Madison Mar 4, 1817 James Monroe Mar 4, 1825 John Quincy Adams Jul 4, 1826 Thomas Jefferson Jul 4, 1826 John Adams Mar 4, 1829 Andrew Jackson Jul 4, 1831 James Monroe Jun 28, 1836 James Madison Mar 4, 1837 Martin Van Buren Mar 4, 1841 William Henry Harrison Apr 4, 1841 John Tyler William Henry Harrison Mar 4, 1845 James K. Polk Jun 8, 1845 Andrew Jackson Feb 23, 1848 John Quincy Adams Mar 4, 1849 Zachary Taylor Jun 15, 1849 James K. Polk Jul 9, 1850 Millard Fillmore Zachary Taylor Mar 4, 1853 Franklin Pierce Mar 4, 1857 James Buchanan Mar 4, 1861 Abraham Lincoln Jan 18, 1862 John Tyler Jul 24, 1862 Martin Van Buren Apr 15, 1865 Andrew Johnson Abraham Lincoln Jun 1, 1868 James Buchanan Mar 4, 1869 Ulysses S. Grant Oct 8, 1869 Franklin Pierce Mar 8, 1874 Millard Fillmore Jul 31, 1875 Andrew Johnson Mar 4, 1877 Rutherford B. Hayes Mar 4, 1881 James A. Garfield Sep 19, 1881 Chester A. Arthur James A. Garfield Mar 4, 1885 Grover Cleveland Jul 23, 1885 Ulysses S. Grant Nov 18, 1886 Chester A. Arthur Mar 4, 1889 Benjamin Harrison Jan 17, 1893 Rutherford B. Hayes Mar 4, 1897 William McKinley Mar 13, 1901 Benjamin Harrison Sep 14, 1901 Theodore Roosevelt William McKinley Jun 24, 1908 Grover Cleveland Mar 4, 1909 William Howard Taft Mar 4, 1913 Woodrow Wilson Jan 6, 1919 Theodore Roosevelt Mar 4, 1921 Warren G. Harding Aug 2, 1923 Calvin Coolidge Warren G. Harding Feb 3, 1924 Woodrow Wilson Mar 4, 1929 Herbert Hoover Mar 8, 1930 William Howard Taft Jan 5, 1933 Calvin Coolidge Mar 4, 1933 Franklin D. Roosevelt Apr 12, 1945 Harry S. Truman Franklin D. Roosevelt Jan 20, 1953 Dwight D. Eisenhower Jan 20, 1961 John F. Kennedy Nov 22, 1963 Lyndon B. Johnson John F. Kennedy Oct 20, 1964 Herbert Hoover Jan 20, 1969 Richard Nixon Mar 28, 1969 Dwight D. Eisenhower Dec 26, 1972 Harry S. Truman Jan 22, 1973 Lyndon B. Johnson Aug 9, 1974 Gerald Ford Jan 20, 1977 Jimmy Carter Jan 20, 1981 Ronald Reagan Jan 20, 1989 George H. W. Bush Jan 20, 1993 Bill Clinton Apr 22, 1994 Richard Nixon Jan 20, 2001 George W. Bush Jun 5, 2004 Ronald Reagan Dec 26, 2006 Gerald Ford Jan 20, 2009 Barack Obama Jan 20, 2017 Donald Trump ; /* graph of number of living presidents */ ods graphics / width=800px height=250px imagemap; /* enable data tips */ title "Timeline of Living US Presidents"; proc sgplot data=LivingPres; format date year4.; step x=Date y=NumLiving / markers markerattrs=(symbol=CircleFilled size=3) tip=(Date NumLiving Inauguration Death); xaxis grid display=(nolabel) offsetmin=0 offsetmax=0 min='01JAN1788'd max='01JAN2020'd; yaxis label="Number of Living Presidents" grid; run; /* simple mean */ proc means data=LivingPres; var NumLiving; run; /* Integral( N(t), t ) / Length_of_time */ proc iml; use LivingPres; read all var {NumLiving daySpan}; close; WeightedAve = sum( numLiving # daySpan ) / sum(daySpan); print WeightedAve; /* who was ex-president the longest? */ use LivingPres; read all var {Date Inauguration Death}; close; idx = loc(Inauguration ^= " "); span = j(ncol(idx), 1,.); do i = 1 to ncol(idx); j = idx[i]; k = loc( Death = Inauguration[j] ); if ncol(k)=0 then span[i] = intck('day', Date[j], today()); /* not dead yet */ else span[i] = intck('day', Date[j], Date[k]); end; maxIdx = span[<:>]; print (Inauguration[idx[maxIdx]]) (span[maxIdx]); /* how many times did a president die in office? */ diedIdx = loc(Inauguration ^= " " & Death ^= " "); diedInOffice = ncol(diedIdx); print diedInOffice, (Death[diedIdx]); QUIT;