Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

Re: st: Extract parts of a string variable

From   "Roger B. Newson" <>
Subject   Re: st: Extract parts of a string variable
Date   Mon, 05 Nov 2012 11:19:58 +0000

You don't say whether it is possible for more than 1 of the pollutant names you are searching for to be present at the same time. The answer to this question affects the correct way to proceed.

However, if it may be possible for 2 or more pollutant strings to be present, then you should define 1 indicator variable for each pollutant. Assuming that your string variable has the name -messy-, your code might read:

foreach PO in co no2 nox o3 pm10 pm25 {;
 gene byte pres_`PO'=strpos(messy,"`PO'")>0;
 lab var pres_`PO' "Presence of `PO'";
 tab pres_`PO', m;

This will create 6 indicator variables pres_co, pres_no2, pres_nox, pres_o3, pres_pm10 and pres_pm25. You can then use these variables in further processing.

I hope this helps.

Best wishes


Roger B Newson BSc MSc DPhil
Lecturer in Medical Statistics
Respiratory Epidemiology and Public Health Group
National Heart and Lung Institute
Imperial College London
Royal Brompton Campus
Room 33, Emmanuel Kaye Building
1B Manresa Road
London SW3 6LR
Tel: +44 (0)20 7352 8121 ext 3381
Fax: +44 (0)20 7351 8322
Web page:
Departmental Web page:

Opinions expressed are those of the author, not of the institution.

On 05/11/2012 11:05, Kuma Raj wrote:
I have a messy string variable that contains names of various air
pollutants. The contents and naming is based on the name of the
pollutant, lags, station name,
and different exposure metrics. There is no uniformity or fixed
position of the contents in the variable name.  I am interested to
parse the variable and extract the names of the pollutants if they are
specific strings. How can I do that ?

A sample of the variable is found below and I am interested to extract
the following strings:- co, no2, nox, o3, pm10,  and pm25


Thanks in advance
*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index