[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: substringing long, varying length text variables into individual variables

From   Svend Juul <SJ@SOCI.AU.DK>
Subject   Re: st: substringing long, varying length text variables into individual variables
Date   Thu, 3 Apr 2008 10:27:11 +0200

Todd wrote:

I have data from a publicly available database ( 
This database has a number of text variables that I want to break 
into individual variables and I could use some help.

For example, one of the variables is called study designs. Here are 
some data from the study designs variable

Treatment|Randomized|Double-Blind|Placebo Control|Parallel
Assignment|Safety/Efficacy Study
Prevention|Randomized|Open Label|Active Control|Parallel
Assignment|Bio-equivalence Study

What I want to do is parse this text using the "|" into individual 

So the first case would be
des1 des2 des3 des4 des5 des6
Treatment Randomized Double-Blind Placebo Control Parallel Assignment 
Safety/Efficacy Study

I can think of a brute force way where I save this variable and my 
id variable, change | to a comma, output as text, read the text into 
stata as a comma separated file, and then merge it back into my data. 
Sounds silly, but perhaps it is the easiest. Any other ideas?


The -insheet- suggestion is valid, I believe. Another possibility is
the -split- command:

input str150 longvar
"Treatment|Randomized|Double-Blind|Placebo Control|Parallel
Assignment|Safety/Efficacy Study"
"Prevention|Randomized|Open Label|Active Control|Parallel
Assignment|Bio-equivalence Study"
"Prevention|Randomized|Double Blind (Subject, Caregiver, Investigator,
Outcomes Assessor)|Crossover Assignment"
"Randomized|Single Blind|Active Control|Parallel Assignment"
"Natural History|Cross-Sectional|Case Control|Prospective Study"
"Treatment|Randomized|Open Label|Active Control|Parallel
Assignment|Efficacy Study"
"Treatment|Randomized|Double-Blind|Placebo Control|Single Group
Assignment|Safety/Efficacy Study"
"Treatment|Randomized|Open Label|Placebo Control|Parallel
Assignment|Safety/Efficacy Study"
"Treatment|Randomized|Double-Blind|Active Control|Parallel
Assignment|Safety/Efficacy Study"
"Prevention|Randomized|Double-Blind|Placebo Control|Parallel
Assignment|Safety/Efficacy Study"
"Treatment|Randomized|Single Blind (Investigator)|Placebo
Control|Parallel Assignment"
"Treatment|Randomized|Open Label|Active Control|Parallel
Assignment|Efficacy Study"

split longvar , generate(des) parse("|") limit(6)

Hope this helps


Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000  Aarhus C, Denmark
Phone:  +45 8942 6090
Home:   +45 8693 7796

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index