<?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: update data in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/583054#M165943</link>
    <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp; ! As a trainee &amp;nbsp;I have leart a lot from your code!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 22 Aug 2019 05:30:47 GMT</pubDate>
    <dc:creator>duanzongran</dc:creator>
    <dc:date>2019-08-22T05:30:47Z</dc:date>
    <item>
      <title>update data in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/582347#M165624</link>
      <description>&lt;P&gt;data lin;&lt;BR /&gt;input x y;&lt;BR /&gt;datalines;&lt;BR /&gt;1 3&lt;BR /&gt;4 5&lt;BR /&gt;2 .&lt;BR /&gt;5 .&lt;BR /&gt;6 .&lt;BR /&gt;7 .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;dear all,I want to update the missing &amp;nbsp;variable y &amp;nbsp;. The rules are below:&lt;/P&gt;&lt;P&gt;(1*3+4*5)/(1+4)=4.6 &amp;nbsp;as the 3rd obs&amp;nbsp;&lt;/P&gt;&lt;P&gt;(4*5+2*4.6)/(4+2)=4.87 as the 4rth obs&amp;nbsp;&lt;/P&gt;&lt;P&gt;........&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the expected &amp;nbsp;result is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="捕获.PNG" style="width: 233px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31857i719D2CD68D7F0E8B/image-size/large?v=v2&amp;amp;px=999" role="button" title="捕获.PNG" alt="捕获.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I do the job like the follows ,but&amp;nbsp;&lt;SPAN&gt;It looks a little silly&lt;/SPAN&gt;. &amp;nbsp;is there a simple way or other method ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro macro_data3();&lt;BR /&gt;data _null_;&lt;BR /&gt;set lin nobs=nobs;&lt;BR /&gt;call symput("nobs",nobs);&lt;BR /&gt;run;&lt;BR /&gt;/*%put &amp;amp;nobs.;*/&lt;/P&gt;&lt;P&gt;data lin3;&lt;BR /&gt;set lin;&lt;BR /&gt;/*if _n_ in (1,2) then z=y;*/&lt;BR /&gt;%do i=1 %to &amp;amp;nobs.;&lt;BR /&gt;z=(lag(y)*lag(x)+lag2(y)*lag2(x))/(lag(x)+lag2(x));&lt;BR /&gt;if y eq . then y=z;&lt;BR /&gt;%end;&lt;BR /&gt;drop z;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%macro_data3();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ANY suggestion is welcome！ Thanks in advance！&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 08:50:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/582347#M165624</guid>
      <dc:creator>duanzongran</dc:creator>
      <dc:date>2019-08-20T08:50:55Z</dc:date>
    </item>
    <item>
      <title>Re: update data in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/582351#M165626</link>
      <description>&lt;P&gt;Here is one way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lin;
input x y;
datalines;
1 3
4 5
2 .
5 .
6 .
7 .
;

data want(drop=obs);
    array _x[0:1] _temporary_;
    array _y[0:1] _temporary_;  
 
    do obs=1 by 1 until (lr);         
        set lin end=lr;
        if y=. then y=sum(_x[0]*_y[0], _x[1]*_y[1])/sum(of _x[*]);
        output;
        _x[mod(obs, 2)]=x;
        _y[mod(obs, 2)]=y;  
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Aug 2019 09:12:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/582351#M165626</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-20T09:12:32Z</dc:date>
    </item>
    <item>
      <title>Re: update data in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/582357#M165631</link>
      <description>&lt;P&gt;Another variation in using Array as given by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
   set lin;
   array kx[7] _temporary_;
   array ky[7] _temporary_;
   kx[_n_] = x;
   ky[_n_] = y;
   yy = y;
   if missing(ky[_n_]) then do;
      ky[_n_] = (kx[_n_ - 1] * ky[_n_ - 1] + kx[_n_ - 2] * ky[_n_ - 2]) / 
                                    (kx[_n_ - 1] + kx[_n_ - 2]) ;
      if missing(y) then yy = ky[_n_];
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited: The following statement can be rewritten&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if missing(y) then yy = ky[_n_];&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;as&lt;/P&gt;
&lt;PRE&gt; yy = ky[_n_];&lt;/PRE&gt;
&lt;P&gt;The array-size can be made dynamic by getting the number of observations as a macro variable from outside this data step.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 10:23:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/582357#M165631</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-08-20T10:23:44Z</dc:date>
    </item>
    <item>
      <title>Re: update data in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/582526#M165693</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/266020"&gt;@duanzongran&lt;/a&gt;&amp;nbsp; &amp;nbsp;Nice fun puzzle, sets of 2 (window length 2) is pretty straight forward as we can afford to type and assign lag1 and lag2.&amp;nbsp; Sorry missed the party earlier.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lin;
input x y;
datalines;
1 3
4 5
2 .
5 .
6 .
7 .
;
run;


data want;
do _n_=1,2 until(z);
 set lin end=z;
 array t(2) _temporary_ ;
 _l1=lag(x);
 _l2=lag2(x);
 if y=. then y=sum(of t(*))/sum(of _l:);
 t(_n_)=y*x;
 output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Aug 2019 18:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/582526#M165693</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-20T18:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: update data in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/583052#M165941</link>
      <description>&lt;P&gt;Thank YOU! &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;You inspired me。&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;AS a trainee i could do the job like this.：&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data lin;&lt;/DIV&gt;&lt;DIV&gt;input x y;&lt;/DIV&gt;&lt;DIV&gt;datalines;&lt;/DIV&gt;&lt;DIV&gt;1 3&lt;/DIV&gt;&lt;DIV&gt;4 5&lt;/DIV&gt;&lt;DIV&gt;2 .&lt;/DIV&gt;&lt;DIV&gt;5 .&lt;/DIV&gt;&lt;DIV&gt;6 .&lt;/DIV&gt;&lt;DIV&gt;7 .&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data want(keep=x y);&lt;/DIV&gt;&lt;DIV&gt;do obs=1 by 1 until (lr);&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set lin end=lr;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if y=. then y=sum(x1*y1, x2*y2)/sum(x1,x2);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;/DIV&gt;&lt;DIV&gt;x1=x;x2=lag(x);&lt;/DIV&gt;&lt;DIV&gt;y1=y;y2=lag(y);&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; end;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2019 05:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/583052#M165941</guid>
      <dc:creator>duanzongran</dc:creator>
      <dc:date>2019-08-22T05:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: update data in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/583053#M165942</link>
      <description>&lt;P&gt;thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;&amp;nbsp;!&amp;nbsp;&lt;SPAN&gt;I thought about it before, but you did it. the way is just like transposing &amp;nbsp;the data.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2019 05:28:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/583053#M165942</guid>
      <dc:creator>duanzongran</dc:creator>
      <dc:date>2019-08-22T05:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: update data in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/583054#M165943</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp; ! As a trainee &amp;nbsp;I have leart a lot from your code!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2019 05:30:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/update-data-in-data-step/m-p/583054#M165943</guid>
      <dc:creator>duanzongran</dc:creator>
      <dc:date>2019-08-22T05:30:47Z</dc:date>
    </item>
  </channel>
</rss>

