*! cno11isco08: recodificar CNO-11 a ISCO-08 (2, 3 o o4 dígitos)
*! version 1.1.0  20nov2025  author: TU NOMBRE

program define cno11isco08
    version 13

    syntax varname(numeric) [ , GENerate(name) REPLACE FOUR THREE ]


    local cno11 `varlist'

// ensure that FOUR and THREE are not requested simultaneously
    if ("`four'" != "" & "`three'" != "") {
        di as err "you cannot specify options {bf:four} and {bf:three} at the same time."
        exit 198
    }

    // decide the default name of the output variable
    if ("`generate'" == "") {
        if ("`four'" != "")          local generate isco08_4d
        else if ("`three'" != "")    local generate isco08_3d
        else                         local generate isco08_2d
    }

    // check whether the output variable already exists
    capture confirm variable `generate'
    if !_rc {
        if ("`replace'" == "") {
            di as err "variable `generate' already exists; use the {bf:replace} option to overwrite it."
            exit 110
        }
        else {
            drop `generate'
        }
    }

    // quick diagnostic check on the number of digits
    quietly summarize `cno11'
    local max = r(max)

    // default: 2-digit mode
    if ("`four'" == "" & "`three'" == "") {
        if (`max' > 99) {
            di as txt "note: the maximum value of `cno11' is " as res `max' ///
               as txt ", which looks more like a 3- or 4-digit code."
        }
    }
    // 4-digit mode
    else if ("`four'" != "") {
        if (`max' < 1000) {
            di as txt "note: the maximum value of `cno11' is " as res `max' ///
               as txt ", which may not be a 4-digit code (check the {bf:four} option)."
        }
    }
    // 3-digit mode
    else if ("`three'" != "") {
        if (`max' < 100 | `max' > 999) {
            di as txt "note: the maximum value of `cno11' is " as res `max' ///
               as txt ", for 3-digit mode I expect values between 100 and 999."
        }
    }

    // ================================
    //  MODO 2 DÍGITOS (CNO11_2 → ISCO08_2)
    // ================================
    if ("`four'" == "" & "`three'" == "") {

        quietly recode `cno11' ///
            (0  = 3) ///
            (11 = 11) ///
            (12 = 12) ///
            (13 = 13) ///
            (14 15 = 14) ///
            (21 = 22) ///
            (22 23 = 23) ///
            (24 = 21) ///
            (25 28 29 = 26) ///
            (26 = 24) ///
            (27 = 25) ///
            (31 32 = 31) ///
            (33 = 32) ///
            (34 = 33) ///
            (35 36 = 33) ///
            (37 = 34) ///
            (38 = 35) ///
            (41 = 43) ///
            (42 = 44) ///
            (43 = 41) ///
            (44 45 = 42) ///
            (50 51 58 = 51) ///
            (52 53 54 55 = 52) ///
            (56 57 = 53) ///
            (59 = 54) ///
            (61 62 63 = 61) ///
            (64 = 62) ///
            (71 72 = 71) ///
            (73 74 = 72) ///
            (75 = 74) ///
            (76 = 73) ///
            (77 78 = 75) ///
            (81 = 81) ///
            (82 = 82) ///
            (83 84 = 83) ///
            (91 92 = 91) ///
            (95 = 92) ///
            (96 97 98 = 93) ///
            (93 = 94) ///
            (94 = 96) ///
            , gen(`generate')


        capture label list isco08_2d
        if !_rc label values `generate' isco08_2d
    }

    // ================================
    //  MODO 4 DÍGITOS (CNO11_4 → ISCO08_4)
    // ================================
    else if ("`four'" != "") {

        quietly recode `cno11' ///
   (1111=	1111)  ///
       (1112	=1112) ///
(1113=	1114) ///
(1120=	1120) ///
(1211=	1211) ///
(1212=	1212) ///
(1219=	1213) ///
(1221=	1221) ///
(1222=	1222) ///
(1223=	1223) ///
(1329 = 1349) ///
(1411 = 1411) ///
(1419 = 1411) ///
(1421 = 1412) ///
(1422 = 1412) ///
(1429 = 1412) ///
(1431 = 1420) ///
(1432 = 1420) ///
(1501 = 1431) ///
(1509 = 1439) ///
(2111 = 2211) ///
(2112 = 2212) ///
(2121 = 2221) ///
(2122 = 2221) ///
(2123 = 2222) ///
(2130 = 2250) ///
(2140 = 2262) ///
(2151 = 2261) ///
(2152 = 2264) ///
(2153 = 2265) ///
(2154 = 2266) ///
(2155 = 2267) ///
(2156 = 2269) ///
(2157 = 2269) ///
(2158 = 2263) ///
(2159 = 2269) ///
(2210 = 2310) ///
(2220 = 2320) ///
(2230 = 2330) ///
(2240 = 2341) ///
(2251 = 2342) ///
(2252 = 2342) ///
(2311 = 2352) ///
(2312 = 2352) ///
(2321 = 2351) ///
(2322 = 2353) ///
(2323 = 2354) ///
(2324 = 2355) ///
(2325 = 2356) ///
(2326 = 2359) ///
(2329 = 2359) ///
(2411 = 2111) ///
(2412 = 2112) ///
(2413 = 2113) ///
(2414 = 2114) ///
(2415 = 2120) ///
(2416 = 2120) ///
(2421 = 2131) ///
(2422 = 2132) ///
(2423 = 2132) ///
(2424 = 2132) ///
(2425 = 2132) ///
(2426 = 2133) ///
(2427 = 2132) ///
(2431 = 2141) ///
(2432 = 2142) ///
(2433 = 2144) ///
(2434 = 2144) ///
(2435 = 2145) ///
(2436 = 2146) ///
(2437 = 2143) ///
(2439 = 2149) ///
(2441 = 2151) ///
(2442 = 2152) ///
(2443 = 2153) ///
(2451 = 2161) ///
(2452 = 2162) ///
(2453 = 2164) ///
(2454 = 2165) ///
(2461 = 2141) ///
(2462 = 2142) ///
(2463 = 2144) ///
(2464 = 2144) ///
(2465 = 2145) ///
(2466 = 2146) ///
(2469 = 2149) ///
(2471 = 2151) ///
(2472 = 2152) ///
(2473 = 2153) ///
(2481 = 2161) ///
(2482 = 2163) ///
(2483 = 2165) ///
(2484 = 2166) ///
(2511 = 2611) ///
(2512 = 2611) ///
(2513 = 2612) ///
(2591 = 2619) ///
(2592 = 2619) ///
(2599 = 2619) ///
(2611 = 2411) ///
(2612 = 2412) ///
(2613 = 2413) ///
(2621 = 2421) ///
(2622 = 2422) ///
(2623 = 2422) ///
(2624 = 2423) ///
(2625 = 2424) ///
(2630 = 2422) ///
(2640 = 2433) ///
(2651 = 2431) ///
(2652 = 2432) ///
(2653 = 2434) ///
(2711 = 2511) ///
(2712 = 2512) ///
(2713 = 2513) ///
(2719 = 2519) ///
(2721 = 2521) ///
(2722 = 2522) ///
(2723 = 2523) ///
(2729 = 2529) ///
(2810 = 2631) ///
(2821 = 2632) ///
(2822 = 2633) ///
(2823 = 2634) ///
(2824 = 2635) ///
(2825 = 2635) ///
(2830 = 2636) ///
(2911 = 2621) ///
(2912 = 2622) ///
(2921 = 2641) ///
(2922 = 2642) ///
(2923 = 2643) ///
(2931 = 2651) ///
(2932 = 2652) ///
(2933 = 2653) ///
(2934 = 2654) ///
(2935 = 2655) ///
(2936 = 2656) ///
(2937 = 2659) ///
(2939 = 2659) ///
(3110 = 3118) ///
(3121 = 3111) ///
(3122 = 3112) ///
(3123 = 3113) ///
(3124 = 3114) ///
(3125 = 3114) ///
(3126 = 3115) ///
(3127 = 3116) ///
(3128 = 3117) ///
(3129 = 3119) ///
(3131 = 3131) ///
(3132 = 3132) ///
(3133 = 3133) ///
(3134 = 3134) ///
(3135 = 3135) ///
(3139 = 3139) ///
(3141 = 3141) ///
(3142 = 3142) ///
(3143 = 3143) ///
(3151 = 3151) ///
(3152 = 3152) ///
(3153 = 3153) ///
(3154 = 3154) ///
(3155 = 3155) ///
(3160 = 3111) ///
(3201 = 3121) ///
(3202 = 3123) ///
(3203 = 3122) ///
(3204 = 3122) ///
(3205 = 3122) ///
(3206 = 3122) ///
(3207 = 3122) ///
(3209 = 3122) ///
(3311 = 3211) ///
(3312 = 3211) ///
(3313 = 3212) ///
(3314 = 3212) ///
(3315 = 3214) ///
(3316 = 3214) ///
(3317 = 3214) ///
(3321 = 3251) ///
(3322 = 3252) ///
(3323 = 3259) ///
(3324 = 3254) ///
(3325 = 3255) ///
(3326 = 3257) ///
(3327 = 3240) ///
(3329 = 3259) ///
(3331 = 2230) ///
(3339 = 3230) ///
(3401 = 3311) ///
(3402 = 3312) ///
(3403 = 3313) ///
(3404 = 3314) ///
(3405 = 3315) ///
(3510 = 3322) ///
(3521 = 3321) ///
(3522 = 3323) ///
(3523 = 3324) ///
(3531 = 3331) ///
(3532 = 3332) ///
(3533 = 3333) ///
(3534 = 3334) ///
(3535 = 3339) ///
(3539 = 3339) ///
(3611 = 3341) ///
(3612 = 3342) ///
(3613 = 3343) ///
(3614 = 3344) ///
(3621 = 3352) ///
(3622 = 3353) ///
(3623 = 3354) ///
(3629 = 3359) ///
(3631 = 3355) ///
(3632 = 3355) ///
(3711 = 3411) ///
(3712 = 3411) ///
(3713 = 3412) ///
(3714 = 3412) ///
(3715 = 3412) ///
(3716 = 3413) ///
(3721 = 3421) ///
(3722 = 3422) ///
(3723 = 3423) ///
(3724 = 3423) ///
(3731 = 3431) ///
(3732 = 3432) ///
(3733 = 3433) ///
(3734 = 3434) ///
(3739 = 3435) ///
(3811 = 3511) ///
(3812 = 3512) ///
(3813 = 3513) ///
(3814 = 3514) ///
(3820 = 3512) ///
(3831 = 3521) ///
(3832 = 3521) ///
(3833 = 3522) ///
(4111 = 4311) ///
(4112 = 4313) ///
(4113 = 4312) ///
(4121 = 4321) ///
(4122 = 4322) ///
(4123 = 4323) ///
(4210 = 4411) ///
(4221 = 4412) ///
(4222 = 4413) ///
(4223 = 4416) ///
(4301 = 4131) ///
(4309 = 4110) ///
(4411 = 4225) ///
(4412 = 4226) ///
(4421 = 4221) ///
(4422 = 4224) ///
(4423 = 4223) ///
(4424 = 4222) ///
(4430 = 4227) ///
(4441 = 4211) ///
(4442 = 4212) ///
(4443 = 4212) ///
(4444 = 4213) ///
(4445 = 4214) ///
(4446 = 4211) ///
(4500 = 4229) ///
(5000 = 5120) ///
(5110 = 5120) ///
(5120 = 5131) ///
(5210 = 5222) ///
(5220 = 5223) ///
(5300 = 5221) ///
(5411 = 5211) ///
(5412 = 5211) ///
(5420 = 5244) ///
(5430 = 5245) ///
(5491 = 5243) ///
(5492 = 5242) ///
(5493 = 5241) ///
(5499 = 5249) ///
(5500 = 5230) ///
(5611 = 5321) ///
(5612 = 5321) ///
(5621 = 3213) ///
(5622 = 3258) ///
(5629 = 5329) ///
(5710 = 5322) ///
(5721 = 5311) ///
(5722 = 5311) ///
(5811 = 5141) ///
(5812 = 5142) ///
(5821 = 5111) ///
(5822 = 5112) ///
(5823 = 5113) ///
(5824 = 5113) ///
(5825 = 5113) ///
(5831 = 5151) ///
(5832 = 5152) ///
(5833 = 5153) ///
(5840 = 5152) ///
(5891 = 5162) ///
(5892 = 5163) ///
(5893 = 5164) ///
(5894 = 5165) ///
(5895 = 5161) ///
(5899 = 5169) ///
(5910 = 3351) ///
(5921 = 3351) ///
(5922 = 5412) ///
(5923 = 5412) ///
(5931 = 5411) ///
(5932 = 5411) ///
(5941 = 5414) ///
(5942 = 5414) ///
(5991 = 5413) ///
(5992 = 5419) ///
(5993 = 5419) ///
(5999 = 5419) ///
(6110 = 6111) ///
(6120 = 6113) ///
(6201 = 6121) ///
(6202 = 6121) ///
(6203 = 6121) ///
(6204 = 6123) ///
(6205 = 6122) ///
(6209 = 6121) ///
(6300 = 6130) ///
(6410 = 6210) ///
(6421 = 6221) ///
(6422 = 6222) ///
(6423 = 6223) ///
(6430 = 6224) ///
(7111 = 7114) ///
(7112 = 7114) ///
(7121 = 7111) ///
(7122 = 7113) ///
(7131 = 7115) ///
(7132 = 7115) ///
(7191 = 7119) ///
(7192 = 7119) ///
(7193 = 7119) ///
(7199 = 7119) ///
(7211 = 7123) ///
(7212 = 7123) ///
(7221 = 7126) ///
(7222 = 7126) ///
(7223 = 7126) ///
(7231 = 7131) ///
(7232 = 7131) ///
(7240 = 7122) ///
(7250 = 7127) ///
(7291 = 7121) ///
(7292 = 7124) ///
(7293 = 7125) ///
(7294 = 7126) ///
(7295 = 7133) ///
(7311 = 7211) ///
(7312 = 7212) ///
(7313 = 7213) ///
(7314 = 7214) ///
(7315 = 7215) ///
(7321 = 7221) ///
(7322 = 7222) ///
(7323 = 7223) ///
(7324 = 7224) ///
(7401 = 7231) ///
(7402 = 7232) ///
(7403 = 7233) ///
(7404 = 7233) ///
(7405 = 7234) ///
(7510 = 7411) ///
(7521 = 7412) ///
(7522 = 7413) ///
(7531 = 7421) ///
(7532 = 7421) ///
(7533 = 7422) ///
(7611 = 7311) ///
(7612 = 7312) ///
(7613 = 7313) ///
(7614 = 7314) ///
(7615 = 7315) ///
(7616 = 7316) ///
(7617 = 7317) ///
(7618 = 7318) ///
(7619 = 7319) ///
(7621 = 7321) ///
(7622 = 7322) ///
(7623 = 7323) ///
(7701 = 7511) ///
(7702 = 7511) ///
(7703 = 7512) ///
(7704 = 7513) ///
(7705 = 7514) ///
(7706 = 7514) ///
(7707 = 7514) ///
(7708 = 7516) ///
(7709 = 7515) ///
(7811 = 7521) ///
(7812 = 7523) ///
(7820 = 7522) ///
(7831 = 7531) ///
(7832 = 7532) ///
(7833 = 7532) ///
(7834 = 7533) ///
(7835 = 7534) ///
(7836 = 7535) ///
(7837 = 7536) ///
(7891 = 7541) ///
(7892 = 7542) ///
(7893 = 7543) ///
(7894 = 7544) ///
(7899 = 7549) ///
(8111 = 8111) ///
(8112 = 8112) ///
(8113 = 8113) ///
(8114 = 8114) ///
(8121 = 8121) ///
(8122 = 8122) ///
(8131 = 8131) ///
(8132 = 8131) ///
(8133 = 8132) ///
(8141 = 8141) ///
(8142 = 8142) ///
(8143 = 8143) ///
(8144 = 8172) ///
(8145 = 8171) ///
(8151 = 8151) ///
(8152 = 8152) ///
(8153 = 8153) ///
(8154 = 8154) ///
(8155 = 8155) ///
(8156 = 8156) ///
(8159 = 8159) ///
(8160 = 8160) ///
(8170 = 8157) ///
(8191 = 8181) ///
(8192 = 8182) ///
(8193 = 8183) ///
(8199 = 8189) ///
(8201 = 8211) ///
(8202 = 8212) ///
(8209 = 8219) ///
(8311 = 8311) ///
(8312 = 8312) ///
(8321 = 8341) ///
(8322 = 8341) ///
(8331 = 8342) ///
(8332 = 8343) ///
(8333 = 8344) ///
(8340 = 8350) ///
(8411 = 8322) ///
(8412 = 8322) ///
(8420 = 8331) ///
(8431 = 8332) ///
(8432 = 8332) ///
(8440 = 8321) ///
(9100 = 9111) ///
(9210 = 9112) ///
(9221 = 9121) ///
(9222 = 9122) ///
(9223 = 9123) ///
(9229 = 9129) ///
(9310 = 9412) ///
(9320 = 9411) ///
(9410 = 9520) ///
(9420 = 9510) ///
(9431 = 9621) ///
(9432 = 9621) ///
(9433 = 9621) ///
(9434 = 9623) ///
(9441 = 9611) ///
(9442 = 9612) ///
(9443 = 9613) ///
(9490 = 9622) ///
(9511 = 9211) ///
(9512 = 9214) ///
(9520 = 9212) ///
(9530 = 9213) ///
(9541 = 9216) ///
(9542 = 9216) ///
(9543 = 9215) ///
(9601 = 9312) ///
(9602 = 9313) ///
(9603  = 9311) ///
(9700 = 9321) ///
(9811 = 9331) ///
(9812 = 9331) ///
(9820 = 9334) ///
, gen(`generate')

        capture label list isco08_4d
        if !_rc label values `generate' isco08_4d
    }

    // ================================
    //  MODO 3 DÍGITOS (CNO11_3 → ISCO08_3)
    // ================================
    else if ("`three'" != "") {

        quietly recode `cno11' ///
		(111 = 111) ///
(112 = 112) ///
(121 = 121) ///
(122 = 122) ///
(131 = 132) ///
(132 = 134) ///
(141 = 141) ///
(142 = 141) ///
(143 = 142) ///
(150 = 143) ///
(211 = 221) ///
(212 = 222) ///
(213 = 225) ///
(214 = 226) ///
(215 = 226) ///
(221 = 231) ///
(222 = 232) ///
(223 = 233) ///
(224 = 234) ///
(225 = 234) ///
(231 = 235) ///
(232 = 235) ///
(241 = 212) ///
(242 = 213) ///
(243 = 214) ///
(244 = 215) ///
(245 = 216) ///
(246 = 214) ///
(247 = 215) ///
(248 = 216) ///
(251 = 261) ///
(259 = 261) ///
(261 = 241) ///
(262 = 242) ///
(263 = 242) ///
(264 = 243) ///
(265 = 243) ///
(271 = 251) ///
(272 = 252) ///
(281 = 263) ///
(282 = 263) ///
(283 = 263) ///
(291 = 262) ///
(292 = 264) ///
(293 = 265) ///
(311 = 311) ///
(312 = 311) ///
(313 = 313) ///
(314 = 314) ///
(315 = 315) ///
(316 = 311) ///
(320 = 312) ///
(331 = 321) ///
(332 = 325) ///
(333 = 323) ///
(340 = 331) ///
(351 = 332) ///
(352 = 332) ///
(353 = 333) ///
(361 = 334) ///
(362 = 335) ///
(363 = 335) ///
(371 = 341) ///
(372 = 342) ///
(373 = 343) ///
(381 = 351) ///
(382 = 351) ///
(383 = 352) ///
(411 = 431) ///
(412 = 432) ///
(421 = 441) ///
(422 = 441) ///
(430 = 413) ///
(441 = 422) ///
(442 = 422) ///
(443 = 422) ///
(444 = 421) ///
(450 = 422) ///
(500 = 512) ///
(511 = 512) ///
(512 = 513) ///
(521 = 522) ///
(522 = 522) ///
(530 = 522) ///
(541 = 521) ///
(542 = 524) ///
(543 = 524) ///
(549 = 524) ///
(550 = 523) ///
(561 = 532) ///
(562 = 532) ///
(571 = 532) ///
(572 = 531) ///
(581 = 514) ///
(582 = 511) ///
(583 = 515) ///
(584 = 515) ///
(589 = 516) ///
(591 = 335) ///
(592 = 541) ///
(593 = 541) ///
(594 = 541) ///
(599 = 541) ///
(611 = 611) ///
(612 = 611) ///
(620 = 612) ///
(630 = 613) ///
(641 = 621) ///
(642 = 622) ///
(643 = 622) ///
(711 = 711) ///
(712 = 711) ///
(713 = 711) ///
(719 = 711) ///
(721 = 712) ///
(722 = 712) ///
(723 = 713) ///
(724 = 712) ///
(725 = 712) ///
(729 = 713) ///
(731 = 721) ///
(732 = 722) ///
(740 = 723) ///
(751 = 741) ///
(752 = 741) ///
(753 = 742) ///
(761 = 731) ///
(762 = 732) ///
(770 = 751) ///
(781 = 752) ///
(782 = 752) ///
(783 = 753) ///
(789 = 754) ///
(811 = 811) ///
(812 = 812) ///
(813 = 813) ///
(814 = 817) ///
(815 = 815) ///
(816 = 816) ///
(817 = 815) ///
(819 = 818) ///
(820 = 821) ///
(831 = 831) ///
(832 = 834) ///
(833 = 834) ///
(834 = 835) ///
(841 = 832) ///
(842 = 833) ///
(843 = 833) ///
(844 = 832) ///
(910 = 911) ///
(921 = 911) ///
(922 = 912) ///
(931 = 941) ///
(932 = 941) ///
(941 = 952) ///
(942 = 951) ///
(943 = 962) ///
(944 = 961) ///
(949 = 962) ///
(951 = 921) ///
(952 = 921) ///
(953 = 921) ///
(954 = 921) ///
(960 = 931) ///
(970 = 932) ///
(981 = 933) ///
(982 = 933) ///
, gen(`generate')

        capture label list isco08_3d
        if !_rc label values `generate' isco08_3d
    }


    quietly count if !missing(`generate')
    local n_ok = r(N)
    quietly count if !missing(`cno11') & missing(`generate')
    local n_miss = r(N)

    di as txt "cno11isco08: recoded " as res `n_ok' ///
       as txt " cases; no equivalence found for " as res `n_miss' as txt "."

end