<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Splitting a character value into several variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314486#M68503</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/68272"&gt;@knveraraju91﻿&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regardless of the issue of a blank in position 200 vs 201, it appears you're doing a lot of work for a straightforward task.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your strategy is to count words, and sum up their cumulative lengths (plus a blank per word) to know when words will be assigned to a new group.&amp;nbsp; Then, for each group, you concatenate those words into the new OUTPUT variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider this strategy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Use the COMPBL function to remove excess internal blanks (i.e. double blanks become single blanks) in VALUE.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Instead of counting words, why not start with a txt length (variable TXTLEN) of 200, beginning with character 1 (variable STARTCHR=1) and ending with character 200 and see if it is immediately followed by a blank (i.e. look at column 201=STARTCHR+TXTLEN for a blank).&amp;nbsp; If it's not a blank, reduce txt length&amp;nbsp;by 1 and see if it is followed by a blank.&amp;nbsp; Once a blank has been found copy all the text from STARTCHR and length TXTLEN to value1.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;Then for value2, increment STARTCHR by TXTLEN+1 (i.e. point at first unproceessed non-blank),&amp;nbsp;look for blanks at position STARTCHR+200&amp;nbsp;and work backward until one is found.&amp;nbsp; Copy the text from the new STARTCHR for a length of the new TXTLEN to value2.&lt;BR /&gt;&lt;BR /&gt;Etc. Etc.&lt;BR /&gt;&lt;BR /&gt;If startchr finally get incremented beyond length of value, then no further work is needed - stop the loop.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;BR /&gt;This is a lot less programming than counting words and concatenating them 1 at a time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;That strategy is in the loop below:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want (drop=v startchr txtlen);

  array val{4} $200 value1-value4;
  set data;
  n+1;
  value=compbl(value);  *remove excess blanks *;
  startchr=1;
  do v=1 to dim(val) until (startchr&amp;gt;length(value));
&lt;BR /&gt;    /* Nothing inside this do loop since we only want it to stop when a blank is found*/
    do txtlen=200 to 1 by -1 while(notspace(char(value,startchr+txtlen)));
    end;
&lt;BR /&gt;    /* copy from startchr for length txtlen to value1, value2, value3, value4*/
    val{v}=substr(value,startchr,txtlen);
    startchr=startchr+txtlen+1;
  end;
run;    
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 26 Nov 2016 23:51:26 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2016-11-26T23:51:26Z</dc:date>
    <item>
      <title>Splitting a character value into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314398#M68464</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;&lt;P&gt;I posted this question today morning. The subject that I gave is different than what I posted. That's why I am posting again. Sorry.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my data the following value is present. My code gave the output i need for all obs except for this value. With my code the value is split into five variables rather than four. In the vendor data, the value is split into 4 variables. I analysed it. I found in the second variable('value2' in the output needed), the length reaches exactly 200. With my code it truncates before. Please help in my code. Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;value&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The OOO re-trained the site staff on re-conoooting current/active suooocts under the updated main OOO at screening. The site staff printed out the updated OOO and included it in the sooject source documents with a note to use the OOO at the subject's next site visit. The site staff created a note-to-file and reported this doooation to the OOO. The site staff will obtain the subject's re-consent during their next visit for the Oooo 00-000 study. The OOO will verify the OOO has been completed at the next OOO. The OOO reminded the site staff to work with the subject at their next Oooo 00-000 visit to obtain the missing consent signature, otherwise the site staff should document/update their note to file in source to reflect in the inability to obtain the signature (update 00JON2000).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;code&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data one;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;length temp $200;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;set data;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;n+1;&lt;BR /&gt;do i=1 to countw(value,'');&lt;BR /&gt;temp=scan(value,i,' ');len=length(temp)+1;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data two;&lt;BR /&gt;set one;&lt;BR /&gt;by n;&lt;BR /&gt;retain sum;&lt;BR /&gt;if first.n then sum=0;&lt;BR /&gt;sum+len;&lt;BR /&gt;if sum gt 200 then do;group+1;sum=len;end;&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data three;&lt;BR /&gt;length output $ 200;&lt;BR /&gt;do until(last.group);&lt;BR /&gt;set two;&lt;BR /&gt;by n group;&lt;BR /&gt;output=catx(' ',output,temp);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;output needed;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;value1&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The OOO re-trained the site staff on re-consenting current/active subjects under the updated main OOO at screening. The site staff printed out the updated OOO and included it in the subject source &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;value2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;documents with a note to use the OOO at the subject's next site visit. The site staff created a note-to-file and reported this deviation to the OOO. The site staff will obtain the subject's re-consent&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;value3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;during their next visit for the Oooo 00-000 study. The OOO will verify the OOO has been completed at the next OOO. The OOO reminded the site staff to work with the subject at their next Oooo 00-000&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;value4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;visit to obtain the missing consent signature, otherwise the site staff should document/update their note to file in source to reflect in the inability to obtain the signature (update 00JON2000).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;output getting;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;value1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The OOO re-trained the site staff on re-consenting current/active subjects under the updated main OOO at screening. The site staff printed out the updated OOO and included it in the subject source&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;value2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;documents with a note to use the OOO at the subject's next site visit. The site staff created a note-to-file and reported this deviation to the OOO. The site staff will obtain the subject's&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;value3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;re-consent during their next visit for the Oooo 00-000 study. The OOO will verify the OOO has been completed at the next OOO. The CRA reminded the site staff to work with the subject at their next&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;value4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Oooo 00-000 visit to obtain the missing consent signature, otherwise the site staff should document/update their note to file in source to reflect in the inability to obtain the signature (update&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;value5&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;21JUN2016).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2016 01:33:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314398#M68464</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-11-26T01:33:32Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a character value into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314411#M68470</link>
      <description>&lt;PRE&gt;
Try this:

if sum gt 200 then do;group+1;sum=len;end;

---&amp;gt;

if sum gt 201 then do;group+1;sum=len;end;

&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Nov 2016 03:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314411#M68470</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-26T03:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a character value into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314418#M68473</link>
      <description>&lt;P&gt;The reason behind&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;'s solution is that when the sum is 201, the last character counted is a space, so the real length of the reassembled substring will be 200.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2016 04:35:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314418#M68473</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-11-26T04:35:24Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a character value into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314486#M68503</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/68272"&gt;@knveraraju91﻿&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regardless of the issue of a blank in position 200 vs 201, it appears you're doing a lot of work for a straightforward task.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your strategy is to count words, and sum up their cumulative lengths (plus a blank per word) to know when words will be assigned to a new group.&amp;nbsp; Then, for each group, you concatenate those words into the new OUTPUT variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider this strategy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Use the COMPBL function to remove excess internal blanks (i.e. double blanks become single blanks) in VALUE.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Instead of counting words, why not start with a txt length (variable TXTLEN) of 200, beginning with character 1 (variable STARTCHR=1) and ending with character 200 and see if it is immediately followed by a blank (i.e. look at column 201=STARTCHR+TXTLEN for a blank).&amp;nbsp; If it's not a blank, reduce txt length&amp;nbsp;by 1 and see if it is followed by a blank.&amp;nbsp; Once a blank has been found copy all the text from STARTCHR and length TXTLEN to value1.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;Then for value2, increment STARTCHR by TXTLEN+1 (i.e. point at first unproceessed non-blank),&amp;nbsp;look for blanks at position STARTCHR+200&amp;nbsp;and work backward until one is found.&amp;nbsp; Copy the text from the new STARTCHR for a length of the new TXTLEN to value2.&lt;BR /&gt;&lt;BR /&gt;Etc. Etc.&lt;BR /&gt;&lt;BR /&gt;If startchr finally get incremented beyond length of value, then no further work is needed - stop the loop.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;BR /&gt;This is a lot less programming than counting words and concatenating them 1 at a time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;That strategy is in the loop below:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want (drop=v startchr txtlen);

  array val{4} $200 value1-value4;
  set data;
  n+1;
  value=compbl(value);  *remove excess blanks *;
  startchr=1;
  do v=1 to dim(val) until (startchr&amp;gt;length(value));
&lt;BR /&gt;    /* Nothing inside this do loop since we only want it to stop when a blank is found*/
    do txtlen=200 to 1 by -1 while(notspace(char(value,startchr+txtlen)));
    end;
&lt;BR /&gt;    /* copy from startchr for length txtlen to value1, value2, value3, value4*/
    val{v}=substr(value,startchr,txtlen);
    startchr=startchr+txtlen+1;
  end;
run;    
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2016 23:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314486#M68503</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-11-26T23:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a character value into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314487#M68504</link>
      <description>&lt;P&gt;Well done &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest moving the startchr=1 statement before the loop and replacing&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;while&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;value&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;startchr&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;txtlen&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;^=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;' '&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;with&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;while(notspace(char(value, startchr + txtlen)))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Nov 2016 21:17:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314487#M68504</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-11-26T21:17:29Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a character value into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314488#M68505</link>
      <description>Thx&lt;BR /&gt;  Will do when I have a real keyboard.</description>
      <pubDate>Sat, 26 Nov 2016 21:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314488#M68505</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-11-26T21:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a character value into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314550#M68526</link>
      <description>&lt;P&gt;Or based on&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz﻿&lt;/a&gt;&amp;nbsp;reformulation of the problem here an approach using a RegEx.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=_:);
  length source_var $800;
  do _char='a','b','c','d';
    do until(lengthn(source_var)&amp;gt;=620);
      source_var=catx(' ',source_var,repeat(_char,ceil(ranuni(1)*10)));
    end;
    output;
    call missing(source_var);
  end;
  stop;
run;

data want(drop=_:);
  array target_var (4) $200;
  retain _re;
  if _n_=1 then
    _re=prxparse('/^\s*(.{1,200})\s+(.{1,200})\s+(.{1,200})\s+(.{1,200})\s*$/o');

  set have;
  if prxmatch(_re, source_var) then
    do _i=1 to dim(target_var);
      target_var[_i]=prxposn(_re, _i, source_var);
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Nov 2016 06:38:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-character-value-into-several-variables/m-p/314550#M68526</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-11-27T06:38:49Z</dc:date>
    </item>
  </channel>
</rss>

