<?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: How to Derive a weight based on previous weight . in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504087#M134852</link>
    <description>&lt;P&gt;Just change a line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if abs(pdiff) &amp;lt;= 10 then dwt = save_bwt; 
	             else dwt = cwt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Pay attention to &lt;STRONG&gt;ABS()&lt;/STRONG&gt; function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 14 Oct 2018 11:26:34 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2018-10-14T11:26:34Z</dc:date>
    <item>
      <title>How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/503666#M134635</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have below data where i need to create a variable&amp;nbsp;&lt;U&gt;Derived weigh&lt;/U&gt;t (Derived_wt)&amp;nbsp;based on the percentage difference between two weights.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In below data subject is having the corresponding weight&amp;nbsp;(Current_wt)&amp;nbsp;for each visit.&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) For first visit we need to find out percentage difference between&amp;nbsp;Current_wt&amp;nbsp;and&amp;nbsp;Base_wt.&amp;nbsp;If percentage difference is within 10 % then we should populate&amp;nbsp;Derived_wt&amp;nbsp;=&amp;nbsp;Base_wt.&amp;nbsp;Else we should populate&amp;nbsp;Derived_wt = Current_wt.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) From visit = 2, the scenario changes as :&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;for second visit I need&amp;nbsp;to populate&amp;nbsp;Base_wt&amp;nbsp; =&amp;nbsp;Derived_wt (Which is derived in previous step).&amp;nbsp;Then i have to find out the percentage difference between&amp;nbsp;Current_wt and base_wt,&amp;nbsp;again if difference is within 10% then we have to populate&amp;nbsp;Derived_wt =Base_wt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Else&amp;nbsp;Derived_wt=Current_wt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3) For Visit =3, now&amp;nbsp;Base_wt =Derived_wt(Which is Derived in previous step).&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I have to find out the percentage difference between&amp;nbsp;Current_wt and base_wt,&amp;nbsp;again if difference is within 10% then we have to populate&amp;nbsp;Derived_wt =Base_wt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Else&amp;nbsp;Derived_wt=Current_wt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This should be done for all the subjects in data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I Could able to derive for first visits for all subjects .&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone please help me to derive weight for all visits for all subjects. Below is the sample data for your understanding.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;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gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;no&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;71&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;52&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;50&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;yes&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;50&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;58&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;no&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;58&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 10:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/503666#M134635</guid>
      <dc:creator>Balaji13</dc:creator>
      <dc:date>2018-10-12T10:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/503750#M134663</link>
      <description>&lt;P&gt;Please post your code and we shall help yo to accomplish it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use either LAG() function or RETAIN statement to hold previous line data.&lt;/P&gt;
&lt;P&gt;Use BY statement to enable IF FIRST.SUBJECT checking.&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
    by subject;   /* assuming data is ordered by subject visit */
         retain save_base_wt;
         if first.subject then save_base_wt = base_wt;
         if visit=1 then derived_wt = save_BASE_WT;
         else do;
              ...........
         end;
run;       &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Oct 2018 15:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/503750#M134663</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-12T15:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504023#M134826</link>
      <description>&lt;P&gt;Hi Shmuel,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply. Attached my code for your reference.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am able to derive the derived weight for Visit 1 and 2. However, from visit 3 my loop is not working. Could you please help me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Oct 2018 16:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504023#M134826</guid>
      <dc:creator>Balaji13</dc:creator>
      <dc:date>2018-10-13T16:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504045#M134833</link>
      <description>&lt;P&gt;Is there a reason that you prefer check first.subject instead check visit number ?&lt;/P&gt;
&lt;P&gt;Can subject start with visit&amp;nbsp; not equal to 1 ?&lt;/P&gt;
&lt;P&gt;I see no reason for two steps (data test1; data test2;) - I believe it can be done in one step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you use a retained variable its value is saved until you change it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On any obs that is not first.subject you do&amp;nbsp;&lt;STRONG&gt;call missing(bwt);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;therefore, for each such obs you will get&amp;nbsp;&lt;STRONG&gt;n(cwt, bwt) = 1&lt;/STRONG&gt; - and you did not supply code for it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;try next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Cwt is current weight
Bwt is base weight. 
Dwt is Derived weight.
*/

data test;
input subject visit cwt bwt 8.;
datalines;
1 1 78 80
1 2 79 .
1 3 70 .
1 4 72 .
1 5 74 .
1 6 88 .
1 7 70.
2 1 51 50
2 2 58 .
;
run;

proc sort data=test;
  by subject visit;
run;

data test1;
  set test;
    by subject;   
         retain save_bwt dwt;
         if first.subject then save_bwt = bwt;
         if visit=1 then dwt = save_bwt;
         else do;
            if n(cwt, save_bwt) = 2 then pdiff = round((cwt - save_bwt)*100/save_bwt,0.1);
            if pdiff &amp;lt;= 10 then dwt = save_bwt; 
			   else dwt = cwt;
         end;
       drop save_bwt;  
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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Oct 2018 21:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504045#M134833</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-13T21:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504077#M134848</link>
      <description>&lt;P&gt;From the above code, i am getting the attached output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Derived weight(dwt) 80 is same for all visit for subject 1. When the pdiff is more than 10%.&amp;nbsp; However, i want to display dwt =cwt when pdiff is more than 10%.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For subject 2 it is working for visit 2. I am not sure, why it is not working for subject 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To answer your questions,&lt;/P&gt;&lt;P&gt;1) In real data, i will sort with subject and visit and take the first.subject as first observation for each subject.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) No, subject will not start visit not equal to 1.&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>Sun, 14 Oct 2018 09:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504077#M134848</guid>
      <dc:creator>Balaji13</dc:creator>
      <dc:date>2018-10-14T09:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504087#M134852</link>
      <description>&lt;P&gt;Just change a line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if abs(pdiff) &amp;lt;= 10 then dwt = save_bwt; 
	             else dwt = cwt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Pay attention to &lt;STRONG&gt;ABS()&lt;/STRONG&gt; function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Oct 2018 11:26:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504087#M134852</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-14T11:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504088#M134853</link>
      <description>To get results as in your first post the code should be (checking for &amp;lt; not for &amp;lt;=):&lt;BR /&gt;&lt;BR /&gt;if abs(pdiff) &amp;lt; 10 then dwt = save_bwt; &lt;BR /&gt;	             else dwt = cwt;</description>
      <pubDate>Sun, 14 Oct 2018 11:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504088#M134853</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-14T11:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504089#M134854</link>
      <description>&lt;P&gt;HI,&lt;/P&gt;&lt;P&gt;&amp;nbsp;I really appreciate your time. Thanks a lot.&amp;nbsp;There is a little catch.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As explained in my first post,&amp;nbsp;&lt;/P&gt;&lt;P&gt;For visit 1, we should first find the pdiff between cwt and bwt. If pdiff within 10% then dwt =bwt. Else dwt=cwt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;From visit 2 onwards, bwt should be equal to lag( dwt ) (derived in previous step). Now we have to find out the difference between cwt and dwt. If pdiff is within 10% then we should populate dwt =lag(dwt). if pdiff is more than 10% then dwt =cwt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for visit 3, now bwt = lag(dwt) and repeat the above steps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example with required output is in my first post.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;attached the output dataset for your reference.&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>Sun, 14 Oct 2018 12:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504089#M134854</guid>
      <dc:creator>Balaji13</dc:creator>
      <dc:date>2018-10-14T12:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504094#M134857</link>
      <description>&lt;P&gt;Pay attention - there is a slight change in data: subject-visit 1-7 and 2-1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have run the next code with data as in your first post and got exactly the expected results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Cwt is current weight
Bwt is base weight. 
Dwt is Derived weight.
*/

data test;
input subject visit cwt bwt 8.;
datalines;
1 1 78 80
1 2 79 .
1 3 70 .
1 4 72 .
1 5 74 .
1 6 88 .
1 7 71.
2 1 52 50
2 2 58 .
;
run;

proc sort data=test;
  by subject visit;
run;

data test1;
  set test;
    by subject;   
         retain save_bwt;  
         /* lag() function should be used unconditionally ! */
         if first.subject then do;
            pdiff = round((cwt - bwt)*100/bwt,0.1);
            if abs(pdiff) &amp;lt; 10 then dwt = bwt; 
	                       else dwt = cwt; 
	    save_bwt = dwt; /* for visits 2 */  
	    output;
	 end;
         else do; /* for visit 2 and following */
            if n(cwt, save_bwt) = 2 then pdiff = round((cwt - save_bwt)*100/save_bwt,0.1);
            if abs(pdiff) &amp;lt; 10 then dwt = save_bwt; 
			       else dwt = cwt;
	    save_bwt = dwt; /* for next visits */  
	    output;			   
         end;
       *drop save_bwt;  /* unmark to drop this variable from the output */
run;   
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 Oct 2018 14:08:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504094#M134857</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-14T14:08:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to Derive a weight based on previous weight .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504097#M134860</link>
      <description>&lt;P&gt;Thanks a lot&amp;nbsp; Shmuel, It is working in test data. Hope it should work in real time data.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Oct 2018 15:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Derive-a-weight-based-on-previous-weight/m-p/504097#M134860</guid>
      <dc:creator>Balaji13</dc:creator>
      <dc:date>2018-10-14T15:14:20Z</dc:date>
    </item>
  </channel>
</rss>

