Open Problem Solution 2012

1
Open Problem - SUAVe (SAS Users Association of Victoria, eh?) for May 29 2012 Meeting
Originally retrieved from: http://www.sascommunity.org/wiki/SUAVe_Open_Problem:
Using these two data sets, two different problems are posed. You can solve both or either.
The first problem is relatively straightforward: determine which of the people have birthdays that match one of
the dates in history. The birthdays do not need to match on year, just month and day.
Using the same two data sets, the second problem is a bit thornier. Find, for each person, the closest date
(year/month/day) in history. This closest date might be earlier or later in time than the birthdate.
Good luck! Please let one of the SUAVe executive know by May 24th if you'd like to walk through your solution
at the SUAVe meeting!
The following data represents dates in history (found on web site:
http://www.xemico.com/adc/infocenter/bigdates/wordhistory.html). Descriptions were truncated to keep lines
length 80. For the Open Problem, you could copy these and either paste into a file or into a SAS program.
Dates_in_history
19990104 Euro debuts as a financial unit in corporate and investment markets
19680105 Prague Spring begins in Czechoslovakia
19160108 Allies retreat from Gallipoli
19200110 League of Nations formally comes into being
19700115 Gadafi is proclaimed premier of Libya
19910116 The Persian Gulf War begins
19660119 Indira Gandhi becomes Prime Minister of India
19610120 John F. Kennedy inaugurated
19010122 Queen Victoria dies
19240125 First Winter Olympic Games
19260127 First public demonstration of a television system in London, UK
19860128 Space shuttle "Challenger" explodes during lift-off
19790201 Ayatollah Homeini returns to Iran
19430202 Battle of Stalingrad ends
19920207 European Union established
19450211 Yalta conference ends
19590216 Fidel Castro is sworn in as prime minister of Cuba
19810223 Spanish rebels storm Parliament
19350226 Hitler organizes Luftwaffe
19720302 Pioneer 10 launched to Jupiter
19170308 February Revolution in Russia begins
19300312 Gandhi leads civil disobedience
19950320 Nerve gas attach on Tokyo subway
19190323 Mussolini founds the Fascist party
19570325 European Economic Community established
19790326 Israel-Egyptian peace agreement signed
19390328 Spanish Civil War ends
2
19730329 United States withdraws from Vietnam
19820402 Argentina invades Falklands Islands
19680404 Marthin Luther King assassinated
19630407 A new constitution Tito the president for life of Yugoslavia
19610412 Soviet cosmonaut Yuri Gagarin becomes the first man in space
19120415 British ocean liner Titanic sinks 400 miles south of Newfoundland, Cana
19170416 Lenin returns to Russia to take the reins of Russian Revolution
19860426 Nuclear disaster at Chernobyl, Soviet Union (today Ukraine)
19690428 De Gaulle resigns as leader of France
19790504 Margaret Thatcher sworn in as UK's prime minister
19150507 British ocean liner Lusitania is torpedoed by a German submarine and si
19450508 German forces agree to an unconditional surrender ending World War II
19780509 Former Italian prime minister Aldo Moro found dead
19940510 Nelson Mandela is sworn in as the first black president of South Africa
19810513 Pope John Paul II shot in Rome's St. Peter's Square
19480514 State of Israel proclaimed
19680516 Massive protests begin in France
19270521 American pilot Charles A. Lindbergh lands at Le Bourget Field in Paris
19450523 Heinrich Himmler, the head of the Gestapo, commits suicide while in All
19530602 Queen Elizabeth II is formally crowned monarch of the United Kingdom
19190604 US Congress approved the 19th Amendment, which would guarantee suffrage
19670605 Six-Day War begins: The Israel launches simultaneous attacks on the ai
19390607 King George VI becomes the first British monarch to visit the United St
19370611 Soviet Union executes eight army leaders during "Great Purge"
19830613 Pioneer 10, the world's first outer-planetary probe, leaves the solar s
19630616 Soviet Cosmonaut Valentina Tereshkova becomes the first woman to travel
19560623 Gamal Abdel Nasser elected first president of the Republic of Egypt
19450626 Delegates from 50 nations sign the United Nations Charter
19140628 Archduke Franz Ferdinand, assassinated in Sarajevo, Bosnia
19970701 Hong Kong returned to China
19940708 Kim Il Sung, the communist dictator of North Korea, dies of a heart att
19850713 Live Aid concert at London and Philadelphia
19180716 In Yekaterinburg, Russia, Czar Nicholas II and his family are executed
19360718 Spanish Civil War breaks out
19690720 American astronaut Neil Armstrong walks on Moon
19520723 Military seizes power in Egypt
19430725 Benito Mussolini, fascist dictator of Italy, is voted out of power
19140728 Austria-Hungary declares war on Serbia, starting World War I
19140801 Germany and Russia declare war against each other
19450806 Atomic bomb is dropped on the city of Hiroshima in Japan killing 80,000
19740808 US President Richard M. Nixon resigns
19450809 An atomic bomb is detonated over the city of Nagasaki
19610815 East German authorities begin building the Berlin Wall
19770816 Elvis Presley dies
19680820 Soviets invade Czechoslovakia, crushing the "Prague Spring"
19390823 Molotov-Ribbentrop Pact signed
19440825 Paris is liberated after more than four years of Nazi occupation
19490829 Soviets explode atomic bomb at a remote test site at Semipalatinsk in K
3
19970831 Princess Diana killed in a car accident
19390901 Germany invades Poland, starting World War II
19450902 Japan formally surrenders to the Allies, ending World War II
19720906 Israeli hostages killed in Munich
20010911 Terrorist Attack on WTC in New York City and The Pentagon in Arlington,
19930913 Israel and Palestine sign a peace accord
19780917 Camp David Accords signed
19550919 Argentine President Juan Domingo Peron is deposed in a military coup
19800922 Iraq invades Iran starting 8-year war
19960927 In Afghanistan, the Taliban capture capital city Kabul
19390929 Germany and the Soviet Union agree to divide control of occupied Poland
19361001 General Franco proclaimed leader of the rebel Nationalist government in
19571004 Soviet Union launches Sputnik, the world's first artificial satellite
19891005 Dalai Lama wins Nobel Peace Prize
19491007 East Germany created within Soviet occupation zone
19901015 Soviet leader Mikhail Gorbachev wins the Nobel Peace Prize
19591021 Rocket designer Wernher von Braun moved from the US Army to the newly c
19621022 Cuban missile crises begin
19831023 Beirut barracks blown up
19291029 Black Tuesday: Stock market crashes leading to Great Depression
19841031 Indira Gandhi, the prime minister of India, is assassinated in New Delh
19931101 The Maastricht Treaty comes into effect, formally establishing the Euro
19171106 Russian Bolshevik Party launch a coup against Russia's Provisional Gove
19181111 Germany signed an armistice agreement with the Allies, ending World War
19901113 The first known World Wide Web page is written
19571115 Nikita Khrushchev challenges United States to a missile "shooting match
19451120 Nuremberg trials begin
19631122 US president, John F. Kennedy is assassinated in Dallas, Texas
19571127 Indian Prime Minister Nehru makes an impassioned speech for nuclear dis
19471129 U.N. votes for partition of Palestine despite strong Arab opposition
19671203 First human heart transplantation
19211206 The Irish Free State is declared
19411207 Japanese warplanes bombed US Pacific fleet at Pearl Harbor
19941211 Russian President Boris Yeltsin orders Russian forces into Chechnya
19011212 Marconi sends first radio transmission across the Atlantic Ocean
19031217 Wright brothers make the first successful flight in history of airplane
19841219 Agreement committing Britain to return Hong Kong to China in 1997 signe
19911221 Soviet republics proclaim the Commonwealth of Independent States, which
19401229 Worst air raid on London
19221230 Soviet Union established
Birth_dates
19450223 John
19620809 Susan
19880204 Michael
20020716 Elsie
19770303 Sylvia
4
Solutions:
Solution 1. Steve Gibbs (PharmaNet/i3)
* SUAVe Open Problem May 2012 *;
data event;
attrib e_date length=8 format=date9. label='Event Date';
length e_date3 $5. ;
format e_date2 date9.;
input e_date1 $ event $ 10-80 ;
e_date=input(e_date1,yymmdd10.);
e_date2=input(compress('2012'||substr(left(e_date1),5,4)),yymmdd10.);
e_date3=substr(put(e_date2,date9.),1,5);
cards;
19990104 Euro debuts as a financial unit in corporate and investment markets
19680105 Prague Spring begins in Czechoslovakia
19160108 Allies retreat from Gallipoli
19200110 League of Nations formally comes into being
19700115 Gadafi is proclaimed premier of Libya
19910116 The Persian Gulf War begins
19660119 Indira Gandhi becomes Prime Minister of India
19610120 John F. Kennedy inaugurated
19010122 Queen Victoria dies
19240125 First Winter Olympic Games
19260127 First public demonstration of a television system in London, UK
19860128 Space shuttle "Challenger" explodes during lift-off
19790201 Ayatollah Homeini returns to Iran
19430202 Battle of Stalingrad ends
19920207 European Union established
19450211 Yalta conference ends
19590216 Fidel Castro is sworn in as prime minister of Cuba
19810223 Spanish rebels storm Parliament
19350226 Hitler organizes Luftwaffe
19720302 Pioneer 10 launched to Jupiter
19170308 February Revolution in Russia begins
19300312 Gandhi leads civil disobedience
19950320 Nerve gas attach on Tokyo subway
19190323 Mussolini founds the Fascist party
19570325 European Economic Community established
19790326 Israel-Egyptian peace agreement signed
19390328 Spanish Civil War ends
19730329 United States withdraws from Vietnam
19820402 Argentina invades Falklands Islands
19680404 Marthin Luther King assassinated
19630407 A new constitution Tito the president for life of Yugoslavia
19610412 Soviet cosmonaut Yuri Gagarin becomes the first man in space
19120415 British ocean liner Titanic sinks 400 miles south of Newfoundland, Cana
19170416 Lenin returns to Russia to take the reins of Russian Revolution
19860426 Nuclear disaster at Chernobyl, Soviet Union (today Ukraine)
19690428 De Gaulle resigns as leader of France
19790504 Margaret Thatcher sworn in as UK's prime minister
19150507 British ocean liner Lusitania is torpedoed by a German submarine and si
19450508 German forces agree to an unconditional surrender ending World War II
19780509 Former Italian prime minister Aldo Moro found dead
19940510 Nelson Mandela is sworn in as the first black president of South Africa
5
19810513
19480514
19680516
19270521
19450523
19530602
19190604
19670605
19390607
19370611
19830613
19630616
19560623
19450626
19140628
19970701
19940708
19850713
19180716
19360718
19690720
19520723
19430725
19140728
19140801
19450806
19740808
19450809
19610815
19770816
19680820
19390823
19440825
19490829
19970831
19390901
19450902
19720906
20010911
19930913
19780917
19550919
19800922
19960927
19390929
19361001
19571004
19891005
19491007
19901015
19591021
19621022
19831023
19291029
19841031
19931101
19171106
Pope John Paul II shot in Rome's St. Peter's Square
State of Israel proclaimed
Massive protests begin in France
American pilot Charles A. Lindbergh lands at Le Bourget Field in Paris
Heinrich Himmler, the head of the Gestapo, commits suicide while in All
Queen Elizabeth II is formally crowned monarch of the United Kingdom
US Congress approved the 19th Amendment, which would guarantee suffrage
Six-Day War begins: The Israel launches simultaneous attacks on the ai
King George VI becomes the first British monarch to visit the United St
Soviet Union executes eight army leaders during "Great Purge"
Pioneer 10, the world's first outer-planetary probe, leaves the solar s
Soviet Cosmonaut Valentina Tereshkova becomes the first woman to travel
Gamal Abdel Nasser elected first president of the Republic of Egypt
Delegates from 50 nations sign the United Nations Charter
Archduke Franz Ferdinand, assassinated in Sarajevo, Bosnia
Hong Kong returned to China
Kim Il Sung, the communist dictator of North Korea, dies of a heart att
Live Aid concert at London and Philadelphia
In Yekaterinburg, Russia, Czar Nicholas II and his family are executed
Spanish Civil War breaks out
American astronaut Neil Armstrong walks on Moon
Military seizes power in Egypt
Benito Mussolini, fascist dictator of Italy, is voted out of power
Austria-Hungary declares war on Serbia, starting World War I
Germany and Russia declare war against each other
Atomic bomb is dropped on the city of Hiroshima in Japan killing 80,000
US President Richard M. Nixon resigns
An atomic bomb is detonated over the city of Nagasaki
East German authorities begin building the Berlin Wall
Elvis Presley dies
Soviets invade Czechoslovakia, crushing the "Prague Spring"
Molotov-Ribbentrop Pact signed
Paris is liberated after more than four years of Nazi occupation
Soviets explode atomic bomb at a remote test site at Semipalatinsk in K
Princess Diana killed in a car accident
Germany invades Poland, starting World War II
Japan formally surrenders to the Allies, ending World War II
Israeli hostages killed in Munich
Terrorist Attack on WTC in New York City and The Pentagon in Arlington,
Israel and Palestine sign a peace accord
Camp David Accords signed
Argentine President Juan Domingo Peron is deposed in a military coup
Iraq invades Iran starting 8-year war
In Afghanistan, the Taliban capture capital city Kabul
Germany and the Soviet Union agree to divide control of occupied Poland
General Franco proclaimed leader of the rebel Nationalist government in
Soviet Union launches Sputnik, the world's first artificial satellite
Dalai Lama wins Nobel Peace Prize
East Germany created within Soviet occupation zone
Soviet leader Mikhail Gorbachev wins the Nobel Peace Prize
Rocket designer Wernher von Braun moved from the US Army to the newly c
Cuban missile crises begin
Beirut barracks blown up
Black Tuesday: Stock market crashes leading to Great Depression
Indira Gandhi, the prime minister of India, is assassinated in New Delh
The Maastricht Treaty comes into effect, formally establishing the Euro
Russian Bolshevik Party launch a coup against Russia's Provisional Gove
6
19181111
19901113
19571115
19451120
19631122
19571127
19471129
19671203
19211206
19411207
19941211
19011212
19031217
19841219
19911221
19401229
19221230
;
run;
Germany signed an armistice agreement with the Allies, ending World War
The first known World Wide Web page is written
Nikita Khrushchev challenges United States to a missile "shooting match
Nuremberg trials begin
US president, John F. Kennedy is assassinated in Dallas, Texas
Indian Prime Minister Nehru makes an impassioned speech for nuclear dis
U.N. votes for partition of Palestine despite strong Arab opposition
First human heart transplantation
The Irish Free State is declared
Japanese warplanes bombed US Pacific fleet at Pearl Harbor
Russian President Boris Yeltsin orders Russian forces into Chechnya
Marconi sends first radio transmission across the Atlantic Ocean
Wright brothers make the first successful flight in history of airplane
Agreement committing Britain to return Hong Kong to China in 1997 signe
Soviet republics proclaim the Commonwealth of Independent States, which
Worst air raid on London
Soviet Union established
data dob;
attrib b_date length=8 format=date9. label='Birth Date';
length b_date3 $5. ;
format b_date2 date9.;
input b_date1 $ subject $10. ;
b_date=input(b_date1,yymmdd10.);
b_date2=input(compress('2012'||substr(left(b_date1),5,4)),yymmdd10.);
b_date3=substr(put(b_date2,date9.),1,5);
cards;
19450223 John
19620809 Susan
19880204 Michael
20020716 Elsie
19770303 Sylvia
;
run;
proc sql;
create table alldat as
select *
from dob,event;
create table closeobs1a as
select subject, b_date2, b_date3, event, e_date2, e_date3, b_date2-e_date2 as days,
abs(calculated days) as absdays,
min(calculated absdays) as close
from alldat
group by subject, b_date2, event, e_date2;
create table q1 as
select * from closeobs1a
where close=absdays=0;
create table q2 as
select subject, b_date, event, e_date, b_date-e_date as days,
abs(calculated days) as absdays,
min(calculated absdays) as close
from alldat
group by subject
having calculated absdays=calculated close;
7
quit;
title 'Q1 SUAVe Open Problem';
proc print data=q1 noobs width=minimum;
var subject b_date3 event e_date3 close;
run;
title 'Q2 SUAVe Open Problem';
proc print data=q2 noobs width=minimum;
var subject b_date event e_date close;
run;
**********************************************************************************;
8
Solution 2. Antoine Lalumière (Canadian Forest Service)
/***************************************************************************/
/* SUAVe Open Problem
*/
/* SUAVe (SAS Users Association of Victoria, eh?) for May 29 2012 Meeting */
/* Antoine Lalumière, Canadian Wood Fibre Centre, Natural Resources Canada */
/***************************************************************************/
/* Clear titles, options, and set options. */
title; title1; title2; options;
options ps=120 ls=170 nodate nonumber nocenter;
********************************************************************************;
/* Read in the two datasets, and apply formats. */
data edates;
format edate date9.;
informat edate yymmdd8.;
informat event $80.;
input edate event &;
cards;
19990104 Euro debuts as a financial unit in corporate and investment markets
19680105 Prague Spring begins in Czechoslovakia
19160108 Allies retreat from Gallipoli
19200110 League of Nations formally comes into being
19700115 Gadafi is proclaimed premier of Libya
19910116 The Persian Gulf War begins
19660119 Indira Gandhi becomes Prime Minister of India
19610120 John F. Kennedy inaugurated
19010122 Queen Victoria dies
19240125 First Winter Olympic Games
19260127 First public demonstration of a television system in London, UK
19860128 Space shuttle "Challenger" explodes during lift-off
19790201 Ayatollah Homeini returns to Iran
19430202 Battle of Stalingrad ends
19920207 European Union established
19450211 Yalta conference ends
19590216 Fidel Castro is sworn in as prime minister of Cuba
19810223 Spanish rebels storm Parliament
19350226 Hitler organizes Luftwaffe
19720302 Pioneer 10 launched to Jupiter
19170308 February Revolution in Russia begins
19300312 Gandhi leads civil disobedience
19950320 Nerve gas attach on Tokyo subway
19190323 Mussolini founds the Fascist party
19570325 European Economic Community established
19790326 Israel-Egyptian peace agreement signed
19390328 Spanish Civil War ends
19730329 United States withdraws from Vietnam
19820402 Argentina invades Falklands Islands
19680404 Marthin Luther King assassinated
19630407 A new constitution Tito the president for life of Yugoslavia
19610412 Soviet cosmonaut Yuri Gagarin becomes the first man in space
19120415 British ocean liner Titanic sinks 400 miles south of Newfoundland, Cana
19170416 Lenin returns to Russia to take the reins of Russian Revolution
19860426 Nuclear disaster at Chernobyl, Soviet Union (today Ukraine)
9
19690428
19790504
19150507
19450508
19780509
19940510
19810513
19480514
19680516
19270521
19450523
19530602
19190604
19670605
19390607
19370611
19830613
19630616
19560623
19450626
19140628
19970701
19940708
19850713
19180716
19360718
19690720
19520723
19430725
19140728
19140801
19450806
19740808
19450809
19610815
19770816
19680820
19390823
19440825
19490829
19970831
19390901
19450902
19720906
20010911
19930913
19780917
19550919
19800922
19960927
19390929
19361001
19571004
19891005
19491007
19901015
19591021
De Gaulle resigns as leader of France
Margaret Thatcher sworn in as UK's prime minister
British ocean liner Lusitania is torpedoed by a German submarine and si
German forces agree to an unconditional surrender ending World War II
Former Italian prime minister Aldo Moro found dead
Nelson Mandela is sworn in as the first black president of South Africa
Pope John Paul II shot in Rome's St. Peter's Square
State of Israel proclaimed
Massive protests begin in France
American pilot Charles A. Lindbergh lands at Le Bourget Field in Paris
Heinrich Himmler, the head of the Gestapo, commits suicide while in All
Queen Elizabeth II is formally crowned monarch of the United Kingdom
US Congress approved the 19th Amendment, which would guarantee suffrage
Six-Day War begins: The Israel launches simultaneous attacks on the ai
King George VI becomes the first British monarch to visit the United St
Soviet Union executes eight army leaders during "Great Purge"
Pioneer 10, the world's first outer-planetary probe, leaves the solar s
Soviet Cosmonaut Valentina Tereshkova becomes the first woman to travel
Gamal Abdel Nasser elected first president of the Republic of Egypt
Delegates from 50 nations sign the United Nations Charter
Archduke Franz Ferdinand, assassinated in Sarajevo, Bosnia
Hong Kong returned to China
Kim Il Sung, the communist dictator of North Korea, dies of a heart att
Live Aid concert at London and Philadelphia
In Yekaterinburg, Russia, Czar Nicholas II and his family are executed
Spanish Civil War breaks out
American astronaut Neil Armstrong walks on Moon
Military seizes power in Egypt
Benito Mussolini, fascist dictator of Italy, is voted out of power
Austria-Hungary declares war on Serbia, starting World War I
Germany and Russia declare war against each other
Atomic bomb is dropped on the city of Hiroshima in Japan killing 80,000
US President Richard M. Nixon resigns
An atomic bomb is detonated over the city of Nagasaki
East German authorities begin building the Berlin Wall
Elvis Presley dies
Soviets invade Czechoslovakia, crushing the "Prague Spring"
Molotov-Ribbentrop Pact signed
Paris is liberated after more than four years of Nazi occupation
Soviets explode atomic bomb at a remote test site at Semipalatinsk in K
Princess Diana killed in a car accident
Germany invades Poland, starting World War II
Japan formally surrenders to the Allies, ending World War II
Israeli hostages killed in Munich
Terrorist Attack on WTC in New York City and The Pentagon in Arlington,
Israel and Palestine sign a peace accord
Camp David Accords signed
Argentine President Juan Domingo Peron is deposed in a military coup
Iraq invades Iran starting 8-year war
In Afghanistan, the Taliban capture capital city Kabul
Germany and the Soviet Union agree to divide control of occupied Poland
General Franco proclaimed leader of the rebel Nationalist government in
Soviet Union launches Sputnik, the world's first artificial satellite
Dalai Lama wins Nobel Peace Prize
East Germany created within Soviet occupation zone
Soviet leader Mikhail Gorbachev wins the Nobel Peace Prize
Rocket designer Wernher von Braun moved from the US Army to the newly c
10
19621022
19831023
19291029
19841031
19931101
19171106
19181111
19901113
19571115
19451120
19631122
19571127
19471129
19671203
19211206
19411207
19941211
19011212
19031217
19841219
19911221
19401229
19221230
;
run;
Cuban missile crises begin
Beirut barracks blown up
Black Tuesday: Stock market crashes leading to Great Depression
Indira Gandhi, the prime minister of India, is assassinated in New Delh
The Maastricht Treaty comes into effect, formally establishing the Euro
Russian Bolshevik Party launch a coup against Russia's Provisional Gove
Germany signed an armistice agreement with the Allies, ending World War
The first known World Wide Web page is written
Nikita Khrushchev challenges United States to a missile "shooting match
Nuremberg trials begin
US president, John F. Kennedy is assassinated in Dallas, Texas
Indian Prime Minister Nehru makes an impassioned speech for nuclear dis
U.N. votes for partition of Palestine despite strong Arab opposition
First human heart transplantation
The Irish Free State is declared
Japanese warplanes bombed US Pacific fleet at Pearl Harbor
Russian President Boris Yeltsin orders Russian forces into Chechnya
Marconi sends first radio transmission across the Atlantic Ocean
Wright brothers make the first successful flight in history of airplane
Agreement committing Britain to return Hong Kong to China in 1997 signe
Soviet republics proclaim the Commonwealth of Independent States, which
Worst air raid on London
Soviet Union established
data bdates;
format bdate date9.;
informat bdate yymmdd8.;
input bdate name $;
cards;
19450223 John
19620809 Susan
19880204 Michael
20020716 Elsie
19770303 Sylvia
;
run;
/* Check the variables and formats. */
*proc contents data=edates; *run;
*proc contents data=bdates; *run;
*********************************************************************************;
/* Question 1.
*/
/* Determine which people have birthdays that match one of the dates in history.*/
/* The birthdays do not need to match on year, just month and day.
*/
*********************************************************************************;
title1 ' ';
title2 'Answer 1 - People having a birthdate that matches an event date - The Scenic
Route (data step merge)';
11
/* Extract month and day from the dates using month and day functions, for each
dataset. */
data edates2;
set edates;
month=month(edate);
day=day(edate);
run;
*proc print data=edates2; *run;
data bdates2;
set bdates;
month=month(bdate);
day=day(bdate);
run;
*proc print data=bdates2; * run;
/* Merge the two datasets, and keep only matches using subsetting if statement. */
proc sort data=edates2;
by month day;
proc sort data=bdates2;
by month day;
data match;
retain name;
/*To have name as first variable in output dataset.*/
merge bdates2 edates2;
by month day;
if name^='' and event^='';
/* Keep only matches where edates and bdates are non-missing. */
*drop month day;
run;
proc print data=match; run;
/**************************************************/
/* Alternate solution in proc sql ...less typing. */
/**************************************************/
title1 ' ';
title2 'Answer 1 - People having a birthdate that matches an event date - The
Interstate (proc sql)';
/* Use SQL to "merge" the datasets "by" month/day in where clause (without
sorting!), and output only matches. */
proc sql;
create table match_sql as
select name, bdate, edate, event
from bdates, edates
where month(bdate) = month(edate) and day(bdate) = day(edate);
/* Only keep matches of month and day from cartesian product. */
quit;
proc print data=match_sql; run;
*********************************************************************************;
/* Question 2.
*/
12
/* Find, for each person, the closest date (year/month/day) in history.
*/
/* This closest date might be earlier or later in time than the birthdate.
*/
/* This 11-line solution (15 with comments) is simple and runs fast.
*/
/* NOTE: PROCEDURE SQL used (Total process time):
*/
/*
real time
0.01 seconds
*/
/*
cpu time
0.02 seconds
*/
*********************************************************************************;
title1 ' ';
title2 'Answer 2 - Closest event date to each birthdate - The Cartesian Product is
so useful!';
proc sql;
create table closest_date as
select name, bdate, edate,
/* Calculate absolute difference between EACH bdate and EACH edate (because
sql first creates cartesian product), and store in var date_diff. */
/* SAS has useful interval functions to calculate differences between sas
dates (http://www2.sas.com/proceedings/forum2007/226-2007.pdf) */
abs(datdif(bdate,edate,'act/act')) as date_diff,
/* Calculate minimum date_diff, by person name, and remerge to table. */
/* This is akin to proc sort + proc means or summary + data step merge...but
all in one step! */
min(calculated date_diff) as min_date_diff,
event
from bdates, edates
group by name
/* BY for min calc.
*/
having date_diff = min_date_diff;
/* Only keep matches of date_diff and
min_date_diff from cartesian product = closest event date per person. */
quit;
proc print data=closest_date; run;
13
Solution 3. Dale Starr (BC Ministry of Health)
/*
*------------------------------------------------------------------------*
*
*
* SUAVe (SAS Users Association of Victoria, eh?) meeting May 29, 2012.
*
* Open Problems, first problem
*
*
*
*------------------------------------------------------------------------*
*/
OPTIONS LS=100 PS=2100
nodate pageno=1;
***************************;
*0. Read In Data Sets;
***************************;
DATA event_data;
FORMAT date yymmdd10.;
INPUT @1 date yymmdd8.
@10 desc $70.;
CARDS4;
19990104 Euro debuts as a financial unit in corporate and investment markets
19680105 Prague Spring begins in Czechoslovakia
19160108 Allies retreat from Gallipoli
19200110 League of Nations formally comes into being
19700115 Gadafi is proclaimed premier of Libya
19910116 The Persian Gulf War begins
19660119 Indira Gandhi becomes Prime Minister of India
19610120 John F. Kennedy inaugurated
19010122 Queen Victoria dies
19240125 First Winter Olympic Games
19260127 First public demonstration of a television system in London, UK
19860128 Space shuttle "Challenger" explodes during lift-off
19790201 Ayatollah Homeini returns to Iran
19430202 Battle of Stalingrad ends
19920207 European Union established
19450211 Yalta conference ends
19590216 Fidel Castro is sworn in as prime minister of Cuba
19810223 Spanish rebels storm Parliament
19350226 Hitler organizes Luftwaffe
19720302 Pioneer 10 launched to Jupiter
19170308 February Revolution in Russia begins
19300312 Gandhi leads civil disobedience
19950320 Nerve gas attach on Tokyo subway
19190323 Mussolini founds the Fascist party
19570325 European Economic Community established
19790326 Israel-Egyptian peace agreement signed
19390328 Spanish Civil War ends
19730329 United States withdraws from Vietnam
19820402 Argentina invades Falklands Islands
19680404 Marthin Luther King assassinated
19630407 A new constitution Tito the president for life of Yugoslavia
19610412 Soviet cosmonaut Yuri Gagarin becomes the first man in space
19120415 British ocean liner Titanic sinks 400 miles south of Newfoundland, Cana
19170416 Lenin returns to Russia to take the reins of Russian Revolution
14
19860426
19690428
19790504
19150507
19450508
19780509
19940510
19810513
19480514
19680516
19270521
19450523
19530602
19190604
19670605
19390607
19370611
19830613
19630616
19560623
19450626
19140628
19970701
19940708
19850713
19180716
19360718
19690720
19520723
19430725
19140728
19140801
19450806
19740808
19450809
19610815
19770816
19680820
19390823
19440825
19490829
19970831
19390901
19450902
19720906
20010911
19930913
19780917
19550919
19800922
19960927
19390929
19361001
19571004
19891005
19491007
19901015
Nuclear disaster at Chernobyl, Soviet Union (today Ukraine)
De Gaulle resigns as leader of France
Margaret Thatcher sworn in as UK's prime minister
British ocean liner Lusitania is torpedoed by a German submarine and si
German forces agree to an unconditional surrender ending World War II
Former Italian prime minister Aldo Moro found dead
Nelson Mandela is sworn in as the first black president of South Africa
Pope John Paul II shot in Rome's St. Peter's Square
State of Israel proclaimed
Massive protests begin in France
American pilot Charles A. Lindbergh lands at Le Bourget Field in Paris
Heinrich Himmler, the head of the Gestapo, commits suicide while in All
Queen Elizabeth II is formally crowned monarch of the United Kingdom
US Congress approved the 19th Amendment, which would guarantee suffrage
Six-Day War begins: The Israel launches simultaneous attacks on the ai
King George VI becomes the first British monarch to visit the United St
Soviet Union executes eight army leaders during "Great Purge"
Pioneer 10, the world's first outer-planetary probe, leaves the solar s
Soviet Cosmonaut Valentina Tereshkova becomes the first woman to travel
Gamal Abdel Nasser elected first president of the Republic of Egypt
Delegates from 50 nations sign the United Nations Charter
Archduke Franz Ferdinand, assassinated in Sarajevo, Bosnia
Hong Kong returned to China
Kim Il Sung, the communist dictator of North Korea, dies of a heart att
Live Aid concert at London and Philadelphia
In Yekaterinburg, Russia, Czar Nicholas II and his family are executed
Spanish Civil War breaks out
American astronaut Neil Armstrong walks on Moon
Military seizes power in Egypt
Benito Mussolini, fascist dictator of Italy, is voted out of power
Austria-Hungary declares war on Serbia, starting World War I
Germany and Russia declare war against each other
Atomic bomb is dropped on the city of Hiroshima in Japan killing 80,000
US President Richard M. Nixon resigns
An atomic bomb is detonated over the city of Nagasaki
East German authorities begin building the Berlin Wall
Elvis Presley dies
Soviets invade Czechoslovakia, crushing the "Prague Spring"
Molotov-Ribbentrop Pact signed
Paris is liberated after more than four years of Nazi occupation
Soviets explode atomic bomb at a remote test site at Semipalatinsk in K
Princess Diana killed in a car accident
Germany invades Poland, starting World War II
Japan formally surrenders to the Allies, ending World War II
Israeli hostages killed in Munich
Terrorist Attack on WTC in New York City and The Pentagon in Arlington,
Israel and Palestine sign a peace accord
Camp David Accords signed
Argentine President Juan Domingo Peron is deposed in a military coup
Iraq invades Iran starting 8-year war
In Afghanistan, the Taliban capture capital city Kabul
Germany and the Soviet Union agree to divide control of occupied Poland
General Franco proclaimed leader of the rebel Nationalist government in
Soviet Union launches Sputnik, the world's first artificial satellite
Dalai Lama wins Nobel Peace Prize
East Germany created within Soviet occupation zone
Soviet leader Mikhail Gorbachev wins the Nobel Peace Prize
15
19591021
19621022
19831023
19291029
19841031
19931101
19171106
19181111
19901113
19571115
19451120
19631122
19571127
19471129
19671203
19211206
19411207
19941211
19011212
19031217
19841219
19911221
19401229
19221230
;;;;
RUN;
Rocket designer Wernher von Braun moved from the US Army to the newly c
Cuban missile crises begin
Beirut barracks blown up
Black Tuesday: Stock market crashes leading to Great Depression
Indira Gandhi, the prime minister of India, is assassinated in New Delh
The Maastricht Treaty comes into effect, formally establishing the Euro
Russian Bolshevik Party launch a coup against Russia's Provisional Gove
Germany signed an armistice agreement with the Allies, ending World War
The first known World Wide Web page is written
Nikita Khrushchev challenges United States to a missile "shooting match
Nuremberg trials begin
US president, John F. Kennedy is assassinated in Dallas, Texas
Indian Prime Minister Nehru makes an impassioned speech for nuclear dis
U.N. votes for partition of Palestine despite strong Arab opposition
First human heart transplantation
The Irish Free State is declared
Japanese warplanes bombed US Pacific fleet at Pearl Harbor
Russian President Boris Yeltsin orders Russian forces into Chechnya
Marconi sends first radio transmission across the Atlantic Ocean
Wright brothers make the first successful flight in history of airplane
Agreement committing Britain to return Hong Kong to China in 1997 signe
Soviet republics proclaim the Commonwealth of Independent States, which
Worst air raid on London
Soviet Union established
DATA birth_data;
FORMAT date yymmdd10.;
INPUT @1 date yymmdd8.
@10 desc $7.;
CARDS4;
19450223 John
19620809 Susan
19880204 Michael
20020716 Elsie
19770303 Sylvia
;;;;
RUN;
***************************;
*1. Use PROC SQL to join day and month from event data
to day and month from birth data.
***************************;
PROC SQL;
CREATE TABLE Match_by_Mnth_day AS
SELECT a.date
AS birth_date,
a.desc
AS person_name,
CASE WHEN (b.date NE .) THEN
LEFT(PUT(MONTH(b.date), z2.) || '-' || PUT(DAY(b.date), z2.))
ELSE 'No Match'
END AS matching_month_day,
b.date
AS event_date,
b.desc
AS event_description
FROM birth_data a LEFT JOIN event_data b
ON MONTH(a.date) = MONTH(b.date) AND DAY(a.date) = DAY(b.date)
/*
16
WHERE (b.date NE .)
*/
ORDER BY a.date;
QUIT;
/*
*------------------------------------------------------------------------*
*
*
* SUAVe (SAS Users Association of Victoria, eh?) meeting May 29, 2012.
*
* Open Problem, problem 2
*
* Three different solutions, Cartesian Join, Interleave, Pointer.
*
*
*
*------------------------------------------------------------------------*
*/
********************* Brute Force Solution *********************;
* Use Cartesian Join to Compare all possible Event Dates
*
* to each of the Birth Dates.
*
****************************************************************;
***************************;
*1. Make a file of all possible combinations of event and person
(ignore note in log about Cartesian product);
***************************;
PROC SQL;
CREATE TABLE Cartesian_Final AS
SELECT b.desc AS person_name,
b.date AS birth_date,
a.date AS event_date,
a.desc AS event_description,
ABS(a.date - b.date) AS date_diff
FROM event_data a, birth_data b
GROUP BY b.desc
HAVING ABS(a.date - b.date) = MIN(ABS(a.date - b.date))
ORDER BY b.date;
QUIT;
***************** End of Brute Force Solution ******************;
********************* Up and Down Solution *********************;
* Use two interleave merges, Ascending and Descending, to get *
* closest events, before and after birthdate.
*
****************************************************************;
***************************;
*2. Sort data sets by date for interleave merge ascending.;
***************************;
PROC SORT DATA = event_data OUT = Asc_event_data;
BY date;
RUN;
PROC SORT DATA = birth_data OUT = Asc_birth_data;
BY date;
RUN;
***************************;
*3. Interleave merge ascending.
17
Matches the event record preceeding the birth date
record to the birth date record;
***************************;
DATA Interleave_asc;
RETAIN event_date event_desc;
LENGTH desc $70.;
SET Asc_birth_data (in = b) Asc_event_data (in = e);
BY date;
FORMAT event_date yymmdd10.;
IF _n_ = 1 THEN DO;
event_date = .;
END;
IF (e) THEN DO;
event_date
= date;
event_desc
= desc;
days_bet_bday_evnt = .;
END;
IF (b) THEN DO;
IF (event_date = .) THEN days_bet_bday_evnt = .;
ELSE DO;
days_bet_bday_evnt = ABS(event_date - date);
END;
IF event_date NE . THEN OUTPUT;
END;
RUN;
***************************;
*4. Sort data sets by date for interleave merge descending.;
***************************;
PROC SORT DATA = event_data OUT = desc_event_data;
BY DESCENDING date;
RUN;
PROC SORT DATA = birth_data OUT = desc_birth_data;
BY DESCENDING date;
RUN;
***************************;
*5. Interleave merge descending.
Matches the event record following the birth date
record to the birth date record.;
***************************;
DATA Interleave_desc;
RETAIN event_date event_desc;
LENGTH desc $70.;
SET desc_birth_data (in = b) desc_event_data (in = e);
BY DESCENDING date;
FORMAT event_date yymmdd10.;
FORMAT event_desc $70.;
18
IF _n_ = 1 THEN DO;
event_date = .;
END;
IF (e) THEN DO;
event_date
= date;
event_desc
= desc;
days_bet_bday_evnt = .;
END;
IF (b) THEN DO;
IF (event_date = .) THEN days_bet_bday_evnt = .;
ELSE DO;
days_bet_bday_evnt = ABS(event_date - date);
END;
IF event_date NE . THEN OUTPUT;
END;
RUN;
***************************;
*6. Merge the ascending and descending interleave merge data sets
into one file, and then select the smallest interval between
event date and birth date by person.
***************************;
PROC SQL;
CREATE TABLE Interleave_Final AS
SELECT TRIM(desc) AS Person_Name FORMAT=$10.,
date
AS Birth_Date FORMAT=yymmdd10.,
Event_Date,
Event_Desc,
Days_Bet_bday_evnt
FROM
(
SELECT *
FROM Interleave_asc
UNION
SELECT *
FROM Interleave_desc
)
GROUP BY Person_Name
HAVING Days_Bet_bday_evnt = MIN(Days_Bet_bday_evnt)
ORDER BY Birth_date;
QUIT;
********************* End of Interleave Solution *********************;
********************* Using POINT function Solution *********************;
* Use the POINT operator to identify event previous to birthday and
*
* event following birthday.
*
*************************************************************************;
***************************;
*7. Using POINT operator find previous and next record.
***************************;
DATA Point_01;
19
/* This retain statement is only used to change the order of the columns in the
output file. */
RETAIN Person_name brth_date closest_date closest_desc days_difference
prev_evnt_date prev_evnt_desc next_evnt_date next_evnt_desc;
FORMAT
FORMAT
FORMAT
FORMAT
next_evnt_date yymmdd10.;
prev_evnt_date yymmdd10.;
Closest_date yymmdd10.;
Closest_desc $70.;
/* Initialize variables before starting */
e_next= 1;
SET Asc_event_data POINT=e_next NOBS = e_n;
next_evnt_date = date;
next_evnt_desc = desc;
DO WHILE (NOT b_end); /* Start DO WHILE 01 */
SET Asc_birth_data END = b_end;
DROP date desc;
FORMAT brth_date yymmdd10.;
FORMAT Person_Name $10.;
Brth_date
= date;
Person_Name = desc;
DO WHILE ((next_evnt_date <= brth_date) AND (next_evnt_date ne .));
/* Start DO WHILE 02 */
e_next = e_next+1;
prev_evnt_date = next_evnt_date;
prev_evnt_desc = next_evnt_desc;
IF e_next <= e_n THEN
DO; /* Start IF THEN DO 01 */
SET Asc_event_data POINT=e_next nobs = e_n;
next_evnt_date = date;
next_evnt_desc = desc;
END; /* End IF THEN DO 01 */
ELSE
DO;
next_evnt_date = .;
next_evnt_desc = .;
END; /* End ELSE DO 01 */
END; /* End DO WHILE 02 */
/* Pick date closest to birthday from previous date and next date */
IF ( (next_evnt_date = .) OR
( (next_evnt_date NE .) AND (prev_evnt_date NE .) )
AND
((ABS(prev_evnt_date - brth_date) < ABS(next_evnt_date - brth_date))
)
)
THEN DO;
Closest_date = prev_evnt_date;
20
Closest_desc = prev_evnt_desc;
days_difference = ABS(prev_evnt_date - brth_date);
END;
ELSE IF ( (prev_evnt_date = .) OR
( (prev_evnt_date NE .) AND (next_evnt_date NE .) )
AND
((ABS(next_evnt_date - brth_date) < ABS(prev_evnt_date brth_date))
)
)
THEN DO;
Closest_date = next_evnt_date;
Closest_desc = next_evnt_desc;
days_difference = ABS(next_evnt_date - brth_date);
END;
ELSE
DO;
Closest_date = .;
Closest_desc = .;
days_difference = .;
END;
/* End Pick date closest to birthday from previous date and next date */
OUTPUT;
END; /* End DO WHILE 01 */
STOP;
RUN; /* End DATA Point_03; */
/* Reduce columns in output file, remove previous and next data.*/
DATA Point_Final;
KEEP Person_Name brth_date closest_date closest_desc days_difference;
SET Point_01;
RUN;
********************* End of POINT function Solution *********************;
21
Solution 4. Mike Atkinson (Acko Systems Consulting, Inc.)
* Solution to second part of SUAVe Open Problem - May 29, 2012;
* M. Atkinson;
* Read event dates;
data event_dates;
infile cards4;
input @01 event_date yymmdd8.
@10 event
$71.;
format event_date yymmdd10.;
cards4;
19990104 Euro debuts as a financial unit in corporate and investment markets
19680105 Prague Spring begins in Czechoslovakia
19160108 Allies retreat from Gallipoli
19200110 League of Nations formally comes into being
19700115 Gadafi is proclaimed premier of Libya
19910116 The Persian Gulf War begins
19660119 Indira Gandhi becomes Prime Minister of India
19610120 John F. Kennedy inaugurated
19010122 Queen Victoria dies
19240125 First Winter Olympic Games
19260127 First public demonstration of a television system in London, UK
19860128 Space shuttle "Challenger" explodes during lift-off
19790201 Ayatollah Homeini returns to Iran
19430202 Battle of Stalingrad ends
19920207 European Union established
19450211 Yalta conference ends
19590216 Fidel Castro is sworn in as prime minister of Cuba
19810223 Spanish rebels storm Parliament
19350226 Hitler organizes Luftwaffe
19720302 Pioneer 10 launched to Jupiter
19170308 February Revolution in Russia begins
19300312 Gandhi leads civil disobedience
19950320 Nerve gas attach on Tokyo subway
19190323 Mussolini founds the Fascist party
19570325 European Economic Community established
19790326 Israel-Egyptian peace agreement signed
19390328 Spanish Civil War ends
19730329 United States withdraws from Vietnam
19820402 Argentina invades Falklands Islands
19680404 Marthin Luther King assassinated
19630407 A new constitution Tito the president for life of Yugoslavia
19610412 Soviet cosmonaut Yuri Gagarin becomes the first man in space
19120415 British ocean liner Titanic sinks 400 miles south of Newfoundland, Cana
19170416 Lenin returns to Russia to take the reins of Russian Revolution
19860426 Nuclear disaster at Chernobyl, Soviet Union (today Ukraine)
19690428 De Gaulle resigns as leader of France
19790504 Margaret Thatcher sworn in as UK's prime minister
19150507 British ocean liner Lusitania is torpedoed by a German submarine and si
19450508 German forces agree to an unconditional surrender ending World War II
19780509 Former Italian prime minister Aldo Moro found dead
19940510 Nelson Mandela is sworn in as the first black president of South Africa
22
19810513
19480514
19680516
19270521
19450523
19530602
19190604
19670605
19390607
19370611
19830613
19630616
19560623
19450626
19140628
19970701
19940708
19850713
19180716
19360718
19690720
19520723
19430725
19140728
19140801
19450806
19740808
19450809
19610815
19770816
19680820
19390823
19440825
19490829
19970831
19390901
19450902
19720906
20010911
19930913
19780917
19550919
19800922
19960927
19390929
19361001
19571004
19891005
19491007
19901015
19591021
19621022
19831023
19291029
19841031
19931101
19171106
Pope John Paul II shot in Rome's St. Peter's Square
State of Israel proclaimed
Massive protests begin in France
American pilot Charles A. Lindbergh lands at Le Bourget Field in Paris
Heinrich Himmler, the head of the Gestapo, commits suicide while in All
Queen Elizabeth II is formally crowned monarch of the United Kingdom
US Congress approved the 19th Amendment, which would guarantee suffrage
Six-Day War begins: The Israel launches simultaneous attacks on the ai
King George VI becomes the first British monarch to visit the United St
Soviet Union executes eight army leaders during "Great Purge"
Pioneer 10, the world's first outer-planetary probe, leaves the solar s
Soviet Cosmonaut Valentina Tereshkova becomes the first woman to travel
Gamal Abdel Nasser elected first president of the Republic of Egypt
Delegates from 50 nations sign the United Nations Charter
Archduke Franz Ferdinand, assassinated in Sarajevo, Bosnia
Hong Kong returned to China
Kim Il Sung, the communist dictator of North Korea, dies of a heart att
Live Aid concert at London and Philadelphia
In Yekaterinburg, Russia, Czar Nicholas II and his family are executed
Spanish Civil War breaks out
American astronaut Neil Armstrong walks on Moon
Military seizes power in Egypt
Benito Mussolini, fascist dictator of Italy, is voted out of power
Austria-Hungary declares war on Serbia, starting World War I
Germany and Russia declare war against each other
Atomic bomb is dropped on the city of Hiroshima in Japan killing 80,000
US President Richard M. Nixon resigns
An atomic bomb is detonated over the city of Nagasaki
East German authorities begin building the Berlin Wall
Elvis Presley dies
Soviets invade Czechoslovakia, crushing the "Prague Spring"
Molotov-Ribbentrop Pact signed
Paris is liberated after more than four years of Nazi occupation
Soviets explode atomic bomb at a remote test site at Semipalatinsk in K
Princess Diana killed in a car accident
Germany invades Poland, starting World War II
Japan formally surrenders to the Allies, ending World War II
Israeli hostages killed in Munich
Terrorist Attack on WTC in New York City and The Pentagon in Arlington,
Israel and Palestine sign a peace accord
Camp David Accords signed
Argentine President Juan Domingo Peron is deposed in a military coup
Iraq invades Iran starting 8-year war
In Afghanistan, the Taliban capture capital city Kabul
Germany and the Soviet Union agree to divide control of occupied Poland
General Franco proclaimed leader of the rebel Nationalist government in
Soviet Union launches Sputnik, the world's first artificial satellite
Dalai Lama wins Nobel Peace Prize
East Germany created within Soviet occupation zone
Soviet leader Mikhail Gorbachev wins the Nobel Peace Prize
Rocket designer Wernher von Braun moved from the US Army to the newly c
Cuban missile crises begin
Beirut barracks blown up
Black Tuesday: Stock market crashes leading to Great Depression
Indira Gandhi, the prime minister of India, is assassinated in New Delh
The Maastricht Treaty comes into effect, formally establishing the Euro
Russian Bolshevik Party launch a coup against Russia's Provisional Gove
23
19181111
19901113
19571115
19451120
19631122
19571127
19471129
19671203
19211206
19411207
19941211
19011212
19031217
19841219
19911221
19401229
19221230
;;;;
run;
Germany signed an armistice agreement with the Allies, ending World War
The first known World Wide Web page is written
Nikita Khrushchev challenges United States to a missile "shooting match
Nuremberg trials begin
US president, John F. Kennedy is assassinated in Dallas, Texas
Indian Prime Minister Nehru makes an impassioned speech for nuclear dis
U.N. votes for partition of Palestine despite strong Arab opposition
First human heart transplantation
The Irish Free State is declared
Japanese warplanes bombed US Pacific fleet at Pearl Harbor
Russian President Boris Yeltsin orders Russian forces into Chechnya
Marconi sends first radio transmission across the Atlantic Ocean
Wright brothers make the first successful flight in history of airplane
Agreement committing Britain to return Hong Kong to China in 1997 signe
Soviet republics proclaim the Commonwealth of Independent States, which
Worst air raid on London
Soviet Union established
* Read birth dates;
* (some extras are thrown in);
data birth_dates;
infile cards4;
input @01 birth_date yymmdd8.
@10 name
$10.;
format birth_date yymmdd10.;
cards4;
19450223 John
19620809 Susan
19880204 Michael
20020716 Elsie
19770303 Sylvia
17770717 Early 1
18880818 Early 2
20120212 Later
19690720 Luna
;;;;
run;
* Sort both events and birth dates into ascending order;
proc sort data=event_dates;
by event_date;
run;
proc sort data=birth_dates;
by birth_date;
run;
* Make a version of events with next event date on each observation;
* in addition to the current date. First obs only has next event;
24
data event_dates_2;
retain event_date event;
format event_date yymmdd10.;
set event_dates (rename=(event_date=next_event_date
event=next_event));
output;
event_date = next_event_date;
event
= next_event;
run;
* Set date constant for use with coalesce;
%let date1 = %sysfunc(inputn(15820101, yymmdd8.));
%put &date1;
* earliest SAS date value;
* Use a data step with interleave SET statement to find the closest date;
* Retain is used to keep event dates (and descriptions) until needed;
data bday_and_closest_event;
keep name birth_date match_event match_event_date;
retain r_event_date r_next_event_date r_event r_next_event;
set event_dates_2 (in=e rename=(event_date=birth_date))
birth_dates
(in=b);
by birth_date;
if (e) then do;
r_event_date
r_event
r_next_event_date
r_next_event
end;
=
=
=
=
birth_date;
event;
next_event_date;
next_event;
if (b) then do;
if ((birth_date - coalesce(r_event_date, &date1)) <
(r_next_event_date - birth_date)) then do;
match_event
= r_event;
match_event_date = r_event_date;
end;
else do;
match_event
= r_next_event;
match_event_date = r_next_event_date;
end;
format match_event_date yymmdd10.;
output;
end;
run;
* Slightly more efficient version starts here;
* Make a version of events with next event date on each observation;
25
* in addition to the current date.
First obs only has next event;
data event_dates_3;
retain event_date;
format event_date yymmdd10.;
set event_dates (rename=(event_date=next_event_date
event=next_event));
output;
event_date = next_event_date;
run;
* Set date constant for use with coalesce;
%let date1 = %sysfunc(inputn(15820101, yymmdd8.));
%put &date1;
* earliest SAS date value;
* Use a data step with interleave SET statement to find the closest date;
* Retain is used to keep event dates (and descriptions) until needed;
data bday_and_closest_event2;
keep name birth_date match_event match_event_date;
retain r_event_date r_next_event_date r_event r_next_event;
length r_event $ 71;
set event_dates_3 (in=e rename=(event_date=birth_date))
birth_dates
(in=b);
by birth_date;
if (e) then do;
r_event_date
r_next_event_date
* Put the previous next
r_event
r_next_event
end;
= birth_date;
= next_event_date;
event into event (before writing in new next event)!;
= r_next_event;
= next_event;
if (b) then do;
if ((birth_date - coalesce(r_event_date, &date1)) <
(r_next_event_date - birth_date)) then do;
match_event
= r_event;
match_event_date = r_event_date;
end;
else do;
match_event
= r_next_event;
match_event_date = r_next_event_date;
end;
format match_event_date yymmdd10.;
output;
end;
run;
26
Solution 5. Arthur Tabachnek (myQNA, Inc ).
Please see separate powerpoint slide deck entitled: ‘SUAVEe Open Problem May 29th, 2012’