*! version 1.0.0  12dec1997  statalist distribution
program define mkstrsn /* newvarname varname_containing_ssn */
        version 5.0
        local new "`1'"
        local old "`2'"
        if "`3'"!="" { error 198 }
        confirm new var `new'
        unabbrev `old'
        local old $S_1

        tempvar p1 p2 sp1 sp2
        quietly {                                  /* 123456789 */
                gen `p1' = int(`old'/100000)       /* p1 = 1234 */
                gen `p2' = `old' - `p1'*100000     /* p2 = 56789 */
                mkstr `sp1' `p1' 4
                mkstr `sp2' `p2' 5
                gen str12 `new' = `sp1' + `sp2'
                compress `new'
        }
end

program define mkstr /* newvar oldvar #digits */
        local new `1'
        local old `2'
        local n `3'

        gen str12 `new' = string(`old')
        capture assert length(`new') <= `n'
        if _rc {
                di in red "social-security number has too many digits"
                exit 109
        }
        capture assert length(`new')==`n'
        while _rc {
                replace `new' = "0" + `new' if length(`new')<`n'
                capture assert length(`new')==`n'
        }
end