## How to convert dates from Gregorian to Solar Hijri calendar

Solved
Regular Contributor
Posts: 166

# How to convert dates from Gregorian to Solar Hijri calendar

[ Edited ]

Hello everybody;

I want to convert the gregorian date to solar Hijri calendar (Iranian calendars - Wikipedia, the free encyclopedia). I found a program in previous posts which was written by @art297 and it is awesome to convert solar Hijri to Gregorian calendar as shown below:

``````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;``````

Here is a sample of my data:

``````data WORK.SAMPLEDATA_1;
infile datalines dsd truncover;
input TRD_EVENT_TM:\$24. TRD_STCK_CD:\$15. TRD_PR:32. TRD_TUROVR:13. TRD_EVENT_DT:DATE9.;
format TRD_TUROVR 13. TRD_EVENT_DT DATE9.;
label TRD_EVENT_TM="TRD_EVENT_TM" TRD_STCK_CD="TRD_STCK_CD" TRD_PR="TRD_PR" TRD_TUROVR="TRD_TUROVR";
datalines4;
12:19:35,IKCQ1,1,100,24MAR2008
12:28:01,ALBZ1,1537,10000,26MAR2008
13:13:44,ALBZ1,1567,10,12MAY2009
12:20:38,AZAB1,683,10000,14JUL2011
13:13:44,AZAB1,695,10,11OCT2013
09:14:57,BALI1,850,9260,23MAR2008
09:15:06,BALI1,850,2000,25APR2008
09:15:14,BALI1,850,10000,14JUN2009
09:15:24,BALI1,850,6000,18JUL2010
09:29:27,BALI1,850,10000,11SEP2011
12:28:00,BALI1,850,10000,20OCT2012
12:28:07,BALI1,850,10000,24NOV2012
13:13:44,BALI1,865,10,26NOV2012
09:38:04,BANK1,1164,10729,13AUG2008
11:24:44,BANK1,1148,2000,15JAN2009
11:24:46,BANK1,1147,1575,19MAR2009
12:10:34,BANK1,1147,5000,17MAY2010
12:10:34,BANK1,1147,3425,22AUG2012
12:14:55,BANK1,1141,41575,24AUG2013
12:14:55,BANK1,1141,8425,24OCT2013
;;;;``````

Now, I need a program to reverse this procedure and convert Gregorian to Solar Hijri calendar.

Would anybody please tell me how can I do that?

Accepted Solutions
Solution
‎11-09-2017 05:59 PM
PROC Star
Posts: 2,318

## Re: How to convert dates from Gregorian to Solar Hijri calendar

This works:

``````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
run;
data CTRL2(drop=LABEL rename=(START=LABEL IN=START ));
set CTRL(drop=FMTNAME);
FMTNAME = 'greg2sh';
TYPE    = 'C';
IN      = put(LABEL,date9. -l);
run;
proc format cntlin=CTRL2;
run;
data WORK.SAMPLEDATA_1;
infile datalines dsd truncover;
input TRD_EVENT_TM:\$24. TRD_STCK_CD:\$15. TRD_PR:32. TRD_TUROVR:13. TRD_EVENT_DT:DATE9.;
format TRD_TUROVR 13. TRD_EVENT_DT DATE9.;
label TRD_EVENT_TM="TRD_EVENT_TM" TRD_STCK_CD="TRD_STCK_CD" TRD_PR="TRD_PR" TRD_TUROVR="TRD_TUROVR";
cards;
12:19:35,IKCQ1,1,100,24MAR2008
12:28:01,ALBZ1,1537,10000,26MAR2008
13:13:44,ALBZ1,1567,10,12MAY2009
12:20:38,AZAB1,683,10000,14JUL2011
13:13:44,AZAB1,695,10,11OCT2013
09:14:57,BALI1,850,9260,23MAR2008
09:15:06,BALI1,850,2000,25APR2008
09:15:14,BALI1,850,10000,14JUN2009
09:15:24,BALI1,850,6000,18JUL2010
09:29:27,BALI1,850,10000,11SEP2011
12:28:00,BALI1,850,10000,20OCT2012
12:28:07,BALI1,850,10000,24NOV2012
13:13:44,BALI1,865,10,26NOV2012
09:38:04,BANK1,1164,10729,13AUG2008
11:24:44,BANK1,1148,2000,15JAN2009
11:24:46,BANK1,1147,1575,19MAR2009
12:10:34,BANK1,1147,5000,17MAY2010
12:10:34,BANK1,1147,3425,22AUG2012
12:14:55,BANK1,1141,41575,24AUG2013
12:14:55,BANK1,1141,8425,24OCT2013
run;
data SAMPLEDATA_2;
set SAMPLEDATA_1;
SHDATE=put(vvalue(TRD_EVENT_DT),greg2sh.);
run;``````

TRD_EVENT_TM TRD_STCK_CD TRD_PR TRD_TUROVR TRD_EVENT_DT SHDATE
12:19:35 IKCQ1 1 100 24MAR2008 1387-01-05
12:28:01 ALBZ1 1537 10000 26MAR2008 1387-01-07
13:13:44 ALBZ1 1567 10 12MAY2009 1388-02-22
12:20:38 AZAB1 683 10000 14JUL2011 1390-04-23
13:13:44 AZAB1 695 10 11OCT2013 1392-07-19
09:14:57 BALI1 850 9260 23MAR2008 1387-01-04
09:15:06 BALI1 850 2000 25APR2008 1387-02-06
09:15:14 BALI1 850 10000 14JUN2009 1388-03-24
09:15:24 BALI1 850 6000 18JUL2010 1389-04-27
09:29:27 BALI1 850 10000 11SEP2011 1390-06-20
12:28:00 BALI1 850 10000 20OCT2012 1391-07-29
12:28:07 BALI1 850 10000 24NOV2012 1391-09-04
13:13:44 BALI1 865 10 26NOV2012 1391-09-06
09:38:04 BANK1 1164 10729 13AUG2008 1387-05-23
11:24:44 BANK1 1148 2000 15JAN2009 1387-10-26
11:24:46 BANK1 1147 1575 19MAR2009 1387-12-29
12:10:34 BANK1 1147 5000 17MAY2010 1389-02-27
12:10:34 BANK1 1147 3425 22AUG2012 1391-06-01
12:14:55 BANK1 1141 41575 24AUG2013 1392-06-02
12:14:55 BANK1 1141 8425 24OCT2013 1392-08-02

All Replies
PROC Star
Posts: 2,318

## Re: How to convert dates from Gregorian to Solar Hijri calendar

Reusing @art297 's table, this may do what you want:

``````
data CTRL2(drop=LABEL rename=(START=LABEL IN=START ));
set CTRL(drop=FMTNAME);
FMTNAME = 'greg2sh';
TYPE    = 'C';
IN      = vvalue(LABEL);
run;

proc format cntlin=CTRL2; run;

data SAMPLEDATA_2;
set SAMPLEDATA_1;
SHDATE=put(vvalue(TRD_EVENT_DT),greg2sh.);
run;``````

TRD_EVENT_TM TRD_STCK_CD TRD_PR TRD_TUROVR TRD_EVENT_DT SHDATE
12:19:35 IKCQ1 1 100 24MAR2008 1387-01-05
12:28:01 ALBZ1 1537 10000 26MAR2008 1387-01-07
13:13:44 ALBZ1 1567 10 12MAY2009 1388-02-22
12:20:38 AZAB1 683 10000 14JUL2011 1390-04-23
13:13:44 AZAB1 695 10 11OCT2013 1392-07-19
09:14:57 BALI1 850 9260 23MAR2008 1387-01-04
Regular Contributor
Posts: 166

## Re: How to convert dates from Gregorian to Solar Hijri calendar

Thanks @ChrisNZ;
But this code regenerates the date variable (TRD_EVENT_DT) with same values. For exmaple: 24MAR2008 is become 24MAR2008 again.
PROC Star
Posts: 2,318

## Re: How to convert dates from Gregorian to Solar Hijri calendar

what should it return?

Regular Contributor
Posts: 166

## Re: How to convert dates from Gregorian to Solar Hijri calendar

Your code generates Gregorian to Gregorian calendar. I ran your code using my data which I posted here, and there is no converting between these two calendars.
PROC Star
Posts: 2,318

## Re: How to convert dates from Gregorian to Solar Hijri calendar

I know nothing of the Solar Hijri calendar and I don't know you data either.

My code sample works as expected and creates Solar Hijri dates.

Regular Contributor
Posts: 166

## Re: How to convert dates from Gregorian to Solar Hijri calendar

No, I cannot;
I use codes and data of this post. However, it doesn't work.
Solution
‎11-09-2017 05:59 PM
PROC Star
Posts: 2,318

## Re: How to convert dates from Gregorian to Solar Hijri calendar

This works:

``````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
run;
data CTRL2(drop=LABEL rename=(START=LABEL IN=START ));
set CTRL(drop=FMTNAME);
FMTNAME = 'greg2sh';
TYPE    = 'C';
IN      = put(LABEL,date9. -l);
run;
proc format cntlin=CTRL2;
run;
data WORK.SAMPLEDATA_1;
infile datalines dsd truncover;
input TRD_EVENT_TM:\$24. TRD_STCK_CD:\$15. TRD_PR:32. TRD_TUROVR:13. TRD_EVENT_DT:DATE9.;
format TRD_TUROVR 13. TRD_EVENT_DT DATE9.;
label TRD_EVENT_TM="TRD_EVENT_TM" TRD_STCK_CD="TRD_STCK_CD" TRD_PR="TRD_PR" TRD_TUROVR="TRD_TUROVR";
cards;
12:19:35,IKCQ1,1,100,24MAR2008
12:28:01,ALBZ1,1537,10000,26MAR2008
13:13:44,ALBZ1,1567,10,12MAY2009
12:20:38,AZAB1,683,10000,14JUL2011
13:13:44,AZAB1,695,10,11OCT2013
09:14:57,BALI1,850,9260,23MAR2008
09:15:06,BALI1,850,2000,25APR2008
09:15:14,BALI1,850,10000,14JUN2009
09:15:24,BALI1,850,6000,18JUL2010
09:29:27,BALI1,850,10000,11SEP2011
12:28:00,BALI1,850,10000,20OCT2012
12:28:07,BALI1,850,10000,24NOV2012
13:13:44,BALI1,865,10,26NOV2012
09:38:04,BANK1,1164,10729,13AUG2008
11:24:44,BANK1,1148,2000,15JAN2009
11:24:46,BANK1,1147,1575,19MAR2009
12:10:34,BANK1,1147,5000,17MAY2010
12:10:34,BANK1,1147,3425,22AUG2012
12:14:55,BANK1,1141,41575,24AUG2013
12:14:55,BANK1,1141,8425,24OCT2013
run;
data SAMPLEDATA_2;
set SAMPLEDATA_1;
SHDATE=put(vvalue(TRD_EVENT_DT),greg2sh.);
run;``````

TRD_EVENT_TM TRD_STCK_CD TRD_PR TRD_TUROVR TRD_EVENT_DT SHDATE
12:19:35 IKCQ1 1 100 24MAR2008 1387-01-05
12:28:01 ALBZ1 1537 10000 26MAR2008 1387-01-07
13:13:44 ALBZ1 1567 10 12MAY2009 1388-02-22
12:20:38 AZAB1 683 10000 14JUL2011 1390-04-23
13:13:44 AZAB1 695 10 11OCT2013 1392-07-19
09:14:57 BALI1 850 9260 23MAR2008 1387-01-04
09:15:06 BALI1 850 2000 25APR2008 1387-02-06
09:15:14 BALI1 850 10000 14JUN2009 1388-03-24
09:15:24 BALI1 850 6000 18JUL2010 1389-04-27
09:29:27 BALI1 850 10000 11SEP2011 1390-06-20
12:28:00 BALI1 850 10000 20OCT2012 1391-07-29
12:28:07 BALI1 850 10000 24NOV2012 1391-09-04
13:13:44 BALI1 865 10 26NOV2012 1391-09-06
09:38:04 BANK1 1164 10729 13AUG2008 1387-05-23
11:24:44 BANK1 1148 2000 15JAN2009 1387-10-26
11:24:46 BANK1 1147 1575 19MAR2009 1387-12-29
12:10:34 BANK1 1147 5000 17MAY2010 1389-02-27
12:10:34 BANK1 1147 3425 22AUG2012 1391-06-01
12:14:55 BANK1 1141 41575 24AUG2013 1392-06-02
12:14:55 BANK1 1141 8425 24OCT2013 1392-08-02
Regular Contributor
Posts: 166

## Re: How to convert dates from Gregorian to Solar Hijri calendar

I am grateful for your help.
PROC Star
Posts: 2,318