<?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: Conditionally replace zeros with repeating values across columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326764#M72833</link>
    <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;If you want to make several variable affectations in your "THEN" clause, then separating them with "AND" is not the way to do it.&lt;BR /&gt;(X=Y AND W=Z) is a boolean that will be 1 if the expression is true and 0 otherwise.&lt;BR /&gt;&lt;BR /&gt;You have to enclose your instructions with "DO" and "END"&lt;BR /&gt;&lt;BR /&gt;if (...) then do;&lt;BR /&gt;...&lt;BR /&gt;...&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;A better option here would be to use arrays.&lt;/P&gt;</description>
    <pubDate>Mon, 23 Jan 2017 16:06:02 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2017-01-23T16:06:02Z</dc:date>
    <item>
      <title>Conditionally replace zeros with repeating values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326758#M72828</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;SPAN&gt;I have a data set consisting of an ID and six ordered numerical columns (Largest, Second, Third, ... Sixth). &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Have:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID Largest Second Third Fourth Fifth Sixth&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;98 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 97 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;96 &amp;nbsp; &amp;nbsp; 85 &amp;nbsp; &amp;nbsp; &amp;nbsp; 70 &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;77 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;61 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 55 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp;44 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is there a way, in a Data step (or some other means), to get the non-zero values to replace the zero values in order, and in some cases repeat? See example below. Also, apologies if this has been answered elsewhere! My search fu may be a little lacking.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Want:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID Largest Second Third Fourth Fifth Sixth&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;98 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 97 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;96 &amp;nbsp; &amp;nbsp; 85 &amp;nbsp; &amp;nbsp; &amp;nbsp; 70 &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;77 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 77 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;77 &amp;nbsp; &amp;nbsp; 77 &amp;nbsp; &amp;nbsp; &amp;nbsp; 77 &amp;nbsp; &amp;nbsp; 77&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;61 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 55 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;61 &amp;nbsp; &amp;nbsp; 55 &amp;nbsp; &amp;nbsp; &amp;nbsp; 61 &amp;nbsp; &amp;nbsp; 55&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp;44 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12 &amp;nbsp; &amp;nbsp; 44 &amp;nbsp; &amp;nbsp; &amp;nbsp; 30 &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried some IF THEN ELSE IF logic in a Data step which&amp;nbsp;doesn't seem to stick. Any help is certainly appreciated!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Want;
SET Have;
IF SECOND=0 AND THIRD=0 AND FOURTH=0 AND FIFTH=0 AND SIXTH=0
           THEN SECOND=Largest
                      AND THIRD=Largest
                      AND FOURTH=Largest
                      AND FIFTH=Largest
                      AND SIXTH=Largest;
ELSE IF SECOND ne 0 AND THIRD=0 AND FOURTH=0 AND FIFTH=0 AND SIXTH=0
           THEN THIRD=Largest
                      AND FOURTH=SECOND
                      AND FIFTH=Largest
                      AND SIXTH=SECOND;
ELSE IF SECOND ne 0 AND THIRD ne 0 AND FOURTH=0 AND FIFTH=0 AND SIXTH=0
           THEN FOURTH=Largest
                      AND FIFTH=SECOND
                      AND SIXTH=THIRD;
ELSE IF SECOND ne 0 AND THIRD ne 0 AND FOURTH ne 0 AND FIFTH=0 AND SIXTH=0
           THEN FIFTH=Largest
                      AND SIXTH=SECOND;
ELSE IF SECOND ne 0 AND THIRD ne 0 AND FOURTH ne 0 AND FIFTH ne 0 AND SIXTH=0
           THEN SIXTH=Largest;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Jan 2017 15:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326758#M72828</guid>
      <dc:creator>DMDykstra</dc:creator>
      <dc:date>2017-01-23T15:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally replace zeros with repeating values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326761#M72830</link>
      <description>&lt;P&gt;I should also note that the variables have a suffix that I didn't include in my example, such that the functions of Largest, Second, etc. are not actually being called. Sorry for the confusion.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 16:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326761#M72830</guid>
      <dc:creator>DMDykstra</dc:creator>
      <dc:date>2017-01-23T16:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally replace zeros with repeating values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326764#M72833</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;If you want to make several variable affectations in your "THEN" clause, then separating them with "AND" is not the way to do it.&lt;BR /&gt;(X=Y AND W=Z) is a boolean that will be 1 if the expression is true and 0 otherwise.&lt;BR /&gt;&lt;BR /&gt;You have to enclose your instructions with "DO" and "END"&lt;BR /&gt;&lt;BR /&gt;if (...) then do;&lt;BR /&gt;...&lt;BR /&gt;...&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;A better option here would be to use arrays.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 16:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326764#M72833</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-01-23T16:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally replace zeros with repeating values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326765#M72834</link>
      <description>&lt;P&gt;Here's one example. Note how to post example data in datastep.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input ID Largest Second Third Fourth Fifth Sixth;
datalines;
1      98         97        96     85       70      1
2      77          0          0        0        0       0
3      61         55         0        0        0       0
4      44         30        12       0        0       0
;
run;

data want;
   set have;
   array v Largest Second Third Fourth Fifth Sixth;
   zpos = whichn(0,of v(*));
   if zpos &amp;gt; 1 then do i=zpos to dim(v);
      r=v[zpos-1];
      v[i] = r;
   end;
   /* drop zpos i r; after debugging uncomment*/
run;
&lt;/PRE&gt;
&lt;P&gt;Replace Largest etc with your variable names. It is up to you to sort the values for this to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 16:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326765#M72834</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-23T16:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally replace zeros with repeating values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326773#M72836</link>
      <description>&lt;P&gt;Here is my tested code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Largest Second Third Fourth Fifth Sixth;
datalines;
1      98         97        96     85       70      1
2      77          0          0        0        0       0
3      61         55         0        0        0       0
4      44         30        12       0        0       0 
;run;

data want;
 set have;
     array vx Largest Second Third Fourth Fifth Sixth;
     do i=1 to dim(vx);
        if vx(i) = 0 then do;
           if i=1 then delete;  /* when starts with 0 - ??? */
           else do;
            m=i-1;
            link fulfill;
           end;
        end;
     end;
     drop i j m;
return;
fulfill: 
   j=1;
   do until(m=0);
      vx(i) = vx(j);
      i+1;
      if i &amp;gt; dim(vx) then m=0;
      else do; 
      j+1;
      if j &amp;gt; m then j=1;
      end;
   end;
return;
run;&lt;/CODE&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 16:48:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326773#M72836</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-01-23T16:48:32Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally replace zeros with repeating values across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326788#M72845</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;Assuming that zero values always are in the end, here is a simple solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array values Largest Second Third Fourth Fifth Sixth;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop i j firstempty;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * find first zero-value column - i.e. length of series to repeat + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i = &lt;STRONG&gt;2&lt;/STRONG&gt; to &lt;STRONG&gt;6&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if values{i} = &lt;STRONG&gt;0&lt;/STRONG&gt; then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; firstempty = i;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; leave;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * replace with value from corresponding element in non-zero series;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if firstempty ne &lt;STRONG&gt;.&lt;/STRONG&gt; then do j = firstempty to &lt;STRONG&gt;6&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; values{j} = values{j - firstempty + &lt;STRONG&gt;1&lt;/STRONG&gt;};&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2017 17:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-replace-zeros-with-repeating-values-across-columns/m-p/326788#M72845</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2017-01-23T17:42:39Z</dc:date>
    </item>
  </channel>
</rss>

