<?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: using % wildcard with substr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682819#M206732</link>
    <description>That should read substr(fx(i),1,4) and substr(fx(i),1,5)</description>
    <pubDate>Thu, 10 Sep 2020 05:19:15 GMT</pubDate>
    <dc:creator>daufoi</dc:creator>
    <dc:date>2020-09-10T05:19:15Z</dc:date>
    <item>
      <title>using % wildcard with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682817#M206730</link>
      <description>&lt;P&gt;I'm trying to subset some data with the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array fx(12) fx1-fx12;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i=1 to 12;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if substr(dx(i),1,4) in ('1115')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; or substr(dx(i),1,5) in ('1146%')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; then output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I cross reference the data output using proc freq to the original dataset. The frequency counts for '1115' matches as they should. They don't for '1146%'). I thought '%' is a wildcard that I can use?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 05:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682817#M206730</guid>
      <dc:creator>daufoi</dc:creator>
      <dc:date>2020-09-10T05:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: using % wildcard with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682819#M206732</link>
      <description>That should read substr(fx(i),1,4) and substr(fx(i),1,5)</description>
      <pubDate>Thu, 10 Sep 2020 05:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682819#M206732</guid>
      <dc:creator>daufoi</dc:creator>
      <dc:date>2020-09-10T05:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: using % wildcard with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682823#M206736</link>
      <description>&lt;P&gt;I would change your Data step as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	DROP  i;
	set have;
	array fx(12) $5 fx1-fx12;

	do i=1 to 12;
		if 	substr(Fx(i),1,4) in ('1115')	OR
			Fx(i) =: ('1146')				THEN
			output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice that I changed your use of &lt;SPAN&gt;substr(Fx(i),1,5) in ('1146%')&lt;/SPAN&gt; to Fx(i) =: ('1146').&amp;nbsp; The use of the colon instructs SAS to evaluate the condition as true for any characters following the specified characters.&amp;nbsp; I think this should give you the wildcard functionality that you're looking for.&amp;nbsp; The "%" you were using before would be taken as a literal if I'm not mistaken.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 06:34:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682823#M206736</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-10T06:34:17Z</dc:date>
    </item>
    <item>
      <title>Re: using % wildcard with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682902#M206753</link>
      <description>&lt;P&gt;Word of caution, if the array contains more than one match you will be outputting replicate rows&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;CODE&gt;%&lt;/CODE&gt; wildcard is recognized by the &lt;CODE&gt;WHERE&lt;/CODE&gt; statement &lt;CODE&gt;LIKE&lt;/CODE&gt; operator. For the &lt;CODE&gt;IF&lt;/CODE&gt; statement you will want to use the string prefix equality (i.e. starts with) operator &lt;CODE&gt;=:&lt;/CODE&gt; or the prefix in set operator &lt;CODE&gt;IN:&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Also, since you are prefix checking only 4 of 5 characters substringed out you could `substring` 4 characters and check with ='1146'. Furthermore, since you are `substr` from position 1 (1st character) you won't need to do &lt;CODE&gt;substr&lt;/CODE&gt; at all (see 3rd example).&lt;/P&gt;
&lt;P&gt;In order to use Perl regular expression pattern matching use the &lt;CODE&gt;PRXMATCH&lt;/CODE&gt; function. The pattern &lt;CODE&gt;/^1146\d*/&lt;/CODE&gt; does not need &lt;CODE&gt;\d*&lt;/CODE&gt; (0 or more digits).&lt;/P&gt;
&lt;P&gt;&lt;CODE&gt;/^1146/'&lt;/CODE&gt; will match anything that &lt;CODE&gt;/^1146\d*/&lt;/CODE&gt; does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example(s) all the same outcome:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;if substr(dx(i),1,4) in ('1115') or fx(i) =: '1146' then output;&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;if substr(dx(i),1,4) in ('1115') or substr(fx(i),1,4) = '1146' then output;
&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;/* expanded example for case of checking two prefix possibilities */
if dx(i) in: ('1115') or fx(i) in: ('1146', '124') then output;&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;if dx(i) =: '1115' or prxmatch('/^1146/', fx(i)) then output;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 11:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682902#M206753</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-09-10T11:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: using % wildcard with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682909#M206755</link>
      <description>&lt;P&gt;You've gotten plenty of good advice so far.&amp;nbsp; Let me add to that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you fix the problem, you could easily be outputting the same observation multiple times.&amp;nbsp; It's at least theoretically possible that multiple diagnosis codes from the same observation will produce a match.&amp;nbsp; Here's a way to save the essential data so you can sort it out later.&amp;nbsp; This will give you the information needed to verify that the counts are coming out correctly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	DROP  i;
	set have;
	array fx(12)  fx1-fx12;
	length diagnosis_category $ 4;
	do i=1 to 12;
		if  Fx(i) in: ('1115', '1146') then do;
		   diagnosis_code = Fx(i);
		   diagnosis_category = Fx(i);
 		   output;
        end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Sep 2020 11:46:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682909#M206755</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-09-10T11:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: using % wildcard with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682941#M206768</link>
      <description>&lt;P&gt;That's a good point that you could have multiple rows in your output if you get more than one match.&amp;nbsp; There's a reasonably simple way to make sure that you only get one row in you output per one row in your input:&amp;nbsp; Set a flag when you get a match and only perform an OUTPUT after all values have been checked.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Putting it all together:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data	want;
	DROP	_:;
	set		have;
	array	fx(12)	$5	fx1 - fx12;
	Match	=	0;
	do	_i	=	1	to	12;
		if	Fx(_i) IN: ('1115' '1146')	THEN
			Match	=	1;
	end;
	IF	Match	THEN
		output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above should give you a reasonably compact notation, the correct results including wildcard functionality, and one and only one output row for each input row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The cool thing here is the colon operator.&amp;nbsp; The equals sign followed by the colon acts as a wildcard.&amp;nbsp; Notice also that I've changed my DROP statement such that it now contains a colon.&amp;nbsp; Coded this way, all variables starting with an underscore will be dropped&amp;nbsp;&lt;EM&gt;without&lt;/EM&gt; having to name them variable by variable.&amp;nbsp; In any program that has this DROP statement, all I have to do to make a variable temporary (i.e. not written out as part of the Data statement) is to prefix it with an underscore.&amp;nbsp; I find this bit of code very handy in a program with a lot of intermediate work variables.&amp;nbsp; A word of caution:&amp;nbsp; Some SAS procedure (e.g. Proc Compare) produce variables prefixed with an underscore.&amp;nbsp; If you want to keep those variables, you have to change to two underscores (DROP __:;) or something along those lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 14:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-wildcard-with-substr/m-p/682941#M206768</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-10T14:13:53Z</dc:date>
    </item>
  </channel>
</rss>

