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]

st: RE: AW: strpos: cleaning string variables

From   Nick Cox <>
To   "''" <>
Subject   st: RE: AW: strpos: cleaning string variables
Date   Mon, 21 Feb 2011 20:47:22 +0000

I don't know what the original question was, but -split- might well have featured in a different answer. 


Hanno Scholtz

in a similar task I first saved the blank positions and afterwards split the
string in its components:

  gen     p1=strpos(content,", ")-1;
  gen     p2=strpos(substr(content,p1+3,.),", ")-1;
  gen     p3=strpos(substr(content,p1+p2+5,.),", ")-1;
  gen     p4=strpos(substr(content,p1+p2+p3+7,.),", ")-1;
  gen     p5=strpos(substr(content,p1+p2+p3+p4+9,.),", ")-1;
  gen     p6=strpos(substr(content,p1+p2+p3+p4+p5+11,.),", ")-1;
  replace p1=length(content) if cparts==1;
  replace p2=length(substr(content,p1+3,.)) if cparts==2;
  replace p3=length(substr(content,p1+p2+5,.))  if cparts==3;
  replace p4=length(substr(content,p1+p2+p3+7,.))  if cparts==4;
  replace p5=length(substr(content,p1+p2+p3+p4+9,.))  if cparts==5;
  replace p6=length(substr(content,p1+p2+p3+p4+p5+11,.))  if cparts==6;
  gen     c1=substr(content,1,p1);
  gen     c2=substr(content,p1+3,p2);
  gen     c3=substr(content,p1+p2+5,p3);
  gen     c4=substr(content,p1+p2+p3+7,p4);
  gen     c5=substr(content,p1+p2+p3+p4+9,p5);
  gen     c6=substr(content,p1+p2+p3+p4+p5+11,p6);
  drop p1-p6;

Okay, it won't get a price for elegance, but it worked.

*   For searches and help try:

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