<?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: Find the first occurence of value inside group of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398373#M96363</link>
    <description>&lt;P&gt;Here is array way.&lt;/P&gt;
&lt;P&gt;Hope this is acceptable.&lt;/P&gt;
&lt;P&gt;As you are just new to the Community, please see a wheel on the top right. Click the arrow. Mark my answer as you please.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id year sex a1 a2 a3 a4 a5;
datalines; 
1 1994  1   1  1  2  2  2 
2 1992  1   1  4  4  4  4
3 1992  2   1  2  3  3  4
4 1991  1   1  1  4  4  4
;
run;

data want;
   set have;
   array a[5] a1 a2 a3 a4 a5;

   retain flag 0;
   retain a4pos;
   do i = 1 to 5;
      if a[i] = 2 or a[i] = 3 then do;
         flag = 1;
         if i &amp;gt; 3 then leave;
      end;
      if flag = 1 and a[i] = 4 then do; a[i] = 2; a4pos = i + 1; leave; end;
   end;
   if flag = 1 and a4pos ^= . then do i = a4pos to 5;
      a[i] = 3;
   end;
   a4pos = .;
drop i flag a4pos;
run;

Obs	id	year	sex	a1	a2	a3	a4	a5
1	1	1994	1	1	1	2	2	2
2	2	1992	1	1	2	3	3	3
3	3	1992	2	1	2	3	3	4
4	4	1991	1	1	1	2	3	3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 24 Sep 2017 10:47:18 GMT</pubDate>
    <dc:creator>KachiM</dc:creator>
    <dc:date>2017-09-24T10:47:18Z</dc:date>
    <item>
      <title>Find the first occurence of value inside group of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398363#M96357</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have multiple indicator variables and I need to find the first occurence of specific value among them and replace the next values with another value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample of my data looks like&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;id year sex a1 a2 a3 a4 a5&amp;nbsp;
1 1994  1   1  1  2  2  2 
2 1992  1   1  4  4  4  4
3 1992  2   1  2  3  3  4
4 1991  1   1  1  4  4  4&lt;/PRE&gt;&lt;P&gt;What I'm trying to is to detect if variable a1--a5 has a value 4 before value 2 or 3 have occurenced, and replace the first occurence of value for with 2 and the next values with 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to do that with arrays, but I haven't find out how to detect the first occurence of value and replace it with another value..&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the data should look like&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;id year sex a1 a2 a3 a4 a5&amp;nbsp;
1 1994  1   1  1  2  2  2 
2 1992  1   1  2  3  3  3
3 1992  2   1  2  3  3  4
4 1991  1   1  1  2  3  3&lt;/PRE&gt;&lt;P&gt;Any help is highly appreciated!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 08:37:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398363#M96357</guid>
      <dc:creator>Laura_123</dc:creator>
      <dc:date>2017-09-24T08:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: Find the first occurence of value inside group of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398373#M96363</link>
      <description>&lt;P&gt;Here is array way.&lt;/P&gt;
&lt;P&gt;Hope this is acceptable.&lt;/P&gt;
&lt;P&gt;As you are just new to the Community, please see a wheel on the top right. Click the arrow. Mark my answer as you please.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id year sex a1 a2 a3 a4 a5;
datalines; 
1 1994  1   1  1  2  2  2 
2 1992  1   1  4  4  4  4
3 1992  2   1  2  3  3  4
4 1991  1   1  1  4  4  4
;
run;

data want;
   set have;
   array a[5] a1 a2 a3 a4 a5;

   retain flag 0;
   retain a4pos;
   do i = 1 to 5;
      if a[i] = 2 or a[i] = 3 then do;
         flag = 1;
         if i &amp;gt; 3 then leave;
      end;
      if flag = 1 and a[i] = 4 then do; a[i] = 2; a4pos = i + 1; leave; end;
   end;
   if flag = 1 and a4pos ^= . then do i = a4pos to 5;
      a[i] = 3;
   end;
   a4pos = .;
drop i flag a4pos;
run;

Obs	id	year	sex	a1	a2	a3	a4	a5
1	1	1994	1	1	1	2	2	2
2	2	1992	1	1	2	3	3	3
3	3	1992	2	1	2	3	3	4
4	4	1991	1	1	1	2	3	3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 10:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398373#M96363</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2017-09-24T10:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find the first occurence of value inside group of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398374#M96364</link>
      <description>&lt;P&gt;Hi!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your reply. This works perfectly, but then I noticed that in my data exists couple of subjects (my data consist 20 000 rows) who has obs like this&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;7 1994  1   1  3  4  4  4&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;So now those rows become&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;7 1994  1   1  3  2  3  3&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;And what I want them to become is&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;7 1994  1   1  3  2  2  2&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Didin't realize when asking the question that there might be also obs like this..&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(So that there would be some kind of logic why I want create rows like this, is that those numbers indicate subjects marital status in year(1 to 5). So that 1=single 2=married 3=child and this number 4 is married &amp;amp; child. And I want separate that value. If subject has value 4 after being single, then it should be first married and then child (that's how it done right now). But if subject has first child and then has a value number 4 then it should be coded that first child(=3) then married(=2).)&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 11:55:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398374#M96364</guid>
      <dc:creator>Laura_123</dc:creator>
      <dc:date>2017-09-24T11:55:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find the first occurence of value inside group of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398375#M96365</link>
      <description>&lt;P&gt;Better you place an input data set with the revised requirements and show how you want the output to looklike. This will worth 1000 words.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 12:04:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398375#M96365</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2017-09-24T12:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find the first occurence of value inside group of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398376#M96366</link>
      <description>&lt;P&gt;Yeah, maybe you're right.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's example rows of all kind of combinations that exists in my data&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;id year sex a1 a2 a3 a4 a5&amp;nbsp;
1 1994  1   1  1  2  2  2 
2 1992  1   1  2  3  3  3
3 1992  2   1  2  3  3  3
4 1991  1   1  1  2  3  3&lt;BR /&gt;5 1990  2   1  2  2  4  4 &lt;BR /&gt;6 1985  2   1  2  4  4  4&lt;BR /&gt;7 1992  2   1  3  4  4  4&lt;BR /&gt;8 1993  1   1  4  4  4  4&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;So basically the idea is to replace value for depenging what value occurs before that.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If value before 4 is 2 then number 4 should be replaced with 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If value before 4 is 3 then number 4 should be replaced with 2&amp;nbsp;&lt;/P&gt;&lt;P&gt;if value before 4 is 1 then number 4 and the next one should be replaced with 2 &amp;amp; 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the desired output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;id year sex a1 a2 a3 a4 a5&amp;nbsp;
1 1994  1   1  1  2  2  2 
2 1992  1   1  2  3  3  3
3 1992  2   1  2  3  3  3
4 1991  1   1  1  2  3  3&lt;BR /&gt;5 1990  2   1  2  2  3  3 &lt;BR /&gt;6 1985  2   1  2  3  3  3&lt;BR /&gt;7 1992  2   1  3  2  2  2&lt;BR /&gt;8 1993  1   1  2  3  3  3 &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 12:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398376#M96366</guid>
      <dc:creator>Laura_123</dc:creator>
      <dc:date>2017-09-24T12:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Find the first occurence of value inside group of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398377#M96367</link>
      <description>&lt;P&gt;Yeah, you're right.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So here's all kind of combinations that exists my data&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;id year sex a1 a2 a3 a4 a5&amp;nbsp;
1 1994  1   1  1  2  2  2 
2 1992  1   1  4  4  4  4
3 1992  2   1  2  3  3  3
4 1991  1   1  2  4  4  4&lt;BR /&gt;5 1992  2   1  2  2  4  4&lt;BR /&gt;6 1985  2   1  3  4  4  4 &lt;BR /&gt;7 1988  2   1  3  3  3  3 &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;And the data what I want:&lt;/P&gt;&lt;PRE&gt;id year sex a1 a2 a3 a4 a5&amp;nbsp;
1 1994  1   1  1  2  2  2 
2 1992  1   1  2  3  3  3
3 1992  2   1  2  3  3  3
4 1991  1   1  2  3  3  3&lt;BR /&gt;5 1992  2   1  2  2  3  3&lt;BR /&gt;6 1985  2   1  3  2  2  2 &lt;BR /&gt;7 1988  2   1  3  3  3  3&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;So if number 4&amp;nbsp;exists, then I want to detect which value occurs before that.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If value before 4 is 2 then number 4 should be replaced with 3&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If value before 4 is 3 then number 4 should be replaced with 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If value before 4 is 1 then number 4&amp;nbsp;and the next one should be replaced with 2 and 3&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 13:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398377#M96367</guid>
      <dc:creator>Laura_123</dc:creator>
      <dc:date>2017-09-24T13:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: Find the first occurence of value inside group of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398385#M96371</link>
      <description>&lt;P&gt;Better understood now&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id year sex a1 a2 a3 a4 a5;
datalines; 
1 1994  1   1  1  2  2  2 
2 1992  1   1  4  4  4  4
3 1992  2   1  2  3  3  3
4 1991  1   1  2  4  4  4
5 1992  2   1  2  2  4  4
6 1985  2   1  3  4  4  4 
7 1988  2   1  3  3  3  3 
;
run;

data need;
   array k[5] _temporary_;
   do until(eof);
      set have end = eof;
      array a a1--a5;
      do i = 1 to dim(k);
         k[i] = a[i];
      end;
      /** Process the conditions **/
     do i = 1 to dim(k);
      if k[i] = 4 then do;
      
         if k[i-1] = 2 then do;
            do j = i to dim(k);
               k[j] = 3;
            end;
         end;
      
         else if k[i-1] = 3 then do;
            do j = i to dim(k);
               k[j] = 2;
            end;
         end;
      
         else if k[i-1] = 1 then do;
            k[i] = 2;
            do j = i+1 to dim(k);
               k[j] = 3;
            end;
         end;
      end;
   end;
   do i = 1 to dim(k);
      a[i] = k[i];
   end;
   output;
end;
drop i j;
run;


Obs	id	year	sex	a1	a2	a3	a4	a5
1	1	1994	1	1	1	2	2	2
2	2	1992	1	1	2	3	3	3
3	3	1992	2	1	2	3	3	3
4	4	1991	1	1	2	3	3	3
5	5	1992	2	1	2	2	3	3
6	6	1985	2	1	3	2	2	2
7	7	1988	2	1	3	3	3	3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;P&gt;It is easier to copy a1 - a5 into a _temporary_ array first.&lt;/P&gt;
&lt;P&gt;Then process the array for 4, and apply the conditions.&lt;/P&gt;
&lt;P&gt;Then copy the elements of array to a1 - a5 and write the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 14:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398385#M96371</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2017-09-24T14:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find the first occurence of value inside group of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398388#M96373</link>
      <description>&lt;P&gt;Thank you! That solved my problem. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 14:46:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-first-occurence-of-value-inside-group-of-variables/m-p/398388#M96373</guid>
      <dc:creator>Laura_123</dc:creator>
      <dc:date>2017-09-24T14:46:01Z</dc:date>
    </item>
  </channel>
</rss>

