Hi,

I am using STATA 14, I am interested in generating a new variable from household ID, which is a 10 digit code. I want to generate a new variable district that takes the 4th number in 10 digit household code.
Further, I want to generate one more variable from household id (hhcode) that is division, which will take value from second number in household code.

The coding scheme is available in this table http://www.pbs.gov.pk/sites/default/...%202015-16.pdf

Actually I want to know the reverse process of this https://www.statalist.org/forums/for...usehold-number

The data set from HIES 2018-19 for Pakistan using dataex command is as follows.
HTML Code:
----------------------- copy starting from the next line -----------------------
[CODE]
* Example generated by -dataex-. To install: ssc install dataex
clear
input double hhcode byte(province region) long psu str3 sec byte(idc s1aq02 s1aq03 s1aq04 s1aq05 age s1aq61 s1aq62) int s1aq63 byte(s1aq07 s1aq08 s1aq09 s1aq10 s1aq11) float weights byte code float division
1102010101 1 2 11020101 "01A"  1  1 . 1 1 45  0  0 1970 2 51 98 54 1 89.76067 11 1
1102010101 1 2 11020101 "01A"  2  7 1 1 1 22  0  0 1993 1  . 99 55 1 89.76067 11 1
1102010101 1 2 11020101 "01A"  3  7 1 1 1 15  0  0 2000 1  . 99 55 1 89.76067 11 1
1102010101 1 2 11020101 "01A" 51  2 1 2 1 40  0  0 1975 2  1 99 99 1 89.76067 11 1
1102010101 1 2 11020101 "01A" 52  3 1 2 1  6 15  4 2009 1  .  1 51 1 89.76067 11 1
1102010101 1 2 11020101 "01A" 53  3 1 1 1  5 20  9 2010 1  .  1 51 1 89.76067 11 1
1102010101 1 2 11020101 "01A" 54  5 1 2 1 60  0  0 1955 3  . 98 98 1 89.76067 11 1
1102010101 1 2 11020101 "01A" 55  6 1 2 1 40  0  0 1975 2 99 98 54 1 89.76067 11 1
1102010102 1 2 11020101 "01A"  1  1 . 1 1 29  0  0 1986 1  .  2 51 1 89.76067 11 1
1102010102 1 2 11020101 "01A"  2  5 1 1 1 65  0  0 1950 2 51 98 98 1 89.76067 11 1
1102010102 1 2 11020101 "01A" 51  5 1 2 1 55  0  0 1960 2  2 98 98 1 89.76067 11 1
1102010103 1 2 11020101 "01A"  1  1 . 1 1 67  0  0 1948 2 51 98 98 1 89.76067 11 1
1102010103 1 2 11020101 "01A"  2  3 3 1 1 26  0  0 1989 2 53  1 51 1 89.76067 11 1
1102010103 1 2 11020101 "01A"  3  4 3 1 1 14 14  3 2001 1  . 99 99 1 89.76067 11 1
1102010103 1 2 11020101 "01A"  4  4 3 1 1 10 12  5 2005 1  . 99 99 1 89.76067 11 1
1102010103 1 2 11020101 "01A" 51  2 3 2 1 67  0  0 1948 2  1 98 98 1 89.76067 11 1
1102010103 1 2 11020101 "01A" 52  3 3 2 1 28  0  0 1987 1  .  1 51 1 89.76067 11 1
1102010103 1 2 11020101 "01A" 53  8 3 2 1 25  0  0 1990 2  2 99 99 1 89.76067 11 1
1102010103 1 2 11020101 "01A" 54  4 3 1 1  8 15  6 2007 1  .  2 53 1 89.76067 11 1
1102010103 1 2 11020101 "01A" 55  4 3 1 1  4 14  4 2011 1  .  2 53 1 89.76067 11 1
1102010104 1 2 11020101 "01A" 51  1 . 2 1 38  0  0 1977 2 99 99 99 1 89.76067 11 1
1102010104 1 2 11020101 "01A" 52  3 2 2 1 14  0  0 2001 1  . 99 51 1 89.76067 11 1
1102010104 1 2 11020101 "01A" 53  3 2 2 1 12  0  0 2003 1  . 99 51 1 89.76067 11 1
1102010104 1 2 11020101 "01A" 54  3 2 1 1  9  0  0 2006 1  . 99 51 1 89.76067 11 1
1102010104 1 2 11020101 "01A" 55  3 2 2 1  3 16  7 2012 1  . 99 51 1 89.76067 11 1
1102010105 1 2 11020101 "01A"  1  1 . 1 1 54  0  0 1961 2 51 98 98 1 89.76067 11 1
1102010105 1 2 11020101 "01A"  2  3 3 1 1 27  0  0 1988 1  .  1 51 1 89.76067 11 1
1102010105 1 2 11020101 "01A"  3  3 3 1 1 23  0  0 1992 1  .  1 51 1 89.76067 11 1
1102010105 1 2 11020101 "01A"  4  3 3 1 1 20  0  0 1995 1  .  1 51 1 89.76067 11 1
1102010105 1 2 11020101 "01A"  5  3 3 1 1 15  0  0 2000 1  .  1 51 1 89.76067 11 1
1102010105 1 2 11020101 "01A" 51  2 3 2 1 48  0  0 1967 2  1 99 98 1 89.76067 11 1
1102010105 1 2 11020101 "01A" 52  3 3 2 1 25  0  0 1990 1  .  1 51 1 89.76067 11 1
1102010105 1 2 11020101 "01A" 53  3 3 2 1 18  0  0 1997 1  .  1 51 1 89.76067 11 1
1102010106 1 2 11020101 "01A"  1  1 . 1 1 21  3 10 1994 2 51 98 98 1 89.76067 11 1
1102010106 1 2 11020101 "01A"  2  6 1 1 1 19  0  0 1996 1  . 98 98 1 89.76067 11 1
1102010106 1 2 11020101 "01A" 51  2 1 2 1 19  0  0 1996 2  1 99 99 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 51  1 . 2 1 55  0  0 1960 3  . 98 98 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 52  3 3 2 1 12  0  0 2003 1  . 98 51 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 53  3 3 2 1 28  0  0 1987 2 99 98 51 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 54  4 3 2 1  3  5  6 2012 1  . 99 53 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 55  4 3 2 1  6 27  4 2009 1  . 99 53 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 56  3 3 2 1 26  0  0 1989 2 99 98 51 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 57  4 3 2 1  9 28  8 2006 1  . 99 56 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 58  4 3 2 1  6 11  5 2009 1  . 99 56 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 59  4 3 2 1  4 22  3 2011 1  . 99 56 1 89.76067 11 1
1102010107 1 2 11020101 "01A" 60  4 3 1 1  0 15  9 2015 1  . 99 56 1 89.76067 11 1
1102010108 1 2 11020101 "01A" 51  1 . 2 1 52  0  0 1963 3  . 98 98 1 89.76067 11 1
1102010108 1 2 11020101 "01A" 52  3 3 2 1 18  0  0 1997 1  . 98 51 1 89.76067 11 1
1102010109 1 2 11020101 "01A"  1  1 . 1 1 62  0  0 1953 2 51 98 98 1 89.76067 11 1
1102010109 1 2 11020101 "01A"  2  3 1 1 1 13 15  3 2002 1  .  1 98 1 89.76067 11 1
1102010109 1 2 11020101 "01A"  3  3 1 1 1 18 24  2 1997 1  .  1 98 1 89.76067 11 1
1102010109 1 2 11020101 "01A" 51  2 1 2 1 44  0  0 1971 2  1 98 98 1 89.76067 11 1
1102010109 1 2 11020101 "01A" 52  3 1 2 1 22 25  4 1993 1  .  1 98 1 89.76067 11 1
1102010109 1 2 11020101 "01A" 53  3 1 2 1 22 25  4 1993 1  .  1 98 1 89.76067 11 1
1102010110 1 2 11020101 "01A"  1  1 . 1 1 57  1  3 1958 2 51 98 98 1 89.76067 11 1
1102010110 1 2 11020101 "01A"  2  3 1 1 1 26 27  4 1989 1  .  1 51 1 89.76067 11 1
1102010110 1 2 11020101 "01A"  3  3 1 1 1 20 23  6 1995 1  .  1 51 1 89.76067 11 1
1102010110 1 2 11020101 "01A"  4  3 1 1 1 10 11  3 2005 1  .  1 51 1 89.76067 11 1
1102010110 1 2 11020101 "01A" 51  2 1 2 1 47  0  0 1968 2  1 98 99 1 89.76067 11 1
1102010110 1 2 11020101 "01A" 52  3 1 2 1 18 25  9 1997 1  .  1 51 1 89.76067 11 1
1102010110 1 2 11020101 "01A" 53  3 1 1 1  6 10 10 2009 1  .  1 51 1 89.76067 11 1
1102010111 1 2 11020101 "01A"  1  1 . 1 1 45  0  0 1970 2 51  4 54 1 89.76067 11 1
1102010111 1 2 11020101 "01A"  2  3 1 1 1 20  0  0 1995 1  .  1 51 1 89.76067 11 1
1102010111 1 2 11020101 "01A"  3  3 1 1 1 17  0  0 1998 1  .  1 51 1 89.76067 11 1
1102010111 1 2 11020101 "01A"  4  5 1 1 1 80  0  0 1935 2 54 98 98 1 89.76067 11 1
1102010111 1 2 11020101 "01A" 51  2 1 2 1 38  0  0 1977 2  1 99 99 1 89.76067 11 1
1102010111 1 2 11020101 "01A" 52  3 1 2 1 15  0  0 2000 1  .  1 51 1 89.76067 11 1
1102010111 1 2 11020101 "01A" 53  3 1 2 1 13  0  0 2002 1  .  1 51 1 89.76067 11 1
1102010111 1 2 11020101 "01A" 54  5 1 2 1 75  0  0 1940 2  4 98 98 1 89.76067 11 1
1102010112 1 2 11020101 "01A"  1  1 . 1 1 50  0  0 1965 2 51 98 56 1 89.76067 11 1
1102010112 1 2 11020101 "01A"  2  3 3 1 1 20  0  0 1995 1  .  1 51 1 89.76067 11 1
1102010112 1 2 11020101 "01A" 51  2 3 2 1 40  0  0 1975 2  1 99 99 1 89.76067 11 1
1102010112 1 2 11020101 "01A" 52  3 3 2 1 14  0  0 2001 1  .  1 51 1 89.76067 11 1
1102010112 1 2 11020101 "01A" 53  3 3 2 1 12  0  0 2003 1  .  1 51 1 89.76067 11 1
1102010112 1 2 11020101 "01A" 54  3 3 2 1  4 15  7 2011 1  .  1 51 1 89.76067 11 1
1102010112 1 2 11020101 "01A" 55  6 3 2 1 38  0  0 1977 1  . 98 56 1 89.76067 11 1
1102010112 1 2 11020101 "01A" 56  5 1 2 1 75  0  0 1940 3  . 98 98 1 89.76067 11 1
1102010113 1 2 11020101 "01A"  1  1 . 1 1 41 16  4 1974 2 51 98 53 1 89.76067 11 1
1102010113 1 2 11020101 "01A"  2  3 1 1 1 14  0  0 2001 1  .  1 51 1 89.76067 11 1
1102010113 1 2 11020101 "01A"  3  3 1 1 1 10 15  6 2005 1  .  1 51 1 89.76067 11 1
1102010113 1 2 11020101 "01A" 51  2 1 2 1 38  0  0 1977 2  1 99 99 1 89.76067 11 1
1102010113 1 2 11020101 "01A" 52  3 1 2 1  8 12  5 2007 1  .  1 51 1 89.76067 11 1
1102010113 1 2 11020101 "01A" 53  5 1 2 1 70  0  0 1945 3  . 98 98 1 89.76067 11 1
1102010114 1 2 11020101 "01A"  1  1 . 1 1 42 23  1 1973 2 51  2 56 1 89.76067 11 1
1102010114 1 2 11020101 "01A"  2  5 1 1 1 64  0  0 1951 2 56 98 59 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 51  2 1 2 1 35  0  0 1980 2  1 98 99 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 52  3 1 2 1 17  0  0 1998 1  .  1 51 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 53  3 1 2 1 14  0  0 2001 1  .  1 51 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 54  3 1 2 1  6 10  5 2009 1  .  1 51 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 55  3 1 1 1  2 22  5 2013 1  .  1 51 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 56  5 1 2 1 64  0  0 1951 2  2 98 98 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 57  6 1 2 1 30  0  0 1985 1  .  2 56 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 58  6 1 2 1 23  0  0 1992 1  .  2 56 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 59 11 1 2 1 90  0  0 1925 3  . 98 98 1 89.76067 11 1
1102010114 1 2 11020101 "01A" 60  3 1 2 1 10  7  8 2005 1  .  1 51 1 89.76067 11 1
1102010115 1 2 11020101 "01A"  1  1 . 1 1 55  0  0 1960 2 51 98 57 1 89.76067 11 1
1102010115 1 2 11020101 "01A"  2  3 1 1 1 28  0  0 1987 2 54  1 51 1 89.76067 11 1
1102010115 1 2 11020101 "01A"  3  3 1 1 1 15  0  0 2000 1  .  1 51 1 89.76067 11 1
1102010115 1 2 11020101 "01A" 51  2 1 2 1 45  0  0 1970 2  1 99 99 1 89.76067 11 1
1102010115 1 2 11020101 "01A" 52  3 1 2 1 21  0  0 1994 1  .  1 51 1 89.76067 11 1
end
label values province province
label def province 1 "kp", modify
label values region region
label def region 2 "urban", modify
label values s1aq02 s1aq02
label def s1aq02 1 "head", modify
label def s1aq02 2 "spouse", modify
label def s1aq02 3 "son/daughter", modify
label def s1aq02 4 "grand child", modify
label def s1aq02 5 "father/mother", modify
label def s1aq02 6 "brother\sister", modify
label def s1aq02 7 "nephew\niece", modify
label def s1aq02 8 "son/daughter-in-law", modify
label def s1aq02 11 "grand father/mother", modify
label values s1aq03 s1aq03
label def s1aq03 1 "main economic provider", modify
label def s1aq03 2 "main provider away for work", modify
label def s1aq03 3 "family elder", modify
label values s1aq04 s1aq04
label def s1aq04 1 "male", modify
label def s1aq04 2 "female", modify
label values s1aq05 s1aq05
label def s1aq05 1 "present", modify
label values s1aq07 s1aq07
label def s1aq07 1 "unmarried/never married", modify
label def s1aq07 2 "currently married", modify
label def s1aq07 3 "widow/widower", modify
label values s1aq11 s1aq11
label def s1aq11 1 "yes", modify
[/CODE]
------------------ copy up to and including the previous line ------------------

Listed 100 out of 157636 observations
Use the count() option to list more