<?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: How to parse characters between two different delimiters? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/485041#M125979</link>
    <description>&lt;P&gt;The problem is that Variable is empty, but you are telling Substr to extract on character, so it throws an error. You should include a check for Variable being empty.&lt;/P&gt;</description>
    <pubDate>Wed, 08 Aug 2018 08:13:09 GMT</pubDate>
    <dc:creator>rudfaden</dc:creator>
    <dc:date>2018-08-08T08:13:09Z</dc:date>
    <item>
      <title>How to parse characters between two different delimiters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484641#M125827</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with two variables. The first variable is a unique ID for each observation, and the second is a character variable. I am trying to use function SCAN to decompose each value of the second variable into multiple separate observations. Below is an example of the original value:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;[2011-12-12--2014-04-04] Moudy El Khodr;[2016-10-01--] Laurent van Tuyckom;[2014-04-04--2016-10-01] Ignace de Coene;[2014-04-04--2016-01-01] Alexander Roose;[2016-10-01--] Alexander Roose;[2014-04-04--2015-03-01] Koen Bosquet;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to obtain all characters between each delimiter&lt;/P&gt;&lt;PRE&gt;[&lt;/PRE&gt;&lt;P&gt;and delimiter&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;as a piece of information, and save&amp;nbsp;it into an observation&amp;nbsp;with the same ID. Thus, for this case, I want to&amp;nbsp;convert the original observation into 6 separate observations. Different IDs might have different numbers of pieces of information, and I am using function&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;COUNTW(variable2, '[')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;to generate this number.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I read through the guide for function SCAN, but now my problem is that, how can I parse information between two specific and different delimiters?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Aug 2018 07:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484641#M125827</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2018-08-07T07:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse characters between two different delimiters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484644#M125828</link>
      <description>&lt;P&gt;You might want to try using regex.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
variable="[2011-12-12--2014-04-04] Moudy El Khodr;"; output;
variable="[2016-10-01--] Laurent van Tuyckom;"; output;
variable="[2014-04-04--2016-10-01] Ignace de Coene;"; output;
variable="[2014-04-04--2016-01-01] Alexander Roose;"; output;
variable="[2016-10-01--] Alexander Roose;"; output;
variable="[2014-04-04--2015-03-01] Koen Bosquet;"; output;
run;


data want;
set have;
	newString=prxchange('s/\[(.*);/$1/', -1, variable);	
run;
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Aug 2018 08:28:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484644#M125828</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2018-08-07T08:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse characters between two different delimiters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484647#M125830</link>
      <description>The six pieces of information were stored in as one value of the second variable in original data. If they were already separate as in your dataset "have", I am done.</description>
      <pubDate>Tue, 07 Aug 2018 08:34:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484647#M125830</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2018-08-07T08:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse characters between two different delimiters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484648#M125831</link>
      <description>&lt;P&gt;Well you can use perl regex, or datastep:&lt;/P&gt;
&lt;PRE&gt;data want;
  length str s new_wrd $2000;
  str="[2011-12-12--2014-04-04] Moudy El Khodr;[2016-10-01--] Laurent van Tuyckom;[2014-04-04--2016-10-01] Ignace de Coene;[2014-04-04--2016-01-01] Alexander Roose;[2016-10-01--] Alexander Roose;[2014-04-04--2015-03-01] Koen Bosquet;";
  s=str;
  do while(lengthn(s));
    new_wrd=substr(s,1,findc(s,";"));
    s=substr(s,findc(s,";")+1);
    output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;If you wanted you could replace the output with a further parse of the individual components.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Aug 2018 08:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484648#M125831</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-07T08:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse characters between two different delimiters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484664#M125836</link>
      <description>&lt;P&gt;Ahh. Okay. I misunderstood your problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
variable="[2011-12-12--2014-04-04] Moudy El Khodr;[2016-10-01--] Laurent van Tuyckom;[2014-04-04--2016-10-01] Ignace de Coene;[2014-04-04--2016-01-01] Alexander Roose;[2016-10-01--] Alexander Roose;[2014-04-04--2015-03-01] Koen Bosquet;";
run;

data want;
set have;
num=1;
start=1;
	do while (1);
		num=findc(variable,';',start); 
		if num = 0 then leave;
		variable2=substr(variable,start,num-start+1); output;
		start=num+1;
	end;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Aug 2018 09:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/484664#M125836</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2018-08-07T09:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse characters between two different delimiters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/485035#M125975</link>
      <description>&lt;P&gt;Thanks for your help. I am trying to use your code to process the dataset, which includes observations with missing values for the variable. Here is a sample and my modified version of your code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length ID $3 variable $500;
ID = "001"; variable = "[2011-12-12--2014-04-04] David Tien;"; output;
ID = "002"; variable="[2011-12-12--2014-04-04] Moudy El Khodr;[2016-10-01--] Laurent van Tuyckom;[2014-04-04--2016-10-01] Ignace de Coene;[2014-04-04--2016-01-01] Alexander Roose;[2016-10-01--] Alexander Roose;[2014-04-04--2015-03-01] Koen Bosquet;"; output;
ID = "003"; variable = ""; output;
run;

data want;
set have;
	num_person = countw(variable, "[");
	if num_mgr = 0 then do;
		varible2 = ""; output;
	end;

	if num_person &amp;gt; 0 then do;
		num_find = 1;
		start = 1;
		i = 1;
		do while (i &amp;lt;= num_person);
			num_find =findc(variable, ';', start); 
			variable2=substr(variable, start, num_find - start + 1); output;
			start=num_find + 1;
			i+1;
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It seems to be doing what I need, but the log shows some error:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P class="p1"&gt;NOTE: Invalid third argument to function SUBSTR at line 1200 column 23.&lt;/P&gt;&lt;P class="p2"&gt;ID=003 variable=&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;num_mgr=1 varible2=&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;num_find=0 start=1 i=2 variable2=&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;_ERROR_=1 _N_=3&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P class="p2"&gt;Did I make any mistake?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 07:30:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/485035#M125975</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2018-08-08T07:30:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse characters between two different delimiters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/485041#M125979</link>
      <description>&lt;P&gt;The problem is that Variable is empty, but you are telling Substr to extract on character, so it throws an error. You should include a check for Variable being empty.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 08:13:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-characters-between-two-different-delimiters/m-p/485041#M125979</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2018-08-08T08:13:09Z</dc:date>
    </item>
  </channel>
</rss>

