PNRCHECK



Stata help for pnrcheck

-------------------------------------------------------------------------------
help for pnrcheck 
-------------------------------------------------------------------------------


pnrcheck

pnrcheck variable_pnr [, savepnr(variable_name) saveind(variable_name) listwrong listmissing display ]

Description

pnrcheck Algorithm to verify the check digit in Swedish personal identification numbers (PNRs). The check digit is the tenth digit constructed from the preceding nine digits, according to date of birth and gender.

Remark

The variable_pnr could be either a string (A) or a numeric variable (B). The check digit could also be missing (C). See help datatypes.

(A) The variable_pnr could be stored as string

Case 1 "4102179704" str10 (standard storage type) Case 2 "194102179704" str12 (if there are 2 initial numbers of the year) Case 3 "410217-9704" str11 (if there is a minus sign) Case 4 "19410217-9704" str13 (if there is a minus sign and the 2 initial numbers of the year)

(B) The variable_pnr could be stored as number with double precision

Case 5 4.102e+09 double (corresponding to Case 1) Case 6 1.914e+11 double (corresponding to Case 2)

(C) The tenth digit or check digit of the variable_pnr could be missing

Case 7 4.102e+08 double (Case 5 without the check digit) Case 8 "410217970" str9 (Case 1 without the check digit)

Algorithm

STEP 1 Multiply each of the first nine digits in the PNR by the digits 2,1,2,1,2,1,2,1,2

STEP 2 Calculate the cumulative sum of each of these 9 calculations

STEP 3 Verify if the check digit of variable_pnr is equal to the number we would have to add to the cumulative sum (from STEP 2) to obtain a multiple of 10

Options

savepnr(variable_name) saves the right PNR variable

saveind(variable_name) saves an indicator variable according to the check digit status (0=Right, 1=Wrong, 2=Missing)

display displays algorithm's results: number of wrong and right PNR, percentage

listwrong lists temporary variables input_pnr i_check output_pnr o_check only for individuals whose check digit is wrong

listmissing lists temporary variables input_pnr i_check output_pnr o_check only for individuals whose check digit is missing

Examples

. insheet using http://nicolaorsini.altervista.org/stata/tutorial/p/ex_pnrs.txt,clear . pnrcheck pnr . pnrcheck pnr, savep(pnrright) di . pnrcheck pnr, savep(pnrright) savei(indvar) di listw

Authors

Nicola Orsini, I.M.M., Karolinska Institutet, Sweden Nicholas J. Cox, University of Durham, U.K. Rino Bellocco, M.E.B., Karolinska Institutet, Sweden

Support

Nicola Orsini, http://nicolaorsini.altervista.org, Karolinska Institutet, Sweden nicola.orsini@imm.ki.se

Also see

[R] destring [U] Commands for dealing with strings

On-line: help for destring, macro, strfun

Worked examples for pnrcheck

Click here to run or save the do-file for the following worked examples and be sure to have an update version. Type
. net install http://nicolaorsini.altervista.org/stata/pnrcheck 
We recommend to handle the pnr variable as a string or numeric with double accuracy.
Note that the correct pnr variable is always stored (if the option savepnr is specified) as a 10 digit string variable whatever it is the original storage type.

Case 1 - The storage type of the pnr variable is string of 10 digits

. clear   

. set obs 1    
obs was 0, now 1

. gen str10 pnr = "3103170993"      

. pnrcheck pnr, di savep(pnrright) listw       

  PNR        Obs        Perc.(%)
----------------------------------------
 Wrong         0         0.0
 Right         1         100.0
----------------------------------------
 Total         1         100.0

. d          

Contains data
  obs:             1                          
 vars:             2                          
 size:            24 (99.9% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
pnr             str10  %10s                   
pnrright        str10  %10s                   
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

. l, sep(0)           

     +-------------------------+
     |        pnr     pnrright |
     |-------------------------|
  1. | 3103170993   3103170993 |
     +-------------------------+

Case 2 - The storage type of the pnr variable is string of 12 digits

. clear    

. set obs 1   
obs was 0, now 1

. gen str12 pnr = "193103170993"   

. pnrcheck pnr, di savep(pnrright) listw    

  PNR        Obs        Perc.(%)
----------------------------------------
 Wrong         0         0.0
 Right         1         100.0
----------------------------------------
 Total         1         100.0

. d   

Contains data
  obs:             1                          
 vars:             2                          
 size:            26 (99.9% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
pnr             str12  %12s                   
pnrright        str10  %10s                   
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

.l , sep(0)   

     +---------------------------+
     |          pnr     pnrright |
     |---------------------------|
  1. | 193103170993   3103170993 |
     +---------------------------+

Case 3 - The storage type of the pnr variable is string of 11 digits

 . clear   

. set obs 1     
obs was 0, now 1

. gen str11 pnr = "310317-0993"   

. pnrcheck pnr, di savep(pnrright) listw   

  PNR        Obs        Perc.(%)
----------------------------------------
 Wrong         0         0.0
 Right         1         100.0
----------------------------------------
 Total         1         100.0

. d   

Contains data
  obs:             1                          
 vars:             2                          
 size:            25 (99.9% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
pnr             str11  %11s                   
pnrright        str10  %10s                   
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

. l, sep(0)    

     +--------------------------+
     |         pnr     pnrright |
     |--------------------------|
  1. | 310317-0993   3103170993 |
     +--------------------------+

Case 4 - The storage type of the pnr variable is string of 13 digits

 . clear       

. set obs 1      
obs was 0, now 1

. gen str13 pnr = "19310317-0993"     

. pnrcheck pnr, di savep(pnrright) listw        

  PNR        Obs        Perc.(%)
----------------------------------------
 Wrong         0         0.0
 Right         1         100.0
----------------------------------------
 Total         1         100.0

. d        

Contains data
  obs:             1                          
 vars:             2                          
 size:            27 (99.9% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
pnr             str13  %13s                   
pnrright        str10  %10s                   
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

. l, sep(0)        

     +----------------------------+
     |           pnr     pnrright |
     |----------------------------|
  1. | 19310317-0993   3103170993 |
     +----------------------------+

Case 5 and 6 - The storage type of the pnr variable is numeric with double accuracy

. insheet using http://nicolaorsini.altervista.org/stata/tutorial/p/ex_pnrs.txt, clear   
(1 var, 6 obs)

. describe         

Contains data
  obs:             6                          
 vars:             1                          
 size:            72 (99.9% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
pnr             double %10.0g                 
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

. list , sep(0)       

     +-----------+
     |       pnr |
     |-----------|
  1. | 3.103e+09 |
  2. | 3.103e+09 |
  3. | 6.812e+09 |
  4. | 6.812e+09 |
  5. | 6.511e+09 |
  6. | 6.511e+09 |
     +-----------+

. format pnr %12.0f      

. list , sep(0)         

     +------------+
     |        pnr |
     |------------|
  1. | 3103170993 |
  2. | 3103170999 |
  3. | 6812241450 |
  4. | 6812241457 |
  5. | 6511280693 |
  6. | 6511280692 |
     +------------+

. pnrcheck pnr             

. pnrcheck pnr, di          

  PNR        Obs        Perc.(%)
----------------------------------------
 Wrong         3         50.0
 Right         3         50.0
----------------------------------------
 Total         6         100.0

. pnrcheck pnr, di listw           

  PNR        Obs        Perc.(%)
----------------------------------------
 Wrong         3         50.0
 Right         3         50.0
----------------------------------------
 Total         6         100.0

     +-----------------------------------------------+
     |  wrong_pnr   check_wr    right_pnr   check_ri |
     |-----------------------------------------------|
  2. | 3103170999          9   3103170993          3 |
  4. | 6812241457          7   6812241450          0 |
  5. | 6511280693          3   6511280692          2 |
     +-----------------------------------------------+

. pnrcheck pnr, savep(pnrright) savei(indvar)      

. describe                                   

Contains data
  obs:             6                          
 vars:             3                          
 size:           138 (99.9% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
pnr             double %12.0f                 
pnrright        str10  %10s                   
indvar          byte   %8.0g                  
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

. list , sep(0)                  

     +----------------------------------+
     |        pnr     pnrright   indvar |
     |----------------------------------|
  1. | 3103170993   3103170993        0 |
  2. | 3103170999   3103170993        1 |
  3. | 6812241450   6812241450        0 |
  4. | 6812241457   6812241450        1 |
  5. | 6511280693   6511280692        1 |
  6. | 6511280692   6511280692        0 |
     +----------------------------------+


Remarks - The storage type of the pnr variable cannot be float

. clear        

. set obs 1         
obs was 0, now 1

. gen float pnr = 3103170993       

. pnrcheck pnr, di savep(pnrright) listw       
                                                     
  PNR        Obs        Perc.(%)
----------------------------------------
 Wrong         1         100.0
 Right         0         0.0
----------------------------------------
 Total         1         100.0

     +-----------------------------------------------+
     |  wrong_pnr   check_wr    right_pnr   check_ri |
     |-----------------------------------------------|
  1. | 3103171072          2   3103171074          4 |
     +-----------------------------------------------+

. d    

Contains data
  obs:             1                          
 vars:             2                          
 size:            18 (99.9% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
pnr             float  %9.0g                  
pnrright        str10  %10s                   
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

. l, sep(0)   

     +-----------------------+
     |      pnr     pnrright |
     |-----------------------|
  1. | 3.10e+09   3103171074 |
     +-----------------------+


Home  Author  Presentations  Publications  Software  Courses  Links