title: Loop for extracting strings from multiple variables
body: <p>I am trying to automate the solution provided for the problem described in my previous post:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/50909267/obtain-values-separated-by-hyphens">Obtain values separated by hyphens</a>.</li>
  </ul>
  
  <p>I am having trouble getting the following loop to work correctly for each variable <code>CTSNo</code>:</p>
  
  <pre><code>foreach i in CTSNo1-CTSNo54{
  
  generate tag = 0
  replace tag = 1 if strmatch(CTSNo1, "*-*")
  
  keep if tag == 1
  generate part1 = regexs(0) if regexm(CTSNo1, "([0-9]+)")
  generate part2 = substr(regexs(0), 2, .) if regexm(CTSNo1, "-.*([0-9])")
  
  local obs = _N
  
  forvalues i = 1 / `obs' {
     local xpa = abs(real(part1[`i']) - real(part2[`i'])) + 1
     expand `xpa' if _n == `i'        
  }
  
  bysort SRNo (CTSNo1): egen interim = seq()
  bysort SRNo (CTSNo1): generate NCTSNo1 = real(part1) + interim - 1
  
  drop tag part1 part2 interim
  
  order SRNo SchemeName SchemeAddress ProposalNo CTSNo1 NCTSNo1 CTSNo2  
  
  }  
  </code></pre>
  
  <p>How can I do this?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Example data:</p>
  
  <pre><code>input str30 CTSNo1 float NCTSNo1 str4 CTSNo2 str24 CTSNo3 str20 CTSNo4 
  str13 CTSNo5 str7 CTSNo6 str17 CTSNo7 str11 CTSNo8 str18 CTSNo9 str3 
  CTSNo10
  "455-D"        455 "" " 455-D/2 - 24"  ""               "" "" ""             
  "" ""                ""
  "156-B"        156 "" " 156-B/36 - 57" " 156-B/70 - 89" "" "" " 156-D 
  (pt.)" "" " 156-D/158 - 69" ""
  "938-A (Part)" 938 "" ""               ""               "" "" ""             
  "" ""                ""
  "631 Part - 3" 631 "" ""               ""               "" "" ""             
  "" ""                ""
  "631 Part - 3" 632 "" ""               ""               "" "" ""             
  "" ""                ""
  "631 Part - 3" 633 "" ""               ""               "" "" ""             
  "" ""                ""
  "631 Part - 3" 634 "" ""               ""               "" "" ""             
  "" ""                ""
  "631 Part - 3" 635 "" ""               ""               "" "" ""             
  "" ""                ""
  "631 Part - 3" 636 "" ""               ""               "" "" ""             
  "" ""                ""
  "631 Part - 3" 637 "" ""               ""               "" "" ""             
  "" ""                ""
  "631 Part - 3" 638 "" ""               ""               "" "" ""             
  "" ""                ""
  </code></pre>
  
  <p>I would like each <code>CTSNo</code> to have a <code>NCTSNo</code> following it in order to expand the values. For example, 156-B/36-57 should have separate rows for 156-B/36, 156-B/37, all the way to 156-B/57.</p>
  
  <p>(I'm also not sure why 631 Part - 3 is expanded and would like values like 455-D to retain the -D instead of just being 455.)</p>