# st: RE: generating number series

 From "Nick Cox" To Subject st: RE: generating number series Date Mon, 14 Oct 2002 19:19:02 +0100

```Satyarth Kulshrestha
>
> i need to generate a series of id codes with following protocol:
>  6 digit length
> 1st digit = 1 or 2 [stands for 2 cities from where subjects
> recruited]
> 2nd and 3rd digit = house number[ from 01 to 99]
> 4th digit [= 1 or 2]
> 5th digit [ 1 to 5]
> 6th digit[ 1 or 2]
>
> so the series would start from 101111 and end with 299252.
>
> can this be done in STATA? and subsequently can we
> breakdown this code to
> its constituents, eg, analyze by city or anlyze by last 2
> digits ie only
> thoser case where id ends with 52]?
>

Yes. My advice, once again, is to build this up as
a string variable. There is an alternative route,
building it up as an integer, but it's not
quite so intuitive to many people.

-egen, concat()- would almost do everything
for you, but your house number must be
tackled separately.

You don't say how the variables are stored.
I'll guess everything is numeric.

gen str6 newid = string(city) + string(houseno,"02.0%f")
+ string(whateveritis) + string(somethingelse) +
string(yetanotherthing)

Selection is then a matter of -index()- and -substr()-
e.g. last two digits are "52".

... if substr(newid,-2,2) == "52"

Analysis by city: you have a city variable, presumably.

Nick
n.j.cox@durham.ac.uk

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```