<?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: Replace zeros  after first zero for each group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947201#M370832</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you very much for your solution&amp;nbsp; I thought &lt;STRONG&gt;COALESCE&amp;nbsp;&lt;/STRONG&gt;function works here&lt;/P&gt;</description>
    <pubDate>Sat, 12 Oct 2024 05:42:34 GMT</pubDate>
    <dc:creator>pavank</dc:creator>
    <dc:date>2024-10-12T05:42:34Z</dc:date>
    <item>
      <title>Replace zeros  after first zero for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947165#M370827</link>
      <description>using below dataset and get below output data sc;&lt;BR /&gt;input group $ id ;&lt;BR /&gt;datalines;&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;A 0&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;B 2&lt;BR /&gt;B 0&lt;BR /&gt;B 2&lt;BR /&gt;B 2&lt;BR /&gt;C 3&lt;BR /&gt;C 0&lt;BR /&gt;C 3&lt;BR /&gt;C 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/*output*/&lt;BR /&gt;Name id&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;A 0&lt;BR /&gt;A 0&lt;BR /&gt;A 0&lt;BR /&gt;B 2&lt;BR /&gt;B 0&lt;BR /&gt;B 0&lt;BR /&gt;B 0&lt;BR /&gt;C 3&lt;BR /&gt;C 0&lt;BR /&gt;C 0&lt;BR /&gt;C 0</description>
      <pubDate>Fri, 11 Oct 2024 16:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947165#M370827</guid>
      <dc:creator>pavank</dc:creator>
      <dc:date>2024-10-11T16:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: Replace zeros  after first zero for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947171#M370829</link>
      <description>&lt;P&gt;data test;&lt;BR /&gt;input group $ id;&lt;BR /&gt;datalines;&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;A 0&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;B 2&lt;BR /&gt;B 0&lt;BR /&gt;B 2&lt;BR /&gt;B 2&lt;BR /&gt;C 3&lt;BR /&gt;C 0&lt;BR /&gt;C 3&lt;BR /&gt;C 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data new;&lt;BR /&gt;set test;&lt;BR /&gt;by group;&lt;BR /&gt;retain id2;&lt;/P&gt;
&lt;P&gt;if first.group=1 then&lt;BR /&gt;id2 = .;&lt;/P&gt;
&lt;P&gt;if first.group=0 and id=0 then&lt;BR /&gt;do;&lt;BR /&gt;id2=0;&lt;BR /&gt;id=id2;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;if id2=. then&lt;BR /&gt;id3 = id;&lt;BR /&gt;else id3=id2;&lt;BR /&gt;keep group id3;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2024 17:03:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947171#M370829</guid>
      <dc:creator>JOL</dc:creator>
      <dc:date>2024-10-11T17:03:26Z</dc:date>
    </item>
    <item>
      <title>Re: Replace zeros  after first zero for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947173#M370830</link>
      <description>&lt;P&gt;If the actual goal is "set all values of Id to 0 within a group after the first 0" then this is one way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by group;
  retain replaceflag;
  if first.group then replaceflag=0;
  if id=0 then replaceflag=1;
  if replaceflag then id=0;
  drop replaceflag;
run;&lt;/PRE&gt;
&lt;P&gt;How this works:&lt;/P&gt;
&lt;P&gt;When you use a BY group in a data step each variable on the BY statement has temporary (not written to the data set) variables First.var and Last.var that indicate whether the current observation is the first or last of the group. The values are 1, true, and 0, false, so can be used to test when group changes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;RETAIN means keep values of the variable across the data step boundary.&lt;/P&gt;
&lt;P&gt;So we set the Replaceflag to 0 at the start of the group. When the first 0 is encountered we change the flag to 1 (true).&lt;/P&gt;
&lt;P&gt;So if Replaceflag is true set the value of Id to 0.&lt;/P&gt;
&lt;P&gt;Redundant a bit for the first Id=0 but easier to do than to try to not set an already existing value of 0 to 0.&lt;/P&gt;
&lt;P&gt;Drop the Replaceflag from the output data set as likely not needed later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: You did not actually provide rules and your subject line is poorly stated.&lt;/P&gt;
&lt;P&gt;It also does not provide any idea if MISSING values should be assigned 0 or not. The above code will assign missing Id to 0 after the Replaceflag is set.&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/390518"&gt;@pavank&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;using below dataset and get below output data sc;&lt;BR /&gt;input group $ id ;&lt;BR /&gt;datalines;&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;A 0&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;B 2&lt;BR /&gt;B 0&lt;BR /&gt;B 2&lt;BR /&gt;B 2&lt;BR /&gt;C 3&lt;BR /&gt;C 0&lt;BR /&gt;C 3&lt;BR /&gt;C 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/*output*/&lt;BR /&gt;Name id&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;A 0&lt;BR /&gt;A 0&lt;BR /&gt;A 0&lt;BR /&gt;B 2&lt;BR /&gt;B 0&lt;BR /&gt;B 0&lt;BR /&gt;B 0&lt;BR /&gt;C 3&lt;BR /&gt;C 0&lt;BR /&gt;C 0&lt;BR /&gt;C 0&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2024 17:15:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947173#M370830</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-11T17:15:05Z</dc:date>
    </item>
    <item>
      <title>Re: Replace zeros  after first zero for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947201#M370832</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you very much for your solution&amp;nbsp; I thought &lt;STRONG&gt;COALESCE&amp;nbsp;&lt;/STRONG&gt;function works here&lt;/P&gt;</description>
      <pubDate>Sat, 12 Oct 2024 05:42:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947201#M370832</guid>
      <dc:creator>pavank</dc:creator>
      <dc:date>2024-10-12T05:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Replace zeros  after first zero for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947208#M370833</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you very much for your solution&amp;nbsp; I thought &lt;STRONG&gt;COALESCE&amp;nbsp;&lt;/STRONG&gt;function works here&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since Coalesce, or Coalescec, involve selecting the first non-missing value from a list of values I am not sure where you are thinking of applying that function. None of your observations showed a missing value for any of the variables.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Oct 2024 12:45:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947208#M370833</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-12T12:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: Replace zeros  after first zero for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947214#M370834</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you very much for your solution&amp;nbsp; I thought &lt;STRONG&gt;COALESCE&amp;nbsp;&lt;/STRONG&gt;function works here&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Interesting that you ask about the COALESCE function because I&amp;nbsp;used that yesterday for an alternative to &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151645" target="_blank" rel="noopener"&gt;JOL&lt;/A&gt;'s solution. But then I saw that &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884" target="_blank" rel="noopener"&gt;ballardw&lt;/A&gt; had suggested a quite similar DATA step in the meantime and so I didn't post mine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here it is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_);
set sc;
by group;
if id=0 then _+0;
else if first.group then _=.;
else id=coalesce(_,id);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Note that the underscore is a valid variable name -- one that is rather unlikely to collide with a name of one of your existing variables.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Oct 2024 17:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-zeros-after-first-zero-for-each-group/m-p/947214#M370834</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-10-12T17:20:22Z</dc:date>
    </item>
  </channel>
</rss>

