<?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 take the create a variable where the next observation is conditioned on its preceding one? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-create-a-variable-where-the-next-observation-is/m-p/748260#M234995</link>
    <description>&lt;P&gt;I have a dataset with 2 variables Amount and Balance. And I want to create a third variable balance_cf:&lt;/P&gt;
&lt;P&gt;Amount&amp;nbsp; &amp;nbsp;Balance&lt;BR /&gt;109.98&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&lt;BR /&gt;25.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&lt;BR /&gt;-4500.00&amp;nbsp; &amp;nbsp;85358.75&lt;BR /&gt;941.60&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&lt;BR /&gt;104.00&amp;nbsp; &amp;nbsp; &amp;nbsp;85358.75&lt;BR /&gt;215.00&amp;nbsp; &amp;nbsp; &amp;nbsp;85358.75&lt;BR /&gt;110.00&amp;nbsp; &amp;nbsp; &amp;nbsp;85358.75&lt;/P&gt;
&lt;P&gt;In the new dataset, Balance_cf will be equal to Balance for the first observation. For the second observation onwards, Balance_cf will be equal to its preceeding observation if amount&amp;gt;=0, else if amount&amp;lt;0, then balance_cf= lag(balance_cf)+amount.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The resultant dataset will look like this:&lt;/P&gt;
&lt;P&gt;Amount&amp;nbsp; &amp;nbsp; &amp;nbsp;Balance&amp;nbsp; &amp;nbsp;Balance_cf&lt;BR /&gt;109.98&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&amp;nbsp; 85358.75&lt;BR /&gt;25.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&amp;nbsp; 85358.75&lt;BR /&gt;-4500.00&amp;nbsp; &amp;nbsp;85358.75 80858.75&lt;BR /&gt;941.60&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;104.00&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;215.00&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;110.00&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;47.72&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;-104.00&amp;nbsp; &amp;nbsp; 85358.75 80754.75&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I achieve this? Thanks in advance for the help!&lt;/P&gt;</description>
    <pubDate>Wed, 16 Jun 2021 07:09:11 GMT</pubDate>
    <dc:creator>Shradha1</dc:creator>
    <dc:date>2021-06-16T07:09:11Z</dc:date>
    <item>
      <title>How to take the create a variable where the next observation is conditioned on its preceding one?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-create-a-variable-where-the-next-observation-is/m-p/748260#M234995</link>
      <description>&lt;P&gt;I have a dataset with 2 variables Amount and Balance. And I want to create a third variable balance_cf:&lt;/P&gt;
&lt;P&gt;Amount&amp;nbsp; &amp;nbsp;Balance&lt;BR /&gt;109.98&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&lt;BR /&gt;25.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&lt;BR /&gt;-4500.00&amp;nbsp; &amp;nbsp;85358.75&lt;BR /&gt;941.60&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&lt;BR /&gt;104.00&amp;nbsp; &amp;nbsp; &amp;nbsp;85358.75&lt;BR /&gt;215.00&amp;nbsp; &amp;nbsp; &amp;nbsp;85358.75&lt;BR /&gt;110.00&amp;nbsp; &amp;nbsp; &amp;nbsp;85358.75&lt;/P&gt;
&lt;P&gt;In the new dataset, Balance_cf will be equal to Balance for the first observation. For the second observation onwards, Balance_cf will be equal to its preceeding observation if amount&amp;gt;=0, else if amount&amp;lt;0, then balance_cf= lag(balance_cf)+amount.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The resultant dataset will look like this:&lt;/P&gt;
&lt;P&gt;Amount&amp;nbsp; &amp;nbsp; &amp;nbsp;Balance&amp;nbsp; &amp;nbsp;Balance_cf&lt;BR /&gt;109.98&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&amp;nbsp; 85358.75&lt;BR /&gt;25.00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75&amp;nbsp; 85358.75&lt;BR /&gt;-4500.00&amp;nbsp; &amp;nbsp;85358.75 80858.75&lt;BR /&gt;941.60&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;104.00&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;215.00&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;110.00&amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;47.72&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 85358.75 80858.75&lt;BR /&gt;-104.00&amp;nbsp; &amp;nbsp; 85358.75 80754.75&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I achieve this? Thanks in advance for the help!&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jun 2021 07:09:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-create-a-variable-where-the-next-observation-is/m-p/748260#M234995</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-06-16T07:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to take the create a variable where the next observation is conditioned on its preceding one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-create-a-variable-where-the-next-observation-is/m-p/748261#M234996</link>
      <description>&lt;P&gt;how about this code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input Amount   Balance;
datalines;
109.98      85358.75
25.00        85358.75
-4500.00   85358.75
941.60      85358.75
104.00     85358.75
215.00     85358.75
110.00     85358.75
47.72     85358.75
-104.00     85358.75
;
run;
data want;
  set have;
  retain Balance_cf;
  if _n_=1 then Balance_cf = Balance;
  else if amount&amp;lt;0 then balance_cf = balance_cf + amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Jun 2021 07:22:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-create-a-variable-where-the-next-observation-is/m-p/748261#M234996</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-06-16T07:22:24Z</dc:date>
    </item>
  </channel>
</rss>

