title: SPSS split a string variable into two or three separate new variables depending on the content
body: <p>Im trying to split a variable into two or three separate parts but for the life of me I cannot get it to work.</p>
  <p>I want to split a string <code>ADATE10</code> into two parts <code>ADATE</code>/<code>10</code>, or a string <code>F8.2</code> into <code>F</code>/<code>8</code>/<code>2</code>.
  So for example the starting variables are like this:</p>
  <pre><code>+-------------+
  | PrintFormat |
  +-------------+
  | A10         |
  | F4          |
  | ADATE10     |
  | F8.2        |
  | F4.1        |
  | A100        |
  +-------------+
  </code></pre>
  <p>And I want to get to this:</p>
  <pre><code>+-------------+-------+-------+-------+
  | PrintFormat | typeA | typeW | typeD |
  +-------------+-------+-------+-------+
  | A10         | A     | 10    |       |
  | F4          | F     | 4     |       |
  | ADATE10     | ADATE | 10    |       |
  | F8.2        | F     | 8     | 2     |
  | F4.1        | F     | 4     | 1     |
  | A100        | A     | 100   |       |
  +-------------+-------+-------+-------+
  </code></pre>
  <p>My main experience with programming is C# and some VBA macros, so here's the approach that I tried in SPSS. Basically I want to loop backwards through the characters in the string value, when the character is a <code>.</code> then thusfar it was the decimal, when the character is not a NUMBER then thusfar it was the width part, and the rest to the beginning is the datatype indicator:</p>
  <pre><code>* add the new variables
  
  string typeA(a50).
  string typeW(a50).
  string typeD(a50).
  
  * split from the existing variable
  
  string #char(a1).
  string #part(a50).
  compute #part = &quot;&quot;.
  compute #partindex = 1.
  
  loop #pos = char.substr(PrintFormat,#pos,1) to 1.
      compute #char = char.substr(PrintFormat,#pos,1).
  
      do if (#char = &quot;.&quot;).
          compute typeD = #part.
          compute #part = &quot;&quot;.
      else if (MISSING(NUMBER(#char, f4)) AND #partindex = 1).
          compute typeW = #part.
          compute #part = &quot;&quot;.
          compute #partindex = 0.
      end if.
  
      compute #part = concat(#part, #char).
      * COMPUTE #pos = #pos - 1. * ??
  END loop.
  compute typeA = #part.
  execute.
  </code></pre>
  <p>That gave so many errors that I don't even know where to begin, and I can't seem to debug it properly, so instead I tried a simpler approach just using <code>INDEX</code> and <code>REPLACE</code>, like so:</p>
  <pre><code>if (INDEX(PrintFormat, &quot;A&quot;) = 1) COMPUTE typeA = &quot;A&quot;.
  if (INDEX(PrintFormat, &quot;F&quot;) = 1) COMPUTE typeA = &quot;F&quot;.
  if (INDEX(PrintFormat, 'ADATE') = 1) COMPUTE typeA = &quot;ADATE&quot;.
  
  COMPUTE typeW = replace(PrintFormat, typeA, &quot;&quot;).
  
  COMPUTE #posdec = char.index(typeW, &quot;.&quot;).
  if (#posdec &gt; 0) COMPUTE typeD = char.substr(typeW, #posdec, 2).
  
  execute.
  </code></pre>
  <p>but this also gives an error right on the first line</p>
  <blockquote>
  <p>An equals sign was not found when excepected afer a target variable in
  a COMPUTE command.</p>
  </blockquote>
  <p>So after working on it for quite a while I have to give it a rest for now. I can only say that SPSS syntax works much differently then I expected it to. What am I doing wrong here, and how can I split a string value like this into separate bits in SPSS?</p>