<?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: using some condition to search target data then replace them with a sequence in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755534#M238447</link>
    <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  set HAVE;
  if mod(J,2)=1 then do;
    N+0.2;
    J=N;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Jul 2021 01:53:51 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2021-07-21T01:53:51Z</dc:date>
    <item>
      <title>using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755516#M238431</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'd like to search a column in a dataset based on some conditions. Then replace them with a sequence with range [0,1] and 1/n as increment (n is the number of data found based on the condition). For example, search odd numbers in the column j in the Test dataset below. Then replace '3, 5, 7, 9, 11' with '0.2, 0.4, 0.6, 0.8, 0.1'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;
 do i=1 to 10 by 1;
 j=i+1;
 output;
 end;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance&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;&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 00:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755516#M238431</guid>
      <dc:creator>yaoyao</dc:creator>
      <dc:date>2021-07-21T00:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755534#M238447</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  set HAVE;
  if mod(J,2)=1 then do;
    N+0.2;
    J=N;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 01:53:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755534#M238447</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-21T01:53:51Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755539#M238452</link>
      <description>Thank you for your help. It is great! Have a further question. Since we know there are five odd numbers, we set 1/5 as increment. How can we have a dynamic method to set the increment.</description>
      <pubDate>Wed, 21 Jul 2021 02:18:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755539#M238452</guid>
      <dc:creator>yaoyao</dc:creator>
      <dc:date>2021-07-21T02:18:49Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755543#M238455</link>
      <description>&lt;P&gt;Do you want 2 steps?&amp;nbsp; 1) Count the number of odd number&amp;nbsp; 2) Apply the derived ratio?&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 02:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755543#M238455</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-21T02:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755546#M238457</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  do until(LASTOBS1);
    set HAVE(where=(mod(J,2)=1)) end=LASTOBS1;
    DENOM+1;
  end;
  do until(LASTOBS2);
    set HAVE end=LASTOBS2;
    if mod(J,2)=1 then do;
      N+1/DENOM;
      J=N;
    end;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 02:40:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755546#M238457</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-21T02:40:47Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755547#M238458</link>
      <description>&lt;P&gt;Yep, 2 steps. Also is it possible we can do it with the dynamic range defined in the columns like column&amp;nbsp;start_range and&amp;nbsp;end_range as below?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;
 do i=1 to 10 by 1;
 j=i+1;
 start_range=0.5;
 end_range=1;
 output;
 end;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Jul 2021 02:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755547#M238458</guid>
      <dc:creator>yaoyao</dc:creator>
      <dc:date>2021-07-21T02:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755548#M238459</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; Also is it possible we can do it with the dynamic range&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Just change the line&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;N+1/DENOM;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 02:54:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755548#M238459</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-21T02:54:13Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755549#M238460</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  do until(LASTOBS1);
    set HAVE(where=(mod(J,2)=1)) end=LASTOBS1;
    DENOM+1;
  end;
  N=START_RANGE;
  do until(LASTOBS2);
    set HAVE end=LASTOBS2;
    if mod(J,2)=1 then do;
      J=N;
      N+(END_RANGE-START_RANGE)/DENOM;
    end;
   output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Jul 2021 02:57:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755549#M238460</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-21T02:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755608#M238487</link>
      <description>&lt;P&gt;As you have seen, you need to pass through the data twice: first time to establish a denominator, and the second time to increment the cumulative fraction and output.&amp;nbsp; You can do this with a pair of loops each with a SET statement, or a single SET with "in=" parameters, and a couple of "summing statements":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (in=firstpass)
      have (in=secondpass) ;
  where mod(j,2)=1;
  if firstpass then _denom+1;  /*summing statement for counting*/
  if secondpass ;  /*"subsetting if" */
  n+1/_denom ;     /* summing statement for incrementing */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Jul 2021 12:25:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755608#M238487</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-21T12:25:06Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755672#M238515</link>
      <description>&lt;P&gt;Many thanks. It is exactly what I wanted. Can I have the last request? How can I calculate N by group? For example, the test dataset has a group column. I want to count the number of the odd numbers by the group. Then set the two subgroups with the different increments. Appreciate your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 do i=1 to 10 by 1;
 j=i+1;
 if j&amp;lt;=5 then
		group = 0;
	else
		group = 1;

 start_range=0.5;
 end_range=1;
 output;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Jul 2021 15:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755672#M238515</guid>
      <dc:creator>yaoyao</dc:creator>
      <dc:date>2021-07-21T15:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755683#M238519</link>
      <description>Thank you for your suggestion. Can you help to write a sample code? I am an R user for several years. But still new to SAS. In my real code, there are thousands of groups. Wondered whether we can do the grouping calculation efficiently.</description>
      <pubDate>Wed, 21 Jul 2021 16:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755683#M238519</guid>
      <dc:creator>yaoyao</dc:creator>
      <dc:date>2021-07-21T16:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755769#M238553</link>
      <description>&lt;P&gt;&amp;gt;&lt;EM&gt;&amp;nbsp;How can I calculate N by group?&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Like this ?&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data WANT; 
  do until(LASTOBS1);
    set HAVE(where=(mod(J,2)=1)) end=LASTOBS1;
    DENOM0+(GROUP=0); 
    DENOM1+(GROUP=1);
  end;
  do until(LASTOBS2);
    set HAVE end=LASTOBS2;
    if mod(J,2)=1 then do;
      if GROUP=0 then do;
        N0+1/DENOM0; 
        J=N0;
      end;
      if GROUP=1 then do;
        N1+1/DENOM1; 
        J=N1;
      end;
    end;
    output;
  end;
run;&lt;/LI-CODE&gt;
&lt;P&gt;Assuming the data is not sorted by GROUP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 22:25:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755769#M238553</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-21T22:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755784#M238562</link>
      <description>Thank you for your code. Is it possible we can dynamically deal with the group? In my real code, the dataset is grouped by date. It expands over time.</description>
      <pubDate>Wed, 21 Jul 2021 23:29:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755784#M238562</guid>
      <dc:creator>yaoyao</dc:creator>
      <dc:date>2021-07-21T23:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755842#M238584</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data WANT; 
  call missing(DENOM,N);
  do until(last.DATE);
    set HAVE(where=(mod(J,2)=1));
    by DATE; 
    DENOM+1;
  end;
  do until(last.DATE);
    set HAVE ;
    by DATE;
    if mod(J,2)=1 then do;
      N+1/DENOM; 
      J=N;
    end;
    output;
  end;
run;&lt;/LI-CODE&gt;
&lt;P&gt;Untested as I don't have access to SAS atm.&lt;/P&gt;
&lt;P&gt;This won't work if some groups contain no odd numbers.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jul 2021 07:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/755842#M238584</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-22T07:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: using some condition to search target data then replace them with a sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/760034#M240290</link>
      <description>Many thanks. It works.</description>
      <pubDate>Fri, 06 Aug 2021 17:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-some-condition-to-search-target-data-then-replace-them/m-p/760034#M240290</guid>
      <dc:creator>yaoyao</dc:creator>
      <dc:date>2021-08-06T17:44:27Z</dc:date>
    </item>
  </channel>
</rss>

