<?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: Counting across variables with stopping/repeating condition in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477896#M14941</link>
    <description>&lt;P&gt;What is your expected output for the data you provided? You need to explain that piece along with your logic too&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jul 2018 15:00:42 GMT</pubDate>
    <dc:creator>Andygray</dc:creator>
    <dc:date>2018-07-13T15:00:42Z</dc:date>
    <item>
      <title>Counting across variables with stopping/repeating condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477893#M14940</link>
      <description>&lt;P&gt;Hi guys, I have a data set that is just a sample for my bigger one. The code looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT face="Courier New" size="3"&gt;data counting_test;
input id a b c d e;
datalines;
12 0 1 0 0 0
13 0 0 1 0 0
14 1 0 0 0 0
;
run;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Essentially this will be a duration counter that will count across the variables 'a' through 'e' so that it will check how long it takes for the binary variable to be a 1. I currently can't get any code to work or output anything that is close to what I need. Any help would be greatly appreciated!&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 14:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477893#M14940</guid>
      <dc:creator>hjones_6</dc:creator>
      <dc:date>2018-07-13T14:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Counting across variables with stopping/repeating condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477896#M14941</link>
      <description>&lt;P&gt;What is your expected output for the data you provided? You need to explain that piece along with your logic too&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 15:00:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477896#M14941</guid>
      <dc:creator>Andygray</dc:creator>
      <dc:date>2018-07-13T15:00:42Z</dc:date>
    </item>
    <item>
      <title>Re: Counting across variables with stopping/repeating condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477901#M14942</link>
      <description>&lt;P&gt;sorry about that! I have been trying to complete a do loop that will accomplish this using the code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data counting_test_2;
set counting_test;
by id;
if first.id then do;
counter = 1;
end;
if last.id then do;
	if a = 0 then counter+1;
	if b = 0 then counter+1;
	if c = 0 then counter+1;
	if d = 0 then counter+1;
	if e = 0 then counter+1;
	end;
else do;
	if a = 1 then counter = 1;
	if b = 1 then counter = 2;
	if c = 1 then counter = 3;
	if d = 1 then counter = 4;
	if e = 1 then counter = 5;
end;
run;&lt;/CODE&gt;&lt;/PRE&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;Right now it is outputting the counter at 5 every time for every record. But my expected outcome is going to be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="output-sas.PNG" style="width: 230px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21774iD3C7204483B82AAF/image-size/large?v=v2&amp;amp;px=999" role="button" title="output-sas.PNG" alt="output-sas.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 15:12:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477901#M14942</guid>
      <dc:creator>hjones_6</dc:creator>
      <dc:date>2018-07-13T15:12:59Z</dc:date>
    </item>
    <item>
      <title>Re: Counting across variables with stopping/repeating condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477903#M14943</link>
      <description>&lt;P&gt;Perhaps Something like:&lt;/P&gt;
&lt;PRE&gt;data counting_test;
input id a b c d e;
result= whichn(1,a,b,c,d,e);
datalines;
12 0 1 0 0 0
13 0 0 1 0 0
14 1 0 0 0 0
;
run;&lt;/PRE&gt;
&lt;P&gt;If you have a largish number of variables an array may be easier to use, especially if they are either named with a numeric suffix (a1 to a25 for example) or adjacent in the data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The change in code would be to add an array definition:&lt;/P&gt;
&lt;P&gt;array test a1-a25;&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;array test a -- lastvar;&lt;/P&gt;
&lt;P&gt;and then use&lt;/P&gt;
&lt;P&gt;result =whichn(1, of test(*));&lt;/P&gt;
&lt;P&gt;the WHICHN function returns the first variable or value in a list&amp;nbsp;that matches the value in the first position (which could be a variable itself).&amp;nbsp; The "of test(*)" is one way to use all of the variable in the array.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 15:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477903#M14943</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-07-13T15:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Counting across variables with stopping/repeating condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477904#M14944</link>
      <description>&lt;P&gt;I'm not sure, but I think you want to do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data counting_test;
input id a b c d e;
datalines;
12 0 1 0 0 0
13 0 0 1 0 0
14 1 0 0 0 0
;
run;

data want;
   set counting_test;
   array MyArray a b c d e;
   FirstOne=whichn(1, of MyArray[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Jul 2018 15:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477904#M14944</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-07-13T15:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Counting across variables with stopping/repeating condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477907#M14945</link>
      <description>&lt;P&gt;Use Arrays&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data counting_test;
input id a b c d e;
datalines;
12 0 1 0 0 0
13 0 0 1 0 0
14 1 0 0 0 0
;
run;

data want(drop=i);
set counting_test;
array num(*) _numeric_;
do i=1 to dim(num);
if num(i)=1 then Place=i-1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Jul 2018 15:20:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477907#M14945</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-07-13T15:20:30Z</dc:date>
    </item>
    <item>
      <title>Re: Counting across variables with stopping/repeating condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477909#M14946</link>
      <description>Thank you! One more quick question; is there anyway to put a repeat condition in this? like for it to restart if there are more than one 1's in the same record?</description>
      <pubDate>Fri, 13 Jul 2018 15:23:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477909#M14946</guid>
      <dc:creator>hjones_6</dc:creator>
      <dc:date>2018-07-13T15:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Counting across variables with stopping/repeating condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477923#M14947</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/220905"&gt;@hjones_6&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you! One more quick question; is there anyway to put a repeat condition in this? like for it to restart if there are more than one 1's in the same record?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Now you have to show what the output data set might look like. Multiple results now imply that you potentially have to record as many elements as you search, which means another array to hold things.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Better might be to explain HOW you will use the resulting multiple indicators.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 15:43:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-across-variables-with-stopping-repeating-condition/m-p/477923#M14947</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-07-13T15:43:24Z</dc:date>
    </item>
  </channel>
</rss>

