<?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: propagate value vertically in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884479#M349431</link>
    <description>&lt;P&gt;Thanks for the solution but unfortunately I don't have the sas package which includes the proc expand&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jul 2023 13:59:55 GMT</pubDate>
    <dc:creator>mariopellegrini</dc:creator>
    <dc:date>2023-07-12T13:59:55Z</dc:date>
    <item>
      <title>propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884447#M349416</link>
      <description>&lt;P&gt;I'm trying to propagate a value vertically, but only in case for every value of k there is at least one non-missing data. I inserted the proc sort to bring the non-missing values to the beginning. This is the example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_test;
input k value;
datalines;
1 100
1 .
1 .
2 .
2 200
2 .
3 .
3 .
4 150
4 .
4 200
;
proc sort data=ds_test;
by k descending value;
run;

data d;
set ds_test;
retain _value;
if not missing(value) then _value=value;
else value=_value;
drop _value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With the code I wrote I don't get the desired result because in the case of k=3 value is always missing.&lt;/P&gt;
&lt;P&gt;Furthermore, in the case of k=4 I would like the value of 150 to propagate before 200, according to the vertical order in which it occurs.&lt;/P&gt;
&lt;P&gt;What I would like to achieve is this result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d;
input k value;
datalines;
1 100
1 100
1 100
2 100
2 200
2 200
3 .
3 .
4 150
4 150
4 200
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 07:59:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884447#M349416</guid>
      <dc:creator>mariopellegrini</dc:creator>
      <dc:date>2023-07-12T07:59:34Z</dc:date>
    </item>
    <item>
      <title>Re: propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884471#M349428</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_test;
input k value;
datalines;
1 100
1 .
1 .
2 .
2 200
2 .
3 .
3 .
4 150
4 .
4 200
;
run;

PROC EXPAND data=ds_test
            out=ds_test_out
            method=step;
run;

proc means data=ds_test nway noprint;
 CLASS k;
 VAR   value;
 output out=all_missing(drop=_:) sum= / autoname;
run;

data d(drop=time value_sum);
 merge ds_test_out all_missing;
 by k;
 if value_sum=. then value=.;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Koen&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 13:16:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884471#M349428</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-07-12T13:16:03Z</dc:date>
    </item>
    <item>
      <title>Re: propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884479#M349431</link>
      <description>&lt;P&gt;Thanks for the solution but unfortunately I don't have the sas package which includes the proc expand&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 13:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884479#M349431</guid>
      <dc:creator>mariopellegrini</dc:creator>
      <dc:date>2023-07-12T13:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884482#M349432</link>
      <description>&lt;P&gt;Not sure why you sorted that data as the results aren't going to match your desired description.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try:&lt;/P&gt;
&lt;PRE&gt;/* DO not SORT DS_TEST*/&lt;BR /&gt;data d;
set ds_test;
by k notsorted;
retain _value;
if first.k then _value=value;
if not missing(value) then _value=value;
else value=_value;
drop _value;
run;&lt;/PRE&gt;
&lt;P&gt;The BY statement means that SAS creates automatic variables first.(variablename) and last.(variablename) for each variable on the by statement. These are numeric 1/0 for true/false indicating that the current observation is the first or last of a combination of the variables.&amp;nbsp; So you can use that to reset the retained variable(s) when the group membership changes.&lt;/P&gt;
&lt;P&gt;Then the data set is processed in order. The option NOTSORTED on the By statement allows the By variables to be treated as grouped but does not require them to be in sort order.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 14:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884482#M349432</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-07-12T14:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884486#M349434</link>
      <description>&lt;P&gt;This sounds like it is standard LOCF (last observation carry forward) within each ID, but with a look-ahead to find the first non-missing value for each ID.&amp;nbsp; I would use a double-DoW loop for this.&amp;nbsp; This reads through each ID by-group twice.&amp;nbsp; The first time to find the first non-missing value.&amp;nbsp; The second time to do the LOCF.&amp;nbsp; The code inside the LOCF DO loop is similar to yours.&amp;nbsp; Note you do not want a RETAIN statement for this solution, because the DATA step appropriately sets _value to missing at the top of the DATA step loop (i.e. at the beginning of each by-group).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_test;
input k value;
datalines;
1 100
1 .
1 .
2 .
2 200
2 .
3 .
3 .
4 150
4 .
4 200
;

data d;
  do until(last.k) ;
    set ds_test;
    by k ;
    if missing(_value) then _value=value ;
  end ;

  do until(last.k) ;
    set ds_test;
    by k ;

    if not missing(value) then _value=value;
    else value=_value;

    output ;
  end ;

  drop _: ;
run;

proc print ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jul 2023 14:39:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884486#M349434</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-07-12T14:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884522#M349449</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_test;
input k value @@;
datalines;
1 100 1 .   1 .   
2 .   2 200 2 .
3 .   3 .
4 150 4 . 4 200
5 .   5 . 5 150 5 . 5 200
;;;;

proc print;
   run;
options msglevel=I;
data want;
   update ds_test(obs=0) ds_test(in=in1);
   by k;
   merge ds_test(keep=k) ds_test(rename=(value=value0) where=(not missing(value0)));
   by k;
   if in1;
   value = coalesce(value,value0);
   output;
   drop value0;
   run;
proc print;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 98px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85742i6A2B85DCAC853EFD/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 17:09:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884522#M349449</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2023-07-12T17:09:43Z</dc:date>
    </item>
    <item>
      <title>Re: propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884530#M349453</link>
      <description>For the first observation with k=2, did you mean for the result to be 100 (continued from k=1)?&lt;BR /&gt;It makes a difference in the list of possible solutions.</description>
      <pubDate>Wed, 12 Jul 2023 17:46:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884530#M349453</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-07-12T17:46:18Z</dc:date>
    </item>
    <item>
      <title>Re: propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884597#M349482</link>
      <description>&lt;P&gt;You don't really need to read each K-group twice, but you do need to know if the k-group in hand has at least one nonmissing VALUE.&amp;nbsp; In such a case, carry forward the most recent nonmissing VALUE.&amp;nbsp; If not then set the carry forward value to missing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  merge ds_test (where=(value^=.) in=nonmissing_value_found)
        ds_test ;
  by k;

  if first.k and nonmissing_value_found=0 then call missing(_most_recent_value);
  retain _most_recent_value;
  value=coalesce(value,_most_recent_value);
  _most_recent_value=value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's vital to this program that the two arguments of the MERGE has the non-missing subset listed first, and the entire set second, so that &lt;STRIKE&gt;any&lt;/STRIKE&gt; each observation preserves all its values (values from rightmost common variable prevails).&amp;nbsp; &amp;nbsp;The important element is generating the NONMISSING_VALUE_FOUND dummy for each K.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, if a given K-group has NO non-missing VALUEs, the IF ... statement sets the carry forward VALUE to missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, this program assumes you want to carry-forward non-missing VALUEs across K-groups, as your sample shows for the first obs of K=2, per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s comment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, your original data needs no further sorting.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2023 01:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884597#M349482</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-13T01:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: propagate value vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884644#M349499</link>
      <description>&lt;P&gt;Thanks for the solution. I would also like to include your post as a solution but I don't think it's possible to put more than one solution&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2023 10:50:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/propagate-value-vertically/m-p/884644#M349499</guid>
      <dc:creator>mariopellegrini</dc:creator>
      <dc:date>2023-07-13T10:50:09Z</dc:date>
    </item>
  </channel>
</rss>

