Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: working with a 24-character string variable consisting of 0s and 1s

From   Jorge Eduardo Pérez Pérez <>
To   "" <>
Subject   Re: st: working with a 24-character string variable consisting of 0s and 1s
Date   Mon, 10 Feb 2014 22:08:03 -0500

A not very elegant solution: break the variable into pieces.

input str24 x
forv i=1(1)24 {
gen p`i'=substr(x,`i',1)
destring p`i', replace

egen year1=rowtotal(p1-p12)
egen year2=rowtotal(p13-p24)
gen total=year1 + year2
drop p*

Jorge Eduardo Pérez Pérez
Graduate Student
Department of Economics
Brown University

On Mon, Feb 10, 2014 at 9:46 PM, Lisa Cook <> wrote:
> Hi,
> I need help working with a cumbersome string variable. I'm using Stata/MP 13.0.
> I've inherited a dataset that includes several variables indicating
> the number of months each person had specific kinds of health
> insurance (Medicaid, Medicare, private, etc.).
> The variables are 24 characters long in string format. Each character
> is either a 0 or 1, and represents whether the person had coverage in
> that month. So, if one of these variables equals
> "000000000000000000000000", the person had no coverage in any month of
> that type, while if it equals "111111111111111111111111", they were
> covered in every month by that kind of insurance. If the variable
> equals, say, "101111111111111111111111", the person had 23 months of
> coverage, but no coverage in the 2nd month.
> I would like to use these variables to generate, for each kind of
> insurance, the total in year 1, the total in year 2, and the total
> number of months of coverage in both years.
> I've used regexm before, but I can't figure out how to apply that code
> to my situation. I'd be very grateful if anyone could suggest some
> options.
> Thanks so much,
> Lisa
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index