*! version 1.0.0  12dec1997  statalist distribution
program define mkdashsn /* 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 p3 sp1 sp2 sp3
        quietly {                                            /* 123456789 */
		gen `p1' = int(`old'/1000000)                /* p1 = 123  */
		gen `p2' = int(`old'/10000) - `p1'*100       /* p2 = 45   */
		gen `p3' = `old' - `p1'*1000000 - `p2'*10000 /* p3 = 6789 */

		mkstr `sp1' `p1' 3
		mkstr `sp2' `p2' 2
		mkstr `sp3' `p3' 4
		gen str12 `new' = `sp1' + "-" + `sp2' + "-" + `sp3'
		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