<?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 does retain work in sas? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861763#M340385</link>
    <description>&lt;P&gt;Does the code I provided work for you?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best advice: don't specify that you need to use a specific part of SAS (in this case RETAIN), just ask the question and let the responders pick a method that works.&lt;/P&gt;</description>
    <pubDate>Wed, 01 Mar 2023 15:55:58 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-03-01T15:55:58Z</dc:date>
    <item>
      <title>How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861747#M340381</link>
      <description>&lt;P&gt;How can I generate below Flag variable that it restarts at 0 once the new maturity date is set?&lt;/P&gt;
&lt;TABLE width="512"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="128"&gt;Account ID&lt;/TD&gt;
&lt;TD width="128"&gt;As_of_Date&lt;/TD&gt;
&lt;TD width="128"&gt;Maturity_Date&lt;/TD&gt;
&lt;TD width="128"&gt;Flag&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;4/30/2022&lt;/TD&gt;
&lt;TD&gt;8/1/2022&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;5/31/2022&lt;/TD&gt;
&lt;TD&gt;8/1/2022&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;6/30/2022&lt;/TD&gt;
&lt;TD&gt;8/1/2022&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;7/31/2022&lt;/TD&gt;
&lt;TD&gt;8/1/2022&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;8/31/2022&lt;/TD&gt;
&lt;TD&gt;9/1/2022&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;9/30/2022&lt;/TD&gt;
&lt;TD&gt;9/1/2022&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;10/31/2022&lt;/TD&gt;
&lt;TD&gt;12/1/2022&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;11/30/2022&lt;/TD&gt;
&lt;TD&gt;12/1/2022&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1A&lt;/TD&gt;
&lt;TD&gt;12/31/2022&lt;/TD&gt;
&lt;TD&gt;1/1/2023&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 15:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861747#M340381</guid>
      <dc:creator>tampham92</dc:creator>
      <dc:date>2023-03-01T15:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861759#M340383</link>
      <description>&lt;P&gt;Not sure why you are asking about RETAIN, as this doesn't seem to be needed in the problem you talk about.&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 maturity_date;
    if last.maturity_date then flag=1; 
    else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 15:54:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861759#M340383</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T15:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861760#M340384</link>
      <description>&lt;P&gt;I don't have that Flag variable yet. I want to generate that Flag variable as shown in the dataset.&lt;/P&gt;
&lt;P&gt;My original dataset only 3 variables: Account ID, As of Date and Maturity Date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 15:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861760#M340384</guid>
      <dc:creator>tampham92</dc:creator>
      <dc:date>2023-03-01T15:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861763#M340385</link>
      <description>&lt;P&gt;Does the code I provided work for you?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best advice: don't specify that you need to use a specific part of SAS (in this case RETAIN), just ask the question and let the responders pick a method that works.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 15:55:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861763#M340385</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T15:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861777#M340393</link>
      <description>&lt;P&gt;The code does not work. I tried and it starts dropping some of my observations off the dataset.&lt;/P&gt;
&lt;P&gt;The original dataset contains multiple Account IDs sorted by Account ID and As of Date.&lt;/P&gt;
&lt;P&gt;Thanks for the advice btw.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 16:40:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861777#M340393</guid>
      <dc:creator>tampham92</dc:creator>
      <dc:date>2023-03-01T16:40:18Z</dc:date>
    </item>
    <item>
      <title>Re: How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861778#M340394</link>
      <description>&lt;P&gt;As to the question in your message body:&lt;/P&gt;
&lt;P&gt;Looks like you just want to save the value of the temporary LAST. automatic flag that is created when you use BY statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
  by accountID Maturity_Date;
  flag=last.Maturity_Date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As to the question in your Subject line:&lt;/P&gt;
&lt;P&gt;Normally when you are creating a new variable in a data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x2 = x*x ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS will reset that variable to missing when it starts the next iteration of the data step.&amp;nbsp; What the RETAIN statement does it let you mark the variables that you don't want SAS to reset to missing.&amp;nbsp; So at the start of the next iteration the variable has the value it had at the end of the previous iteration.&amp;nbsp; That is why it is so useful for remembering things or creating accumulators.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
   by id ;
   retain running_total;
    if first.id then running_total=0;
    running_total=sum(running_total,amount);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS even has a special statement, the sum statement, to make doing accumulators even easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
   by id ;
   if first.id then running_total=0;
   running_total + amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 16:42:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861778#M340394</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-01T16:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861780#M340395</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/176468"&gt;@tampham92&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The code does not work. I tried and it starts dropping some of my observations off the dataset.&lt;/P&gt;
&lt;P&gt;The original dataset contains multiple Account IDs sorted by Account ID and As of Date.&lt;/P&gt;
&lt;P&gt;Thanks for the advice btw.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Nothing in my code will cause observations to be deleted. Can you show us the exact code you used, and the output data set? (I see &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; has provided &lt;EM&gt;almost&lt;/EM&gt; the same code)&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 16:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861780#M340395</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T16:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861798#M340401</link>
      <description>&lt;P&gt;I think when I just sort the data by "maturity date" without accountID, somehow it drops observations.&lt;/P&gt;
&lt;P&gt;It works now once I add accountID back to "BY" statement. Thanks a lot for your and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s help.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 18:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861798#M340401</guid>
      <dc:creator>tampham92</dc:creator>
      <dc:date>2023-03-01T18:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: How does retain work in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861799#M340402</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/176468"&gt;@tampham92&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tip for next time: if you want code that works over multiple IDs, give example data that has multiple IDs, and state you want code that works on multiple IDs in your original message.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 18:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-retain-work-in-sas/m-p/861799#M340402</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T18:06:47Z</dc:date>
    </item>
  </channel>
</rss>

