Skip to content

Instantly share code, notes, and snippets.

@rudvfaden
Created May 25, 2021 06:50
Show Gist options
  • Save rudvfaden/0c512318cfb7dfba386b39d0419c6159 to your computer and use it in GitHub Desktop.
Save rudvfaden/0c512318cfb7dfba386b39d0419c6159 to your computer and use it in GitHub Desktop.
Creates birthdate from cpr
***********************************************************************;
* Projekt : Makro
*
* Program name :
*
* Path :
*
* Author : Rud Faden
*
* Date created :
*
* Comments : Makroen beregner fødselsdag ud fra CPR-nummer.
*
InputDS: Inputdataset
OutputDS: Outputdataset
CPRKolonne: Kolonne i InputDS, som indeholder det CPR-nummer der skal beregnes på
FoedselsdatoKolonne: navnet på den kolonne der skal indeholde fødselsdatoen (Kolonnen er en SASdato med formatet date9.)
Eksempel:
%fodselsdatofracpr(InputDS=test,OutputDS=test2,CPRKolonne=CPR,FoedselsdatoKolonne=dag)
|**********************************************************************;
%macro fodselsdatofracpr(inputds,outputds,cprkolonne,foedselsdatokolonne);
data &outputds.(drop=pos0102 pos0304 pos0506 pos07 stringcpr);
set &inputds.;
%if %datatyp(&cprkolonne.)=CHAR %then %do;
attrib &foedselsdatokolonne. format = date9. length = 4;
stringcpr = put(&cprkolonne., 10.);
pos0102 = input(substr(stringcpr,1,2),best8.);
pos0304 = input(substr(stringcpr,3,2),best8.);
pos0506 = input(substr(stringcpr,5,2),best8.);
pos07 = input(substr(stringcpr,7,1),best8.);
%* Beregner fødselsdatud ud fram cpr numerets opbygning. https://www.cpr.dk/media/9345/personnummeret-i-cpr.pdf;
if pos07 <= 3 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+1900);
else if 0 <= pos0506 and pos0506 <=36 and pos07=4 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+2000);
else if 37 <= pos0506 and pos0506 <=99 and pos07=4 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+1900);
else if 0 <= pos0506 and pos0506 <=57 and pos07=5 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+2000);
else if 58 <= pos0506 and pos0506 <=99 and pos07=5 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+1800);
else if 0 <= pos0506 and pos0506 <=57 and pos07=6 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+2000);
else if 58 <= pos0506 and pos0506 <=99 and pos07=6 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+1800);
else if 0 <= pos0506 and pos0506 <=57 and pos07=7 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+2000);
else if 58 <= pos0506 and pos0506 <=99 and pos07=7 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+1800);
else if 0 <= pos0506 and pos0506 <=57 and pos07=8 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+2000);
else if 58 = pos0506 and pos0506 <=99 and pos07=8 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+1800);
else if 0 <= pos0506 and pos0506 <=36 and pos07=9 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+2000);
else if 37 <= pos0506 and pos0506 <=99 and pos07=9 then &foedselsdatokolonne.=mdy(pos0304,pos0102,pos0506+1900);
%end;
%else %do;
%put ERROR: CPR nummer skal være en en streng;
%end;
run;
%mend fodselsdatofracpr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment