<?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: Do Loops: Conditionally Assigning a Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519584#M140749</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

data want;
set sample;
cond1=^^findc(cats(of var:),'ef');
cond2=^^findc(cats(of x:),'72');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 07 Dec 2018 22:28:23 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-12-07T22:28:23Z</dc:date>
    <item>
      <title>Do Loops: Conditionally Assigning a Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519555#M140731</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For my question I'm using a dataset similar to the one below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm trying to detect whether var1-var4 has an "e" or "f" in it and whether x1-x4 has a "7" or "2" in it. My code to complete this is similar to what follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
	array cond (2) cond1-cond2 (2*0);
	
	do i=1 to 4;
	    if var[i] in ("e", "f") then cond1=1;
	    if x[i] in ("7", "2") then cond2=1;
	end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My problem is that once cond# is given a value of 1, it does not reset to 0 and will continue to be 1 for all following observations regardless of whether the condition is met or not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How might I be able to implement a code that will let me given cond# a value of 1 or 0 based on checking each variable in their respective arrays? Am I incorrectly using the if/then function?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for any help or ideas you have.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Dec 2018 20:49:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519555#M140731</guid>
      <dc:creator>jdchang</dc:creator>
      <dc:date>2018-12-07T20:49:50Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops: Conditionally Assigning a Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519556#M140732</link>
      <description>&lt;P&gt;use boolean and &lt;STRONG&gt;not&lt;/STRONG&gt; initialize&amp;nbsp; array 2*0 which retains across&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
/*	array cond (2) cond1-cond2 ;not needed*/
	
	do i=1 to 4;
	  if not cond1 then   cond1=var[i] in ("e", "f");
	   if not cond2 then  cond2=x[i] in ("7", "2");
	end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Dec 2018 20:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519556#M140732</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-07T20:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops: Conditionally Assigning a Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519558#M140734</link>
      <description>Thank you!</description>
      <pubDate>Fri, 07 Dec 2018 20:56:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519558#M140734</guid>
      <dc:creator>jdchang</dc:creator>
      <dc:date>2018-12-07T20:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops: Conditionally Assigning a Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519561#M140736</link>
      <description>&lt;P&gt;WHICHC() + OR&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
set sample;

cond1 = whichc('e', of var1-var4) &lt;STRONG&gt;|&lt;/STRONG&gt; whichc('f', of var1-var4);
cond2 = whichc('7', of x1-x4) &lt;STRONG&gt;|&lt;/STRONG&gt; whichc('2', of x1-x4);

run;&lt;/PRE&gt;
&lt;P&gt;This works for me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/243966"&gt;@jdchang&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For my question I'm using a dataset similar to the one below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm trying to detect whether var1-var4 has an "e" or "f" in it and whether x1-x4 has a "7" or "2" in it. My code to complete this is similar to what follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
	array cond (2) cond1-cond2 (2*0);
	
	do i=1 to 4;
	    if var[i] in ("e", "f") then cond1=1;
	    if x[i] in ("7", "2") then cond2=1;
	end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My problem is that once cond# is given a value of 1, it does not reset to 0 and will continue to be 1 for all following observations regardless of whether the condition is met or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How might I be able to implement a code that will let me given cond# a value of 1 or 0 based on checking each variable in their respective arrays? Am I incorrectly using the if/then function?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for any help or ideas you have.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Dec 2018 21:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519561#M140736</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-07T21:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops: Conditionally Assigning a Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519563#M140737</link>
      <description>&lt;P&gt;When you initialize an array, it also does an implicit retain on those variables. You can get around that by not initializing them in the ARRAY statement but after the fact, or not at all unless it's needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/243966"&gt;@jdchang&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For my question I'm using a dataset similar to the one below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm trying to detect whether var1-var4 has an "e" or "f" in it and whether x1-x4 has a "7" or "2" in it. My code to complete this is similar to what follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data record;
set sample;
	array var {4} $ var1-var4;
	array x {4} $ x1-x4;
	array cond (2) cond1-cond2 (2*0);
	
	do i=1 to 4;
	    if var[i] in ("e", "f") then cond1=1;
	    if x[i] in ("7", "2") then cond2=1;
	end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;My problem is that once cond# is given a value of 1, it does not reset to 0 and will continue to be 1 for all following observations regardless of whether the condition is met or not.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;How might I be able to implement a code that will let me given cond# a value of 1 or 0 based on checking each variable in their respective arrays? Am I incorrectly using the if/then function?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for any help or ideas you have.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Dec 2018 21:10:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519563#M140737</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-07T21:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops: Conditionally Assigning a Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519584#M140749</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

data want;
set sample;
cond1=^^findc(cats(of var:),'ef');
cond2=^^findc(cats(of x:),'72');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Dec 2018 22:28:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519584#M140749</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-07T22:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops: Conditionally Assigning a Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519610#M140764</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data sample;
input var1 $ var2 $ var3 $ var4 $ x1 $ x2 $ x3 $ x4 $;
datalines;
a b c d 9 8 7 6
b c d e 8 7 6 5
c d e f 7 6 5 4
d e f g 6 5 4 3
e f g h 5 4 3 2
f g h i 4 3 2 1
g h i j 3 2 1 0
;
run;

data want;
set sample;
cond1=^^findc(cats(of var:),'ef');
cond2=^^findc(cats(of x:),'72');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And I think&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wins obfuscation code of the day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For our OP the FINDC function returns either the number position of the character found or 0. The&amp;nbsp;second ^ (meaning NOT) negates the value of the number returned. Any 0 becomes 1 and any value returned other than 0 becomes 1 in the way SAS does logic operations. Then the first ^ negates the other result switching 0 to 1 and 1 to 0 as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I like it.&lt;/P&gt;
&lt;P&gt;Then the&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Dec 2018 00:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519610#M140764</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-12-08T00:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loops: Conditionally Assigning a Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519617#M140768</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;Sir. It's always been a privilege to have learned/learn a lot from you. I have sought your help numerous times and you must be glad I pay&amp;nbsp; sincere attention. Thank you again&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Dec 2018 00:36:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loops-Conditionally-Assigning-a-Variable/m-p/519617#M140768</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-08T00:36:23Z</dc:date>
    </item>
  </channel>
</rss>

