Skip to content

Instantly share code, notes, and snippets.

@rudvfaden
Last active June 3, 2021 09:09
Show Gist options
  • Save rudvfaden/60e152420a8e5209c04bdee81c7ff496 to your computer and use it in GitHub Desktop.
Save rudvfaden/60e152420a8e5209c04bdee81c7ff496 to your computer and use it in GitHub Desktop.
Get birthday from danish cpr using case expression
%macro CprToBirthdaySql(cprnr);
case
when 0 <= input(substr(&cprnr,8,1),?1.) <= 9 and
0 <= input(substr(&cprnr,9,1),?1.) <= 9 then
case
when input(substr(&cprnr,7,1),1.) in (5,6,7,8) and
input(substr(&cprnr,5,2),2.) > 58 then
mdy(input(substr(&cprnr,3,2),2.),
input(substr(&cprnr,1,2),2.),
sum(1800,input(substr(&cprnr,5,2),2.)))
when input(substr(&cprnr,7,1),1.) in (0,1,2,3) or
(input(substr(&cprnr,7,1),1.) in (4,9) and
input(substr(&cprnr,5,2),2.) >= 37) then
mdy(input(substr(&cprnr,3,2),2.),
input(substr(&cprnr,1,2),2.),
sum(1900,input(substr(&cprnr,5,2),2.)))
when input(substr(&cprnr,7,1),1.) in (4,5,6,7,8,9) and
input(substr(&cprnr,5,2),2.) < 37 then
mdy(input(substr(&cprnr,3,2),2.),
input(substr(&cprnr,1,2),2.),
sum(2000,input(substr(&cprnr,5,2),2.)))
else .
end
else
case
when input(substr(&cprnr,7,1),1.) in (5,9) then
mdy(input(substr(&cprnr,3,2),2.),
input(substr(&cprnr,1,2),2.),
sum(1800,input(substr(&cprnr,5,2),2.)))
when input(substr(&cprnr,7,1),1.) in (0,8) then
mdy(input(substr(&cprnr,3,2),2.),
input(substr(&cprnr,1,2),2.),
sum(1900,input(substr(&cprnr,5,2),2.)))
when input(substr(&cprnr,7,1),1.) = 6 then
mdy(input(substr(&cprnr,3,2),2.),
input(substr(&cprnr,1,2),2.),
sum(2000,input(substr(&cprnr,5,2),2.)))
else .
end
end
%mend CprToBirthdaySql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment