Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- age: procedure options(main);
- dcl 1 date_type based,
- 2 dd pic '99',
- 2 mm pic '99',
- 2 yyyy pic '9999';
- dcl format char(8) nonasgn init('DDMMYYYY');
- dcl (i, xx) fixed bin(31);
- dcl (d1, d2) like date_type;
- dcl (firstdays, numdays, yeardays) fixed bin(31);
- dcl (today, birthday) char(8) date('DDMMYYYY');
- dcl (temp, days_to_today) bit(*) controlled;
- dcl (y, m, d) fixed bin(31);
- dcl input char(20) var;
- ask: display('Input birthday (DDMMYYYY)') reply(input);
- if ^validdate(trim(input), format) then
- do;
- display('Not a valid birthday!');
- goto ask;
- end;
- birthday = trim(input);
- today = datetime(format);
- firstdays = days('0101' || trim(substr(today, 5, 4)), format);
- yeardays = days('0101' || trim(substr(today, 5, 4) + 1), format) -
- days('0101' || trim(substr(today, 5, 4)), format);
- numdays = days(today, format) - firstdays;
- allocate temp bit (yeardays);
- allocate days_to_today bit(numdays);
- d1.dd = substr(birthday, 1, 2);
- d1.yyyy = substr(today, 5, 4);
- do i = 1 to 12;
- d1.mm = i;
- if validdate(string(d1), format) then
- do;
- xx = days(string(d1), format) - firstdays;
- if xx = 0 then
- xx = 1;
- substr(temp, xx, 1) = '1'b;
- end;
- else
- do;
- d2 = d1;
- d2.dd = '01';
- d2.mm += 1;
- xx = (days(string(d2), format) - 1) - firstdays;
- substr(temp, xx, 1) = '1'b;
- end;
- end;
- days_to_today = substr(temp, 1, numdays);
- free temp;
- y = substr(today, 5, 4) - substr(birthday, 5, 4);
- m = tally(days_to_today, '1'b) - substr(birthday, 3, 2);
- if m < 0 then
- do;
- m += 12;
- y -= 1;
- end;
- d = numdays - searchr(days_to_today,'1'b);
- display('You are ' || trim(y) || ' years, '
- || trim(m) || ' months and ' || trim(d) || ' days old.');
- free days_to_today;
- end age;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement