<?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 String/Text Functions(Scan and Countw and Looping) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639776#M190395</link>
    <description>&lt;P&gt;I have a string as below&amp;nbsp; with variable names embedded in square brackets and plain text in quotes.Need to apply the coalescec function for identifying the first non missing values. I have used the do loop , SCAN and COUNTW function for identifying the variables , separating and using a strip for each variable and for differentiation of the variable names and text in quotes. Is there any simpler way to perform with one/two line of code to get the required output as shown. HAVE is the column with the string and WANT is the OUTPUT&amp;nbsp; in simple steps.Variables and Text can be in any order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;HAVE&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;WANT&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;(values=[DECOD]&amp;nbsp;[TEXT])&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;VAR=coalescec(strip(DECOD),strip(TEXT))&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;(values=[DECOD]&amp;nbsp;[TEXT] "NOT REPORTED")&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;VAR=coalescec(strip(DECOD),&amp;nbsp;strip(TEXT), "NOT REPORTED")&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;(values= "NOT REPORTED" [DECOD]&amp;nbsp; "Zero data"[TEXT] )&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;VAR=coalescec("NOT REPORTED",strip(DECOD), "zero data",strip(TEXT) )&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;(values= [TEXT] "Not Reported,NOW" [DECOD]&amp;nbsp; )&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;VAR=coalescec (strip(TEXT) "Not REPORTED,Now" , strip(DECOD) )&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
    <pubDate>Tue, 14 Apr 2020 14:27:06 GMT</pubDate>
    <dc:creator>keen_sas</dc:creator>
    <dc:date>2020-04-14T14:27:06Z</dc:date>
    <item>
      <title>String/Text Functions(Scan and Countw and Looping)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639776#M190395</link>
      <description>&lt;P&gt;I have a string as below&amp;nbsp; with variable names embedded in square brackets and plain text in quotes.Need to apply the coalescec function for identifying the first non missing values. I have used the do loop , SCAN and COUNTW function for identifying the variables , separating and using a strip for each variable and for differentiation of the variable names and text in quotes. Is there any simpler way to perform with one/two line of code to get the required output as shown. HAVE is the column with the string and WANT is the OUTPUT&amp;nbsp; in simple steps.Variables and Text can be in any order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;HAVE&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;WANT&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;(values=[DECOD]&amp;nbsp;[TEXT])&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;VAR=coalescec(strip(DECOD),strip(TEXT))&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;(values=[DECOD]&amp;nbsp;[TEXT] "NOT REPORTED")&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;VAR=coalescec(strip(DECOD),&amp;nbsp;strip(TEXT), "NOT REPORTED")&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;(values= "NOT REPORTED" [DECOD]&amp;nbsp; "Zero data"[TEXT] )&lt;/TD&gt;
&lt;TD width="50%"&gt;&lt;STRONG&gt;VAR=coalescec("NOT REPORTED",strip(DECOD), "zero data",strip(TEXT) )&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;(values= [TEXT] "Not Reported,NOW" [DECOD]&amp;nbsp; )&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;VAR=coalescec (strip(TEXT) "Not REPORTED,Now" , strip(DECOD) )&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 14 Apr 2020 14:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639776#M190395</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2020-04-14T14:27:06Z</dc:date>
    </item>
    <item>
      <title>Re: String/Text Functions(Scan and Countw and Looping)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639783#M190397</link>
      <description>&lt;P&gt;So you want to create code from strings stored in a dataset?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 14:36:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639783#M190397</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-14T14:36:57Z</dc:date>
    </item>
    <item>
      <title>Re: String/Text Functions(Scan and Countw and Looping)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639787#M190399</link>
      <description>&lt;P&gt;If you can already parse it aren't you just asking to make branch based on whether the current token starts with square backet or double quote?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 14:53:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639787#M190399</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-14T14:53:57Z</dc:date>
    </item>
    <item>
      <title>Re: String/Text Functions(Scan and Countw and Looping)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639802#M190402</link>
      <description>&lt;P&gt;If your source is as messy as your example then you need to something to put delimiters between the tokens.&lt;/P&gt;
&lt;P&gt;So perhaps like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input have $80.;
cards4;
(values=[DECOD] [TEXT])	
(values=[DECOD] [TEXT] "NOT REPORTED")	
(values= "NOT REPORTED" [DECOD]  "Zero data"[TEXT] )	
(values= [TEXT] "Not Reported,NOW" [DECOD]  )	
;;;;

data want;
  set have;
  length word want $80 ;
  have=tranwrd(have,'][','] [');
  have=tranwrd(have,'"[','" [');
  have=tranwrd(have,']"','] "');
  want='VAR=coalescec';
  sep='(';
  do index=2 to countw(have,'= )','q');
    word=scan(have,index,'= )','q');
    if word=:'[' then want=catx(sep,want,cats('strip(',scan(word,1,'[]'),')'));
    else want=catx(sep,want,word);
    sep=',';
  end;
  want=cats(want,')');
  drop word index sep;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    have

 1     (values=[DECOD] [TEXT])
 2     (values=[DECOD] [TEXT] "NOT REPORTED")
 3     (values= "NOT REPORTED" [DECOD]  "Zero data" [TEXT] )
 4     (values= [TEXT] "Not Reported,NOW" [DECOD]  )

Obs    want

 1     VAR=coalescec(strip(DECOD),strip(TEXT))
 2     VAR=coalescec(strip(DECOD),strip(TEXT),"NOT REPORTED")
 3     VAR=coalescec("NOT REPORTED",strip(DECOD),"Zero data",strip(TEXT))
 4     VAR=coalescec(strip(TEXT),"Not Reported,NOW",strip(DECOD))
&lt;/PRE&gt;
&lt;P&gt;But notice that the resulting code looks a little strange.&amp;nbsp; Once you have a fixed text string, like in 3rd and 4th observations there isn't really any point in having the other arguments to COALESCEC().&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 15:21:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639802#M190402</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-14T15:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: String/Text Functions(Scan and Countw and Looping)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639807#M190405</link>
      <description>&lt;PRE&gt;data have;
length x $200;
x="(values=[DECOD] [TEXT])"; output;
x='(values=[DECOD] [TEXT] "NOT REPORTED")'; output;
x='(values= "NOT REPORTED" [DECOD]  "Zero data" [TEXT] )';output;
x='(values= [TEXT] "Not Reported,NOW" [DECOD]  )';output;
;
run;
 data want;  
set have;
length y $200;
y= prxchange('s/\[/strip(/',-1, x);
y= prxchange('s/\]/),/',-1, y);
y=prxchange('s/\(values=/VAR=COALESCEC(/',-1, y);
y=prxchange('s/" s/",s/',-1, y);
y=prxchange('s/,\)/)/',-1, y);
y=prxchange('s/, \)/)/',-1, y);
y=prxchange('s/,  \)/)/',-1, y);
run; &lt;/PRE&gt;&lt;P&gt;Hope can help you&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 15:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639807#M190405</guid>
      <dc:creator>kelxxx</dc:creator>
      <dc:date>2020-04-14T15:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: String/Text Functions(Scan and Countw and Looping)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639814#M190408</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data want;  
set have;
length y $200;
y= prxchange('s/\[/strip(/',-1, x);
y= prxchange('s/\]/),/',-1, y);
y=prxchange('s/\(values=/VAR=COALESCE(/',-1, y);
y=prxchange('s/" *s/",s/',-1, y);
y=prxchange('s/, *\)/)/',-1, y);
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;a little modification&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 15:59:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-Text-Functions-Scan-and-Countw-and-Looping/m-p/639814#M190408</guid>
      <dc:creator>kelxxx</dc:creator>
      <dc:date>2020-04-14T15:59:32Z</dc:date>
    </item>
  </channel>
</rss>

