<?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 How to calculate a variable based on previous observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-previous-observation/m-p/512093#M137889</link>
    <description>&lt;P&gt;Hi, can you help me to see how to program it in SAS to calculate y for my below data? The data are already sorted by cust_id and period. The orginal data does not have y. The calculated y should look like the one in my data below. The requirement for y: y=x1 for the first period of a cust_id (this is how the first number of y, that is 1000 is calculated). y=x2 of the previous period multiply by x3 of the current period (this is how the second number of y, that is 6 is calculated: x2 of the previous period, that is 3, multiply by x3 of the current period, that is 2) I guess it might need retain or lag, but I am not sure.&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/24810i9FDAD68AC3B38C76/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.png" alt="Untitled.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Nov 2018 05:54:58 GMT</pubDate>
    <dc:creator>yunfeizhao100</dc:creator>
    <dc:date>2018-11-12T05:54:58Z</dc:date>
    <item>
      <title>How to calculate a variable based on previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-previous-observation/m-p/512093#M137889</link>
      <description>&lt;P&gt;Hi, can you help me to see how to program it in SAS to calculate y for my below data? The data are already sorted by cust_id and period. The orginal data does not have y. The calculated y should look like the one in my data below. The requirement for y: y=x1 for the first period of a cust_id (this is how the first number of y, that is 1000 is calculated). y=x2 of the previous period multiply by x3 of the current period (this is how the second number of y, that is 6 is calculated: x2 of the previous period, that is 3, multiply by x3 of the current period, that is 2) I guess it might need retain or lag, but I am not sure.&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/24810i9FDAD68AC3B38C76/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.png" alt="Untitled.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 05:54:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-previous-observation/m-p/512093#M137889</guid>
      <dc:creator>yunfeizhao100</dc:creator>
      <dc:date>2018-11-12T05:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate a variable based on previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-previous-observation/m-p/512094#M137890</link>
      <description>Your post may have been garbled by the forum. Please insert data and/or code using the little icons on the editor (running man or { i }). It helps if we can clearly see the data/question.</description>
      <pubDate>Mon, 12 Nov 2018 05:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-previous-observation/m-p/512094#M137890</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-12T05:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate a variable based on previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-previous-observation/m-p/512415#M138003</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/245869"&gt;@yunfeizhao100&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is very difficult for SAS to read data from pictures. (Maybe this is why no solution has been provided yet.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With sample data in the form of a DATA step ...&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cust_id period x1-x3;
cards;
1 1 1000 3 3
1 2 1000 5 2
1 3 1000 6 4
1 4 1000 7 6
1 5 1000 3 2
2 1 2000 6 5
2 2 2000 4 6
2 3 2000 3 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;... testing solutions&amp;nbsp;is much easier. A simple solution could look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by cust_id;
prev_x2=lag(x2);
if first.cust_id then y=x1;
else y=prev_x2*x3;
drop prev_x2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For each observation the &lt;FONT face="courier new,courier"&gt;x2&lt;/FONT&gt; value of the previous observation is stored in variable &lt;FONT face="courier new,courier"&gt;prev_x2&lt;/FONT&gt;. Then, &lt;FONT face="courier new,courier"&gt;y=x1&lt;/FONT&gt; for the first observation of each &lt;FONT face="courier new,courier"&gt;cust_id&lt;/FONT&gt; BY group, and &lt;FONT face="courier new,courier"&gt;y=prev_x2*x3&lt;/FONT&gt; for the remaining observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, the same results can be obtained with more concise code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by cust_id;
y=ifn(first.cust_id, x1, lag(x2)*x3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But this (without further precautions) would cause an undesirable note in the log:&lt;/P&gt;
&lt;PRE&gt;NOTE: Missing values were generated as a result of performing an operation on missing values.
...&lt;/PRE&gt;
&lt;P&gt;(due to the missing value of &lt;FONT face="courier new,courier"&gt;lag(x2)&lt;/FONT&gt; in the very first observation).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both solutions assume that your sample data is representative of your real data, i.e.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The first &lt;EM&gt;observation&lt;/EM&gt; of each &lt;FONT face="courier new,courier"&gt;cust_id&lt;/FONT&gt; BY group represents the first &lt;EM&gt;period&lt;/EM&gt; of the respective &lt;FONT face="courier new,courier"&gt;cust_id&lt;/FONT&gt;. (Note that I didn't use variable &lt;FONT face="courier new,courier"&gt;period&lt;/FONT&gt;.)&lt;/LI&gt;
&lt;LI&gt;Similarly, the second, third, ... observation&amp;nbsp;&lt;SPAN&gt;of each&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;cust_id&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;BY group represents the second, third, ...&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;period&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;of the respective&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;cust_id&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;(so that "previous observation" and "previous period" are equivalent for these observations).&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;If these assumptions are not met or if &lt;FONT face="courier new,courier"&gt;x2&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;x3&lt;/FONT&gt; can have missing values (and you want to avoid those notes in the log about missing values), we'll need to modify the suggested code.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 23:41:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-previous-observation/m-p/512415#M138003</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-11-12T23:41:41Z</dc:date>
    </item>
  </channel>
</rss>

