<?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 list a range of alphanumeric categories for a variable in SAS. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929618#M365769</link>
    <description>&lt;P&gt;Then IN operator first requires ( ) around the values. Second the only "list" behavior it understands is integer numeric values where 5:15 are the integers from 5 to 15 inclusive.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Character values need to be in quotes so you need to provide something that looks like:&lt;/P&gt;
&lt;PRE&gt;if myvariable in ( 'A20' 'A21' 'A22' ... 'A40') then &amp;lt;whatever&amp;gt;&lt;/PRE&gt;
&lt;P&gt;For an example like yours a macro to build the list may be practical:&lt;/P&gt;
&lt;PRE&gt;%macro stemseq (Stem,start, end);
   %let result=;
   %do i=&amp;amp;start %to &amp;amp;end;&lt;BR /&gt;      %let i = %sysfunc(putn(&amp;amp;i,z2.));
      %let result = &amp;amp;result "&amp;amp;Stem.&amp;amp;i";
   %end;
   &amp;amp;result
%mend;

data have;
   input var $;
datalines;
A10
A20
A34
B16
;

data example;
   set have;
   myfavorite = (var in (%stemseq(A,15,40) ) );
run;&lt;/PRE&gt;
&lt;P&gt;A few important things about this approach:&lt;/P&gt;
&lt;P&gt;The macro executes during the compile the stage of the data step using it. That means the macro cannot see variable values in your data set.&lt;/P&gt;
&lt;P&gt;You can provide two sequences but each would require a separate call to the macro for example&lt;/P&gt;
&lt;PRE&gt;data example;
   set have;
   myfavorite = (var in (%stemseq(A,15,40) %stemseq(B,10,18) ) );
run;&lt;/PRE&gt;
&lt;P&gt;If you place other values that you may want in the code properly then you can include other values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The comparison when you use IN is case sensitive. If the value is "A10" you want make sure to use A in the macro.&lt;/P&gt;
&lt;P&gt;The last line of the macro code &lt;STRONG&gt;DOESNOT and should not have a semicolon.&lt;/STRONG&gt; If you code in SAS long enough it may feel wrong but we want to expose the result as code lines and if you include a ; then that will be placed in the code as well.&lt;/P&gt;
&lt;P&gt;The MACRO code from the %macro through %mend; has to be executed once to compile the code. Then it can be used multiple times during that SAS session. When you restart SAS you need to recompile the macro to use it.&lt;/P&gt;
&lt;P&gt;If you want to just test what the macro creates you can use code like:&lt;/P&gt;
&lt;PRE&gt;%put %stemseq(A,15,40);&lt;/PRE&gt;
&lt;P&gt;% is interpreted by SAS as a macro language code flag.&lt;/P&gt;
&lt;P&gt;The macro also uses the %sysfunc function to force leading zeroes into values . It is limited to 2 characters per your data description.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 24 May 2024 16:23:05 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-05-24T16:23:05Z</dc:date>
    <item>
      <title>How to list a range of alphanumeric categories for a variable in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929609#M365764</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable in my dataset. The categories for this variable are alphanumeric and in character format. For example, they are: A00 to A99, B60 to B89, and so on.&lt;/P&gt;&lt;P&gt;Now, I want to list some of them in my SAS syntax:&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=""&gt;Data want;&amp;nbsp;

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;set have;&amp;nbsp;

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If myvariable in A20 to A45 then myfavourite = 1;

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else myfavourite = 0;

run;&amp;nbsp;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help would be much appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2024 15:07:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929609#M365764</guid>
      <dc:creator>altadata1</dc:creator>
      <dc:date>2024-05-24T15:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to list a range of alphanumeric categories for a variable in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929618#M365769</link>
      <description>&lt;P&gt;Then IN operator first requires ( ) around the values. Second the only "list" behavior it understands is integer numeric values where 5:15 are the integers from 5 to 15 inclusive.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Character values need to be in quotes so you need to provide something that looks like:&lt;/P&gt;
&lt;PRE&gt;if myvariable in ( 'A20' 'A21' 'A22' ... 'A40') then &amp;lt;whatever&amp;gt;&lt;/PRE&gt;
&lt;P&gt;For an example like yours a macro to build the list may be practical:&lt;/P&gt;
&lt;PRE&gt;%macro stemseq (Stem,start, end);
   %let result=;
   %do i=&amp;amp;start %to &amp;amp;end;&lt;BR /&gt;      %let i = %sysfunc(putn(&amp;amp;i,z2.));
      %let result = &amp;amp;result "&amp;amp;Stem.&amp;amp;i";
   %end;
   &amp;amp;result
%mend;

data have;
   input var $;
datalines;
A10
A20
A34
B16
;

data example;
   set have;
   myfavorite = (var in (%stemseq(A,15,40) ) );
run;&lt;/PRE&gt;
&lt;P&gt;A few important things about this approach:&lt;/P&gt;
&lt;P&gt;The macro executes during the compile the stage of the data step using it. That means the macro cannot see variable values in your data set.&lt;/P&gt;
&lt;P&gt;You can provide two sequences but each would require a separate call to the macro for example&lt;/P&gt;
&lt;PRE&gt;data example;
   set have;
   myfavorite = (var in (%stemseq(A,15,40) %stemseq(B,10,18) ) );
run;&lt;/PRE&gt;
&lt;P&gt;If you place other values that you may want in the code properly then you can include other values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The comparison when you use IN is case sensitive. If the value is "A10" you want make sure to use A in the macro.&lt;/P&gt;
&lt;P&gt;The last line of the macro code &lt;STRONG&gt;DOESNOT and should not have a semicolon.&lt;/STRONG&gt; If you code in SAS long enough it may feel wrong but we want to expose the result as code lines and if you include a ; then that will be placed in the code as well.&lt;/P&gt;
&lt;P&gt;The MACRO code from the %macro through %mend; has to be executed once to compile the code. Then it can be used multiple times during that SAS session. When you restart SAS you need to recompile the macro to use it.&lt;/P&gt;
&lt;P&gt;If you want to just test what the macro creates you can use code like:&lt;/P&gt;
&lt;PRE&gt;%put %stemseq(A,15,40);&lt;/PRE&gt;
&lt;P&gt;% is interpreted by SAS as a macro language code flag.&lt;/P&gt;
&lt;P&gt;The macro also uses the %sysfunc function to force leading zeroes into values . It is limited to 2 characters per your data description.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2024 16:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929618#M365769</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-24T16:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to list a range of alphanumeric categories for a variable in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929621#M365771</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/220658"&gt;@altadata1&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A simple IF condition such as&lt;/P&gt;
&lt;PRE&gt;'A20'&amp;lt;=myvariable&amp;lt;='A45'&lt;/PRE&gt;
&lt;P&gt;would incorrectly include values like &lt;FONT face="courier new,courier"&gt;'A2b'&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;'A234'&lt;/FONT&gt; &lt;EM&gt;if&lt;/EM&gt; such values occurred in &lt;FONT face="courier new,courier"&gt;myvariable&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you may want to use a very restrictive condition, e.g.&lt;/P&gt;
&lt;PRE&gt;length(myvariable)=3 &amp;amp; myvariable=:'A' &amp;amp; 20&amp;lt;=input(substr(myvariable,2),?? 2.)&amp;lt;=45&lt;/PRE&gt;
&lt;P&gt;where you can omit the length check if the defined length of variable&amp;nbsp;&lt;FONT face="courier new,courier"&gt;myvariable&lt;/FONT&gt; is 3 anyway.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By using a Perl regular expression you can obtain the same result with shorter code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
myfavourite=prxmatch('/^(A[23]\d|A4[0-5])$/',trim(myvariable));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 May 2024 16:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929621#M365771</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-05-24T16:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to list a range of alphanumeric categories for a variable in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929656#M365782</link>
      <description>&lt;P&gt;Thank you ballardw. Sorry, I should have explained it more. I know how to specify character value (double quote) and i can write it like this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if myvariable in ( 'A20' 'A21' 'A22' ... 'A40') then &amp;lt;whatever&amp;gt;&lt;/PRE&gt;&lt;P&gt;I want to avoid writing all the values because there are many of them, for example from X00 to X99.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2024 23:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929656#M365782</guid>
      <dc:creator>altadata1</dc:creator>
      <dc:date>2024-05-24T23:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to list a range of alphanumeric categories for a variable in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929657#M365783</link>
      <description>&lt;P&gt;It works well. Thank you so much.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 May 2024 00:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-list-a-range-of-alphanumeric-categories-for-a-variable-in/m-p/929657#M365783</guid>
      <dc:creator>altadata1</dc:creator>
      <dc:date>2024-05-25T00:05:28Z</dc:date>
    </item>
  </channel>
</rss>

