<?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 CALL SCAN to extract substring and create variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/CALL-SCAN-to-extract-substring-and-create-variables/m-p/580807#M165013</link>
    <description>&lt;P&gt;I have a string which consists of sort ,group and case parameters in random order .Task is to separate the values present in sort , group and case into separate variables and store the respective values present&amp;nbsp; in these parameters . Order of their presence is random and it can be missing in few cases as well.All of the parameters are separated by comma(,) if present and sometimes the parameter is missing and sometimes&amp;nbsp; values are missing as well in parameters&lt;BR /&gt;Tried this using CALL SCAN , but could not get the proper result. Any suggestions how to extract each sub string or any other approach to extract as shown below.&lt;/P&gt;
&lt;P&gt;As below TEXT is the string and variables to be created are SORT, GROUP and CASE and store values in it from the TEXT shown below.&lt;/P&gt;
&lt;TABLE width="703"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;&lt;STRONG&gt;Text&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="168"&gt;sort&amp;nbsp;&lt;/TD&gt;
&lt;TD width="80"&gt;group&lt;/TD&gt;
&lt;TD width="64"&gt;case&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(sort= custname birthdt, group= seqno,case=lower);&lt;/TD&gt;
&lt;TD&gt;custname birthdt&lt;/TD&gt;
&lt;TD&gt;seqno&lt;/TD&gt;
&lt;TD&gt;lower&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(group= seqno);&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;seqno&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(group= Custid,sort =custname );&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(case=upper,group= transid,sort= custname custseq );&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;custname custseq&lt;/TD&gt;
&lt;TD&gt;transid&lt;/TD&gt;
&lt;TD&gt;upper&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(group= ,sort= custname custseq );&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;custname custseq&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have ;&lt;BR /&gt;length text $200.;&lt;BR /&gt;text="function(sort= custname birthdt, group= seqno,case=lower);";output;&lt;BR /&gt;text="function(group= seqno);"; output;&lt;BR /&gt;text="function(group= Custid,sort =custname ); ";output;&lt;BR /&gt;text="function(case=upper,group= transid,sort= custname custseq ); ";output;&lt;BR /&gt;text="function(group= ,sort= custname custseq ); ";output;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Need to create separate variables like group ,case,sort with respective values as shown above using call scan.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 13 Aug 2019 13:48:20 GMT</pubDate>
    <dc:creator>keen_sas</dc:creator>
    <dc:date>2019-08-13T13:48:20Z</dc:date>
    <item>
      <title>CALL SCAN to extract substring and create variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CALL-SCAN-to-extract-substring-and-create-variables/m-p/580807#M165013</link>
      <description>&lt;P&gt;I have a string which consists of sort ,group and case parameters in random order .Task is to separate the values present in sort , group and case into separate variables and store the respective values present&amp;nbsp; in these parameters . Order of their presence is random and it can be missing in few cases as well.All of the parameters are separated by comma(,) if present and sometimes the parameter is missing and sometimes&amp;nbsp; values are missing as well in parameters&lt;BR /&gt;Tried this using CALL SCAN , but could not get the proper result. Any suggestions how to extract each sub string or any other approach to extract as shown below.&lt;/P&gt;
&lt;P&gt;As below TEXT is the string and variables to be created are SORT, GROUP and CASE and store values in it from the TEXT shown below.&lt;/P&gt;
&lt;TABLE width="703"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;&lt;STRONG&gt;Text&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="168"&gt;sort&amp;nbsp;&lt;/TD&gt;
&lt;TD width="80"&gt;group&lt;/TD&gt;
&lt;TD width="64"&gt;case&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(sort= custname birthdt, group= seqno,case=lower);&lt;/TD&gt;
&lt;TD&gt;custname birthdt&lt;/TD&gt;
&lt;TD&gt;seqno&lt;/TD&gt;
&lt;TD&gt;lower&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(group= seqno);&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;seqno&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(group= Custid,sort =custname );&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(case=upper,group= transid,sort= custname custseq );&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;custname custseq&lt;/TD&gt;
&lt;TD&gt;transid&lt;/TD&gt;
&lt;TD&gt;upper&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="391"&gt;function(group= ,sort= custname custseq );&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;custname custseq&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have ;&lt;BR /&gt;length text $200.;&lt;BR /&gt;text="function(sort= custname birthdt, group= seqno,case=lower);";output;&lt;BR /&gt;text="function(group= seqno);"; output;&lt;BR /&gt;text="function(group= Custid,sort =custname ); ";output;&lt;BR /&gt;text="function(case=upper,group= transid,sort= custname custseq ); ";output;&lt;BR /&gt;text="function(group= ,sort= custname custseq ); ";output;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Need to create separate variables like group ,case,sort with respective values as shown above using call scan.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 13:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CALL-SCAN-to-extract-substring-and-create-variables/m-p/580807#M165013</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2019-08-13T13:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: CALL SCAN to extract substring and create variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CALL-SCAN-to-extract-substring-and-create-variables/m-p/580815#M165015</link>
      <description>&lt;P&gt;Let me examine one of your texts:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;"function(sort= custname birthdt, group= seqno,case=lower);"&lt;/PRE&gt;
&lt;P&gt;there are few delimiters: "()" assign the start and the end of the text, "=," separate the variable name from its value/&lt;/P&gt;
&lt;P&gt;I shall use those delimiters to catch the subtexts and assign into the appropriate variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
length text $200.;
text="function(sort= custname birthdt, group= seqno,case=lower);";output;
text="function(group= seqno);"; output;
text="function(group= Custid,sort =custname ); ";output;
text="function(case=upper,group= transid,sort= custname custseq ); ";output;
text="function(group= ,sort= custname custseq ); ";output;
run ;

data want;
 set have;
     length subtext $50; /* adapt to max length */
	 length sort group case $50;
	 
	 text = scan(text,2,"()"); /* omit "function(" and ')' */
	 do i=1 to 3;
	    subtext = scan(text,i,",");
		varname = scan(subtext,1,'=,');
		var_value = scan(subtext,2,'=,');
		if varname = 'sort' then sort = var_value; else
		if varname = 'group' then group = var_value; else
		if varname = 'case' then case = var_value; else 
		if missing(varname) then leave; /* exit loop */
	end;
	drop subtext;
run;
	    &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Aug 2019 14:21:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CALL-SCAN-to-extract-substring-and-create-variables/m-p/580815#M165015</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-08-13T14:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: CALL SCAN to extract substring and create variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CALL-SCAN-to-extract-substring-and-create-variables/m-p/580855#M165032</link>
      <description>&lt;P&gt;Parse the string into NAME/VALUE pairs and use PROC TRANSPOSE to make variables based on the NAME values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data middle;
  set have ;
  obs+1;
  text=scan(text,2,'()');
  do row=1 by 1 until (done);
     next=scan(text,row,',');
     if next=' ' then done=1;
     else do;
       name=scan(next,1,'=');
       value=scan(next,2,'=');
       output;
    end;
  end;
run;

proc transpose data=middle out=want;
  by obs text ;
  id name;
  var value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Aug 2019 15:35:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CALL-SCAN-to-extract-substring-and-create-variables/m-p/580855#M165032</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-13T15:35:59Z</dc:date>
    </item>
  </channel>
</rss>

