<?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: sort observations starting with x in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678312#M23841</link>
    <description>&lt;P&gt;I fully agree to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226241"&gt;@AMSAS&lt;/a&gt;: This is the job for a regex.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please have a look at:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   if prxmatch('/\b2775/', catx(' ', of dx1-dx3));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 21 Aug 2020 05:10:09 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2020-08-21T05:10:09Z</dc:date>
    <item>
      <title>sort observations starting with x</title>
      <link>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678195#M23827</link>
      <description>&lt;P&gt;Hi all -&lt;/P&gt;&lt;P&gt;I have a data set with variables dx1, dx2, dx3...dx25 (example below).&amp;nbsp; I only want to keep observations with a value that begins with 2775 (that would be #1, 2, and 4). Note that not all values are the same length.&amp;nbsp; Is there a way to do this without regex??&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Conversely, how do I exclude values? For example, take out observations that have values starting with 727 (#2).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data;&lt;BR /&gt;input dx1 dx2 dx3;&lt;BR /&gt;cards;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;27756&lt;/FONT&gt;  826  79883&lt;BR /&gt;78399  &lt;FONT color="#FF0000"&gt;2775&lt;/FONT&gt;  7274&lt;BR /&gt;99462  62294  677&lt;BR /&gt;48268  54981  &lt;FONT color="#FF0000"&gt;27752&lt;/FONT&gt;&lt;BR /&gt;18872  992  27274&lt;BR /&gt;;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Right now, all I have is a very clunky brute force method. It works but it's a pain in the butt every time I want to add or subtract a new criteria.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data sorted;&lt;BR /&gt;set data;&lt;BR /&gt;if dx1 in ('2775', '27751, '27752', '27753, '27754', '27755', '27756', '27757', '27758', '27759')&lt;BR /&gt;if dx2 in ('2775', '27751, '27752', '27753, '27754', '27755', '27756', '27757', '27758', '27759')&lt;BR /&gt;or dx3 in ('2775', '27751, '27752', '27753, '27754', '27755', '27756', '27757', '27758', '27759')&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 18:02:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678195#M23827</guid>
      <dc:creator>jsheu</dc:creator>
      <dc:date>2020-08-20T18:02:31Z</dc:date>
    </item>
    <item>
      <title>Re: sort observations starting with x</title>
      <link>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678222#M23828</link>
      <description>&lt;P&gt;Is this what you are looking for?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p1vz3ljudbd756n19502acxazevk.htm&amp;amp;locale=en" target="_self"&gt;Using Perl Regular Expressions in the DATA Step&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
input dx1 $ dx2 $ dx3 $;
cards;
27756  826  79883
78399  2775  7274
99462  62294  677
48268  54981  27752
18872  992  27274
12345  12345 127750
;


data want ;
	set have ;
	x=prxmatch('/2775/', dx1) ;
	y=prxmatch('/2775/', dx2) ;
	z=prxmatch('/2775/', dx3) ;
	a=sum(x,y,z) ;
	put a= x= y= z=;
	if a=0 then
		delete ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Aug 2020 19:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678222#M23828</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2020-08-20T19:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: sort observations starting with x</title>
      <link>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678223#M23829</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input dx1 dx2 dx3;
cards;
27756  826  79883
78399  2775  7274
99462  62294  677
48268  54981  27752
18872  992  27274
; run;

data want;
 set have;
     array dx dx1-dx3;
     do i=1 to dim(dx);
        numx = strip(put(dx(i),8.));
        if length(numx) ge 4 and
           substr(numx,1,4) = '2775' then flag=1;
     end;
     if flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Aug 2020 19:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678223#M23829</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-20T19:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: sort observations starting with x</title>
      <link>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678225#M23830</link>
      <description>&lt;P&gt;Ok now I see your comment "&lt;SPAN&gt;Is there a way to do this without regex??"&amp;nbsp;&lt;BR /&gt;Why would you not use RegEx it's the easy way to solve this problem&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 19:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678225#M23830</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2020-08-20T19:16:55Z</dc:date>
    </item>
    <item>
      <title>Re: sort observations starting with x</title>
      <link>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678228#M23831</link>
      <description>&lt;P&gt;Perhaps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input dx1 dx2 dx3;
cards;
27756  826  79883
78399  2775  7274
99462  62294  677
48268  54981  27752
18872  992  27274
;

data want;
   set  have;
   array d dx: ;
   do i=1 to dim(d);
      if put(d[i],best12. -L)=: '2775' then do;
         output;
         leave;
      end;
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;Almost any time you need to do the same thing with multiple variables an Array is a likely tool so you can write code that works for one instance and then use all the values in the array.&lt;/P&gt;
&lt;P&gt;The -L in the put statement left justifies the result so we can use the =: , a "begins with" comparison with your fixed value of 2775.&amp;nbsp;&amp;nbsp; The explicit output only writes to the output data set when the value is found. The LEAVE instruction is used to quit the loop the first time the condition is true so you would only get one record even if multiple variables meet the condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will leave the exclude as an exercise for the interested reader but the solution is very similar but uses DELETE instead of OUTPUT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Though one does wonder why these values are numeric to begin with. You do not appear to be using them as numbers. If these are some sort of identifier then you likely should make sure the values are character instead and then you don't need the somewhat kludgy Put to make a comparison value.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 19:20:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678228#M23831</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-20T19:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: sort observations starting with x</title>
      <link>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678312#M23841</link>
      <description>&lt;P&gt;I fully agree to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226241"&gt;@AMSAS&lt;/a&gt;: This is the job for a regex.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please have a look at:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   if prxmatch('/\b2775/', catx(' ', of dx1-dx3));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 05:10:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678312#M23841</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-08-21T05:10:09Z</dc:date>
    </item>
    <item>
      <title>Re: sort observations starting with x</title>
      <link>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678418#M23846</link>
      <description>&lt;PRE&gt;data have;
input dx1 dx2 dx3;
cards;
27756  826  79883
78399  2775  7274
99462  62294  677
48268  54981  27752
18872  992  27274
;
data want;
 set have;
 array x{*} _numeric_;
 do i=1 to dim(x);
  if int( x{i}/10**(int(log10(x{i}))-3) )=2775 then do;output;leave;end;
 end;
 drop i;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Aug 2020 12:09:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/sort-observations-starting-with-x/m-p/678418#M23846</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-08-21T12:09:52Z</dc:date>
    </item>
  </channel>
</rss>

