BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zana
Calcite | Level 5

ِDear all,

I am struggling to find a way to convert date in a file data. Does anyone have a code that converts a Islamic date to Gregorian???

Any help is greatly appreciated. Smiley Happy

Zana

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

: I just realized that one reason you may not have been able to run the code I originally proposed is that the tab characters weren't copied correctly. Try the following version that uses a comma delimited file:

data ctrl (keep=fmtname type start label);

  informat gdates $31.;

  infile cards dlm=',';

  input _pyear $ gdates &;

  retain fmtname 'pdate' type 'j';

  leap=index(_pyear,'*');

  pyear=input(compress(_pyear,'*'),12.);

  bday=input(scan(gdates,1,'.'),12.);

  byear=input(scan(gdates,3),12.);

  bmonth=3;

  if _n_ eq 1 then label=mdy(bmonth,bday,byear)-1;

  do pmonth=1 to 12;

    if pmonth le 6 then eday=31;

    else if pmonth le 11 then eday=30;

    else if leap then eday=30;

    else eday=29;

    do pday=1 to eday;

      label+1;

      start=catx('-',pyear,put(pmonth,z2.),put(pday,z2.));

      output;

    end;

  end;

  cards;

1354*,21. March 1975 – 20. March 1976

1355,21. March 1976 – 20. March 1977

1356,21. March 1977 – 20. March 1978

1357,21. March 1978 – 20. March 1979

1358*,21. March 1979 – 20. March 1980

1359,21. March 1980 – 20. March 1981

1360,21. March 1981 – 20. March 1982

1361,21. March 1982 – 20. March 1983

1362*,21. March 1983 – 20. March 1984

1363,21. March 1984 – 20. March 1985

1364,21. March 1985 – 20. March 1986

1365,21. March 1986 – 20. March 1987

1366*,21. March 1987 – 20. March 1988

1367,21. March 1988 – 20. March 1989

1368,21. March 1989 – 20. March 1990

1369,21. March 1990 – 20. March 1991

1370*,21. March 1991 – 20. March 1992

1371,21. March 1992 – 20. March 1993

1372,21. March 1993 – 20. March 1994

1373,21. March 1994 – 20. March 1995

1374,21. March 1995 – 19. March 1996

1375*,20. March 1996 – 20. March 1997

1376,21. March 1997 – 20. March 1998

1377,21. March 1998 – 20. March 1999

1378,21. March 1999 – 19. March 2000

1379*,20. March 2000 – 20. March 2001

1380,21. March 2001 – 20. March 2002

1381,21. March 2002 – 20. March 2003

1382,21. March 2003 – 19. March 2004

1383*,20. March 2004 – 20. March 2005

1384,21. March 2005 – 20. March 2006

1385,21. March 2006 – 20. March 2007

1386,21. March 2007 – 19. March 2008

1387*,20. March 2008 – 20. March 2009

1388,21. March 2009 – 20. March 2010

1389,21. March 2010 – 20. March 2011

1390,21. March 2011 – 19. March 2012

1391*,20. March 2012 – 20. March 2013

1392,21. March 2013 – 20. March 2014

1393,21. March 2014 – 20. March 2015

1394,21. March 2015 – 19. March 2016

1395*,20. March 2016 – 20. March 2017

1396,21. March 2017 – 20. March 2018

1397,21. March 2018 – 20. March 2019

1398,21. March 2019 – 19. March 2020

1399*,20. March 2020 – 20. March 2021

1400,21. March 2021 – 20. March 2022

1401,21. March 2022 – 20. March 2023

1402,21. March 2023 – 19. March 2024

1403*,20. March 2024 – 20. March 2025

1404,21. March 2025 – 20. March 2026

1405,21. March 2026 – 20. March 2027

1406,21. March 2027 – 19. March 2028

1407,20. March 2028 – 19. March 2029

1408*,20. March 2029 – 20. March 2030

1409,21. March 2030 – 20. March 2031

1410,21. March 2031 – 19. March 2032

1411,20. March 2032 – 19. March 2033

1412*,20. March 2033 – 20. March 2034

1413,21. March 2034 – 20. March 2035

1414,21. March 2035 – 19. March 2036

1415,20. March 2036 – 19. March 2037

1416*,20. March 2037 – 20. March 2038

1417,21. March 2038 – 20. March 2039

1418,21. March 2039 – 19. March 2040

1419,20. March 2040 – 19. March 2041

;

proc format library=work cntlin=ctrl;

run;

data want;

  informat persian_date1 persian_date2 $10.;

  input persian_date1 persian_date2;

  format sd1 sd2 date9.;

  sd1=input(persian_date1,$pdate10.);

  sd2=input(persian_date2,$pdate10.);

  days_between=sd2-sd1;

  cards;

1392-10-11 1393-01-01

1393-02-31 1393-06-30

1393-08-02 1395-03-02

1391-10-09 1391-10-12

;

View solution in original post

42 REPLIES 42
stefan_r
Calcite | Level 5

I am trying to do the same and found a custom function ISLAMIC_TO_SD here:
http://www.sascommunity.org/wiki/Sometimes_One_Needs_an_Option_with_Unusual_Dates
(Click here to download the SAS code for creating Hebrew and Islamic calendar interval datasets)

However, I tested it with today's date (2014-10-23) and the Islamic date is off by one day (1435-12-28 instead of 1435-12-29).

I wonder if the author of that function could help...

zana
Calcite | Level 5

Thank you so much.

But i can't run each of them. Please say me how can i run these cod for convert an Islamic (e.g., 1380-10-28) to gregorian date!

Best regards

Zana

OS2Rules
Obsidian | Level 7

Hi:

I noticed that the paper referred to was from Art - perhaps he can help you?

art297
Opal | Level 21

wrote that part of the code and I've sent him a note referring to this thread.

Ksharp
Super User

Don't forget me too. I don't know how Islamic date look like. But When Art and me changed traditional Chinese date into gregorian date , we need a data contains all these traditional Chinese date which we download it from a HongKong observatory website by filename + url . Maybe you do all these followed Art and me .

Good Luck.

Xia Keshan

zana
Calcite | Level 5

Iranian date is an Islamic. I need a program for calculating a duration for Iranian date.

When I tried by SAS to find that how many days there are between today (1393/07/30) and 5 months ago (1393/02/31), found that i'm powerless. I think that by convert date to gregorian this problem maybe solved.

Do you have any idea for me?

Thanks

Reeza
Super User

I'm not sure how your Iranian calendar is formatted/structured but you could also try subtracting them directly to see if that duration would be correct directly.

Otherwise try the steps below:

1. Extract your Iranian dates into their components - Year, Month, Date - making sure they are all numeric variables.

2. Run the attached code in the .txt file in SAS

3. Convert your dates to Gregorian using the islamic_to_sd(year,month,day) function as in my sample code above

4. Subtract your dates to get the duration.

zana
Calcite | Level 5

Dear Reeza,

Thanks all for your time and post. My apologies, I'm a novice in SAS. I try to run the following code (in SAS 9.1.3), but did not work.

proc fcmp outlib=work.func.dates;

function islamic_leap(year);

    return(mod((year*11)+14,30)<11);

  endsub;

  function islamic_to_jd(year,month,day);

    return((day+ceil(29.5*(month-1))+(year-1)*354+

     floor((3+(11*year))/30)+1948439.5)-1);

  endsub;

data test;

    date_want=islamic_to_sd(1435,12,28);

    format date_want date9.;

run;

ERROR 68-185: The function ISLAMIC_TO_SD is unknown, or cannot be accessed.

Reeza
Super User

What do you mean you can't run it?

Run up to the proc format, not the macro sendit portion and then use the

islamic_to_sd function to convert the dates:

data test;

    date_want=islamic_to_sd(1435,12,28);

    format date_want date9.;

run;

Reeza
Super User

Oh, yeah proc fcmp isn't until 9.2 at least.

Convert to SAS date based on the following then, assuming you do the conversion to day, month, year as indicated:

You can still take the difference of the dates to get duration.

data test;

year=1435;

day=28;

month=12;

sas_date=((day+ceil(29.5*(month-1))+(year-1)*354+

     floor((3+(11*year))/30)+1948439.5)-1)-2436934.5;

format sas_date date9.;

run;

zana
Calcite | Level 5

Thank you so much. Please let me i add more suitable information. For example (please note) in Iranian date today is 1393-08-02 (year, month, day) that it is equal October 24, 2014.

So, my expected code is:

Iranian_Date     Gregorian_Date

1392-10-11        01JAN2014

1393-01-01        21MAR2014

1393-02-31        21MAY2014

1393-06-30        21SEP2014

1393-08-02        24OCT2014

Zana

Reeza
Super User

@jaap is partially correct it will be hard to make an exact conversion based on a formula.  Do you have a specific date range in your data? Then you could download an online calendar and do a lookup conversion.

Also, given your dates how are you expecting to calculate duration, second row minus first row?

zana
Calcite | Level 5

Dear Reeza, I have a data set with more than 10,000 records from 1365-01-01 to 1393-07-02 [28 years* 365 (days/year)].

I want to calculate that as second column minus first column.

Zana

Reeza
Super User

Is the 4/5th column of dates in wikipedia a correct mapping for your needs?

Iranian calendars - Wikipedia, the free encyclopedia

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 42 replies
  • 5004 views
  • 6 likes
  • 9 in conversation