<?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 calculate by using value from another observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-by-using-value-from-another-observation/m-p/965857#M375979</link>
    <description>&lt;P&gt;In the absence of sample data in the form of a working DATA step, here is untested code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code assumes every EOS record is preceded by a matching WBC record:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array var     aval    anrlo     anrhi;
  array pct     lborres lbornrlo  lborrnrhi;

  do over var;
    var=ifn(param='EOS',sum(0,lag(var))*pct/100,var);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The reason for the "sum(0,lag(var))" expression is to avoid an error message with the first observation, for which lag(var) is missing, and therefore would cause a missing value result when multiplied by "pct/100").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could test for matching WBC record with 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;
  array var     aval    anrlo     anrhi;
  array pct     lborres lbornrlo  lborrnrhi;

  do over var;
    var=ifn(param='EOS' and lag(param)='WBC' 
              and subjid=lag(subjid) and avisit=lag(avisit)
           ,sum(0,lag(var))*pct/100
           ,var);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Again, untested.&lt;/P&gt;</description>
    <pubDate>Tue, 06 May 2025 04:12:35 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2025-05-06T04:12:35Z</dc:date>
    <item>
      <title>how to calculate by using value from another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-by-using-value-from-another-observation/m-p/965852#M375978</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 9 records that contain missing SI values (in 10^9/L - &lt;STRONG&gt;LBSTRESU&lt;/STRONG&gt;) for paramcd= '&lt;STRONG&gt;EOS&lt;/STRONG&gt;' test while raw values (in % unit- &lt;STRONG&gt;LBORRES&lt;/STRONG&gt;) are provided,&amp;nbsp; I would need to add the following derivation to &amp;nbsp;convert values to the SI units in my analysis dataset (variable &lt;STRONG&gt;AVAL&lt;/STRONG&gt;&amp;nbsp;) to handle the missing values while raw values (in % unit) are provided:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EOS (SI unit, 10^9/L, Column AVAL) = WBC (10^9/L, Column AVAL from WBC test at the same time/visit - AVISIT) x EOS in percentage % (Column LBORRES from EOS test at the same time/visit - AVISIT) , where WBC is the lab result from the same patient at the same time/visit (i.e., both WBC and EOS are collected at the same time - AVISIT).&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;current data (green are missing - need calculated values):&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HitmonTran_0-1746501086417.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106756iBD0D32C54696B974/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HitmonTran_0-1746501086417.png" alt="HitmonTran_0-1746501086417.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;want (calculated values are color-coded. Previously green colored -&amp;nbsp;&lt;/STRONG&gt;eg 4.5 * .04 = 0.18)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HitmonTran_0-1746499947528.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106754iF4962D7B27B26EC4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HitmonTran_0-1746499947528.png" alt="HitmonTran_0-1746499947528.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;attached is the data in excel format (sheet 3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Tue, 06 May 2025 03:21:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-by-using-value-from-another-observation/m-p/965852#M375978</guid>
      <dc:creator>HitmonTran</dc:creator>
      <dc:date>2025-05-06T03:21:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to calculate by using value from another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-by-using-value-from-another-observation/m-p/965857#M375979</link>
      <description>&lt;P&gt;In the absence of sample data in the form of a working DATA step, here is untested code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code assumes every EOS record is preceded by a matching WBC record:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array var     aval    anrlo     anrhi;
  array pct     lborres lbornrlo  lborrnrhi;

  do over var;
    var=ifn(param='EOS',sum(0,lag(var))*pct/100,var);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The reason for the "sum(0,lag(var))" expression is to avoid an error message with the first observation, for which lag(var) is missing, and therefore would cause a missing value result when multiplied by "pct/100").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could test for matching WBC record with 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;
  array var     aval    anrlo     anrhi;
  array pct     lborres lbornrlo  lborrnrhi;

  do over var;
    var=ifn(param='EOS' and lag(param)='WBC' 
              and subjid=lag(subjid) and avisit=lag(avisit)
           ,sum(0,lag(var))*pct/100
           ,var);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Again, untested.&lt;/P&gt;</description>
      <pubDate>Tue, 06 May 2025 04:12:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-by-using-value-from-another-observation/m-p/965857#M375979</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-05-06T04:12:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to calculate by using value from another observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-by-using-value-from-another-observation/m-p/965858#M375980</link>
      <description>&lt;P&gt;That would be very easy as long as your data structure looks like what you showed. (a.ka. two obs for each subject and each visit ,first is k/ul ,second is %)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject param $ visit aval lb;
cards;
1 WBC 1 4.5 4.5
1 EOD 1 .   4
1 WBC 2 8.1 8.1
1 EOD 2 .   0.2
2 WBC 1 4.5 4.5
2 EOD 1 .   2
2 WBC 2 8.1 8.1
2 EOD 2 .   0.2
;
data want;
 set have;
 lag_aval=lag(aval);
 if missing(aval) then aval=lag_aval*lb*0.01;
 drop lag_aval;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 May 2025 05:09:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-by-using-value-from-another-observation/m-p/965858#M375980</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-05-06T05:09:14Z</dc:date>
    </item>
  </channel>
</rss>

