<?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 use character variables in if then statement in an array in SAS. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594680#M170900</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;There's no need for WHICHC, as the search can be done directly against the direct array name reference:&lt;/P&gt;
&lt;PRE&gt;data have ;                                                                                                                                                                                                                                                     
  input (day1-day10) ($) ;                                                                                                                                                                                                                                      
  cards ;                                                                                                                                                                                                                                                       
a1 b32 c56 d9876 e298 f402 a1 b32 c56 d9876 e298                                                                                                                                                                                                                
a1 b32 c56 d9876 e298 a1   a1 b32 c56 d9876 e298                                                                                                                                                                                                                
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data want ;                                                                                                                                                                                                                                                     
  set have ;                                                                                                                                                                                                                                                    
  array d day: ;                                                                                                                                                                                                                                                
  important_day = put (ifc (&lt;FONT color="#800000"&gt;&lt;STRONG&gt;"f402" in d&lt;/STRONG&gt;&lt;/FONT&gt;, "yes", "no"), $3.) ;                                                                                                                                                                                                   
run ;                                                                 
&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Oct 2019 09:11:35 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-10-08T09:11:35Z</dc:date>
    <item>
      <title>How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594668#M170890</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having an array of variables day1-day10 (10 variables) including character values there, such as a1, b32, c56, d9876, e298, and f402. I want to create a new variable---important_day. Only when day1 (or day2, day3, or...day10)=f402 then important_day=Yes, when day1 (or day2, day3, or...day10) is anyone else then important_day=No.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please advice how to use character variables in if then statement in an array in SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;with regards,&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 08:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594668#M170890</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2019-10-08T08:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594670#M170892</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (day1-day10)(:$);
datalines;
a1 b32 c56 d9876 e298 f402 a1 b32 c56 d9876 e298
a1 b32 c56 d9876 e298 a1   a1 b32 c56 d9876 e298
;

data want;
    set have;
    array d{10} day1-day10;
    important_day=ifc(whichc('f402', of d[*]) &amp;gt; 0, 'Yes', 'No');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Oct 2019 08:32:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594670#M170892</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-08T08:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594672#M170894</link>
      <description>I tried, but it didn't work.&lt;BR /&gt;what does ifc mean?</description>
      <pubDate>Tue, 08 Oct 2019 08:42:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594672#M170894</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2019-10-08T08:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594673#M170895</link>
      <description>&lt;P&gt;What do you mean by '&lt;SPAN&gt;it didn't work'? Did you get an error in the log or did the results just differ from what you want? And if so, how? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The IFC Function Simply returns 'Yes' if the first argument is true ('f402' is there) and 'No' if the first argument is false ('f402' is not there).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 08:45:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594673#M170895</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-08T08:45:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594679#M170899</link>
      <description>&lt;P&gt;Just to be clear, the above is equivalent to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    array d{10} day1-day10;
    if whichc('f402', of d[*]) &amp;gt; 0 then important_day='Yes';
    else                                important_day='No';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Oct 2019 09:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594679#M170899</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-08T09:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594680#M170900</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;There's no need for WHICHC, as the search can be done directly against the direct array name reference:&lt;/P&gt;
&lt;PRE&gt;data have ;                                                                                                                                                                                                                                                     
  input (day1-day10) ($) ;                                                                                                                                                                                                                                      
  cards ;                                                                                                                                                                                                                                                       
a1 b32 c56 d9876 e298 f402 a1 b32 c56 d9876 e298                                                                                                                                                                                                                
a1 b32 c56 d9876 e298 a1   a1 b32 c56 d9876 e298                                                                                                                                                                                                                
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data want ;                                                                                                                                                                                                                                                     
  set have ;                                                                                                                                                                                                                                                    
  array d day: ;                                                                                                                                                                                                                                                
  important_day = put (ifc (&lt;FONT color="#800000"&gt;&lt;STRONG&gt;"f402" in d&lt;/STRONG&gt;&lt;/FONT&gt;, "yes", "no"), $3.) ;                                                                                                                                                                                                   
run ;                                                                 
&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 09:11:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594680#M170900</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-08T09:11:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594681#M170901</link>
      <description>&lt;P&gt;You can try also this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    length important_day $3;
    important_day="No";
    array day(10) $;
    do i=1 to dim(day);
    	if day(i)= "f402" then do;
    		important_day="Yes";	/* When "f402" is found, change the flag variable to "Yes" */
    		leave;	/* No need to continue checking because "f402" is found */
    	end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Oct 2019 09:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594681#M170901</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-10-08T09:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594682#M170902</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;of course. Good catch!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 09:15:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594682#M170902</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-08T09:15:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594722#M170941</link>
      <description>&lt;P&gt;Not sure how using one method (IN operator) is better/worse than the other (WHICHC() function).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that if you use the function there is no need to define an array.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 12:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594722#M170941</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-08T12:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594881#M171028</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;I don't think it's a matter of "better" or "worse"; just two different pieces of SAS functionality.&lt;/P&gt;
&lt;P&gt;If you only want to know if the value to which an expression resolves is present an array A, then coding:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;lt;expression&amp;gt; IN A&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is less busy code-wise (and, IMO, more self-explanatory) than:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; whichn (&amp;lt;expression&amp;gt;, of A[*]) &amp;gt; 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Besides, IN is good for both numeric and character arrays, while WHICHN/C has to be type-specific. If you want to know the index of the first array item where &amp;lt;expression&amp;gt; is found, then of course IN doesn't tell while WHICHN/C does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Performance-wise:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Both use sequential search, so both execute in &lt;EM&gt;O(N)&lt;/EM&gt; time.&lt;/LI&gt;
&lt;LI&gt;IN doesn't use any extra memory over the memory allocated for the array, no matter how many times the compiler sees it.&lt;/LI&gt;
&lt;LI&gt;WHICHN/C uses extra memory equal to that allocated for the array for &lt;EM&gt;every reference&lt;/EM&gt; to WHICHN/C the compiler sees.&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;For example, run the following two pieces of code and compare their RAM footprints:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option fullstimer ;                                                                                                                     
data _null_ ;                                                                                                                           
  array a [1000000] _temporary_ (1:1000000) ;                                                                                           
  do x = 1, 500000, 1000000 ;                                                                                                           
    f1 = x     in a ;                                                                                                                   
    f2 = x + 1 in a ;                                                                                                                   
    f3 = x - 1 in a ;                                                                                                                   
    put (f1-f3) (=) ;                                                                                                                   
  end ;                                                                                                                                 
run ;                                                                                                                                   
data _null_ ;                                                                                                                           
  array a [1000000] _temporary_ (1:1000000) ;                                                                                           
  do x = 1, 500000, 1000000 ;                                                                                                           
    f1 = whichn (x,     of a[*]) &amp;gt; 0 ;                                                                                                  
    f2 = whichn (x + 1, of a[*]) &amp;gt; 0 ;                                                                                                  
    f3 = whichn (x - 1, of a[*]) &amp;gt; 0 ;                                                                                                  
    put (f1-f3) (=) ;                                                                                                                   
  end ;                                                                                                                                 
run ;              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then comment out one or two calls for IN and WHICHN and watch what FULLSTIMER reports compared to the uncommented version.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&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>Tue, 08 Oct 2019 19:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/594881#M171028</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-08T19:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/595926#M171517</link>
      <description>I really appreciate your help!&lt;BR /&gt;Best regards,&lt;BR /&gt;</description>
      <pubDate>Fri, 11 Oct 2019 23:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/595926#M171517</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2019-10-11T23:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/595927#M171518</link>
      <description>I really appreciate your help!&lt;BR /&gt;Best regards,</description>
      <pubDate>Fri, 11 Oct 2019 23:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/595927#M171518</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2019-10-11T23:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to use character variables in if then statement in an array in SAS.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/595938#M171522</link>
      <description>Thank you very much!</description>
      <pubDate>Sat, 12 Oct 2019 03:26:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-character-variables-in-if-then-statement-in-an-array/m-p/595938#M171522</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2019-10-12T03:26:09Z</dc:date>
    </item>
  </channel>
</rss>

