DATA Step, Macro, Functions and more

Hebrew Date Conversion

Reply
Trusted Advisor
Posts: 1,300

Hebrew Date Conversion

Hi everyone,

I was reading Rick Wicklin's blog a few weeks ago 'The DO Loop' and he posted a short holiday entry for fun about creating ASCII pictures of common holiday emblems.  I replyed there with a quick version of a menorah for Hanukkah.  Someone later asked me humorously if I modify the code to light more candles for each day.  It got me thinking how I could automate it?  What I came up with is a package of functions for dealing with Hebrew dates.  Maybe someone else will find it useful.  I thought I would be able to just alter my locale options and use the NLS date formats, etc... however when I looked at the documentation it did not list Hebrew dates as a workable locale, I could be wrong, and I didn't test it.

Link to The DO Loop post:

http://blogs.sas.com/content/iml/2011/12/16/a-sas-christmas-tree/

Here is the SAS Documention I referenced:

http://support.sas.com/documentation/cdl/en/nlsref/61893/HTML/default/viewer.htm#a003090795.htm

Here is the code I came up with.  First are the functions I wrote which should have more universal application and then is a more complex version of what I originally posted on The DO Loop comments that will automatically light the correct number of candles based on the date.

http://support.sas.com/documentation/cdl/en/nlsref/61893/HTML/default/viewer.htm#a003090795.htm

proc fcmp outlib=work.func.hebdate;

*is given year a leap year?  returns boolean answer;

function hebrew_leap(year);

  return(mod(((year*7)+1),19)<7);

endsub;

*how many months are in given year? (12=normal, 13=leap);

function hebrew_year_months(year);

  return(ifn(hebrew_leap(year),13,12));

endsub;

*test for delay of start of new year and to avoid

  Sunday, Wednesday, and Friday as start of the new year;

function hebrew_delay_1(year);

  months=floor(((235*year)-234)/19);

  parts=12084+(13753*months);

  day=(months*29)+floor(parts/25920);

  if mod((3*(day+1)),7)<3 then day+1;

  return(day);

endsub;

*check for delay in start of new year due to length of adjacent years;

function hebrew_delay_2(year);

  last=hebrew_delay_1(year-1);

  present=hebrew_delay_1(year);

  next=hebrew_delay_1(year+1);

  a=ifn(next-present=356,2,ifn(present-last=382,1,0));

  return(a);

endsub;

*how many days in given year?;

function hebrew_year_days(year);

  a=hebrew_to_jd(year+1,7,1)-hebrew_to_jd(year,7,1);

  return(a);

endsub;

*how many days in given month? (29 or 30);

function hebrew_month_days(year,month);

  *months with fixed number of days;

  if month in (2,4,6,10,13) then return(29);

  *if not a leap year Adar has 29 days;

  else if month=12 and not(hebrew_leap(year)) then return(29);

  *if month is Heshvan then days in month depends on days in year;

  else if month=8 and not(mod(hebrew_year_days(year),10)=5) then return(29);

  *Kislev varies similar to Heshvan;

  else if month=9 and mod(hebrew_year_days(year),10)=3 then return(29);

  *otherwise there are 30 days;

  else return(30);

endsub;

*bring it all together;

function hebrew_to_jd(year,month,day);

  hebrew_epoch=347995.5;

  months=hebrew_year_months(year);

  jd=hebrew_epoch+hebrew_delay_1(year)+hebrew_delay_2(year)+day+1;

  if month<7 then do;

   do mon=7 to months;

    jd+hebrew_month_days(year,mon);

   end;

   do mon=1 to month-1;

    jd+hebrew_month_days(year,mon);

   end;

  end;

  else do mon=7 to month-1;

   jd+hebrew_month_days(year,mon);

  end;

  return(jd);

endsub;

*convert jd to sas date value, assuming default start date is used or this will not work;

function hebrew_to_sd(year,month,day);

  return(hebrew_to_jd(year,month,day)-2436934.5);

endsub;

quit;

options cmplib=work.func;

*%let dt="21DEC2011"d;

%let dt=%sysfunc(today());

data _null_;

n1=hebrew_to_sd(year(&dt)+3761,9,25)-1;

n=intck('day',n1,&dt)+1;

if 1<=n<=8 then put #1 @9 '(';

do i=1 to n*2 by 2;

  put #2 @i '(' @;

end;

put #2 @9 '|' @;

l=3;

do j=' ','_'; do k=1 to 17;

  c=ifc(not mod(k,2),j,'|');

  put #l @k c @;

end; l+1; end;

put #l @8 '_|_';

run;

PROC Star
Posts: 7,363

Hebrew Date Conversion

I ended up using your picture and just manually adding a " )"  each day before I sent the card.  However, you provide me with an opening to advertise one of the papers I will be presenting at the 2012 SGF, namely "Sometimes one needs an option with unusual dates".

It talks about the SAS option intervalds with which one can specify any calendar and set of intervals.  Your code could easily be adapted to create a hebrew calendar and then simply schedule emails to be sent, each year, on each of the eight days.

Trusted Advisor
Posts: 1,300

Hebrew Date Conversion

That is a good thought Art.  I will need to think more about it.

Ask a Question
Discussion stats
  • 2 replies
  • 175 views
  • 0 likes
  • 2 in conversation