<?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: Retaining values by a condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405524#M98666</link>
    <description>&lt;P&gt;First of all: Please, and I mean &lt;FONT size="5"&gt;&lt;STRONG&gt;PLEASE!&lt;/STRONG&gt;&lt;/FONT&gt; use either the {i} or "little running man" icons (6th and 7th above the posting window) for code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your example data code looks like this after pasting into my Enterprise Guide:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

 

input account_id reporting_date sales_date sale_amount admin_cost fines interest ;

 

　

INFORMAT reporting_date sales_date date9.;

format reporting_date sales_date date9.;

 

cards;

 

1000 31JAN2017 . . . . .

 

1000 28FEB2017 . . . . .

 

1000 31MAR2017 06MAR2017 100000 1000 500 200

 

1000 30APR2017 06MAR2017 100000 1500 600 220

 

1000 31MAY2017 06MAR2017 100000 1100 200 300

 

2000 31JAN2017 . . . . .

 

2000 28FEB2017 24FEB2017 80000 50 200 365

 

2000 31MAR2017 24FEB2017 80000 98 25 36

 

2000 30APR2017 24FEB2017 80000 150 65 96

 

;

 

RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and produces an error when submitted.&lt;/P&gt;
&lt;P&gt;Unusable garbage.&lt;/P&gt;
&lt;P&gt;After going over it, this is what it should look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input account_id reporting_date sales_date sale_amount admin_cost fines interest;
informat reporting_date sales_date date9.;
format reporting_date sales_date date9.;
cards;
1000 31JAN2017 . . . . .
1000 28FEB2017 . . . . .
1000 31MAR2017 06MAR2017 100000 1000 500 200
1000 30APR2017 06MAR2017 100000 1500 600 220
1000 31MAY2017 06MAR2017 100000 1100 200 300
2000 31JAN2017 . . . . .
2000 28FEB2017 24FEB2017 80000 50 200 365
2000 31MAR2017 24FEB2017 80000 98 25 36
2000 30APR2017 24FEB2017 80000 150 65 96
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Given your subsequent post, I think this is what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by account_id;
retain k_admin_cost k_fines k_interest;
if first.account_id
then do;
  k_admin_cost = .;
  k_fines = .;
  k_interest = .;
end;
if k_admin_cost ne .
then admin_cost = k_admin_cost;
else if admin_cost ne .
then k_admin_cost = admin_cost;
if k_fines ne .
then fines = k_fines;
else if fines ne .
then k_fines = fines;
if k_interest ne .
then interest = k_interest;
else if interest ne .
then k_interest = interest;
drop k_admin_cost k_fines k_interest;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 19 Oct 2017 13:20:34 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-10-19T13:20:34Z</dc:date>
    <item>
      <title>Retaining values by a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405450#M98645</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have dataset which is updated on a monthly basis and certain values can change on update.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following dataset&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;　&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT color="#0000e6" size="1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT size="1"&gt; have;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT color="#0000e6" size="1"&gt;input&lt;/FONT&gt;&lt;FONT size="1"&gt; account_id reporting_date sales_date sale_amount admin_cost fines interest ;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;　&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT color="#0000e6" size="1"&gt;INFORMAT&lt;/FONT&gt;&lt;FONT size="1"&gt; reporting_date sales_date &lt;/FONT&gt;&lt;FONT color="#008080" size="1"&gt;date9.&lt;/FONT&gt;&lt;FONT size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT color="#0000e6" size="1"&gt;format&lt;/FONT&gt;&lt;FONT size="1"&gt; reporting_date sales_date &lt;/FONT&gt;&lt;FONT color="#008080" size="1"&gt;date9.&lt;/FONT&gt;&lt;FONT size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT color="#0000e6" size="1"&gt;&lt;STRONG&gt;cards&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 31JAN2017 . . . . .&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 28FEB2017 . . . . .&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 31MAR2017 06MAR2017 100000 1000 500 200&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 30APR2017 06MAR2017 100000 1500 600 220&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 31MAY2017 06MAR2017 100000 1100 200 300&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;2000 31JAN2017 . . . . .&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;2000 28FEB2017 24FEB2017 80000 50 200 365&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;2000 31MAR2017 24FEB2017 80000 98 25 36&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;2000 30APR2017 24FEB2017 80000 150 65 96&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000e6" size="1"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT size="3"&gt;What I want to do is to retain the values of variables admin_cost fines and interest once the sale date is first&amp;nbsp;populated.&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT size="3"&gt;Many Thanks&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT size="3"&gt;Adnan&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 09:50:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405450#M98645</guid>
      <dc:creator>Adnan_Razaq</dc:creator>
      <dc:date>2017-10-19T09:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values by a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405456#M98647</link>
      <description>&lt;P&gt;How do you mean retain, the example you give doesn't have any records after the sales date is populated.&amp;nbsp; Do you mean the records before that event?&amp;nbsp; If so then process is the same, you just sort the data in reverse, so this:&lt;/P&gt;
&lt;P align="left"&gt;1000 31JAN2017 . . . . .&lt;/P&gt;
&lt;P align="left"&gt;1000 28FEB2017 . . . . .&lt;/P&gt;
&lt;P align="left"&gt;1000 31MAR2017 06MAR2017 100000 1000 500 200&amp;nbsp;&lt;/P&gt;
&lt;P align="left"&gt;1000 30APR2017 06MAR2017 100000 1500 600 220&lt;/P&gt;
&lt;P align="left"&gt;Becomes&lt;/P&gt;
&lt;P align="left"&gt;1000 30APR2017 06MAR2017 100000 1500 600 220&lt;/P&gt;
&lt;P align="left"&gt;1000 31MAR2017 06MAR2017 100000 1000 500 200&lt;/P&gt;
&lt;P align="left"&gt;1000 28FEB2017 . . . . .&lt;/P&gt;
&lt;P align="left"&gt;1000 31JAN2017 . . . . .&lt;/P&gt;
&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P align="left"&gt;Then if fist group set retains to missing, then if sales_date not null then retains = data, if missing then use retained variables instead.&lt;/P&gt;
&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 10:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405456#M98647</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-19T10:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values by a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405458#M98649</link>
      <description>&lt;P&gt;What I need is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 31JAN2017 . . . . .&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 28FEB2017 . . . . .&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 31MAR2017 06MAR2017 100000 &lt;STRONG&gt;1000 500 200&lt;/STRONG&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 30APR2017 06MAR2017 100000 1000 500 200&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;1000 31MAY2017 06MAR2017 100000 1000 500 200&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;I want to retain to the values in bold for the reporting months after&amp;nbsp;the sales date is populated&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 10:12:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405458#M98649</guid>
      <dc:creator>Adnan_Razaq</dc:creator>
      <dc:date>2017-10-19T10:12:36Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values by a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405459#M98650</link>
      <description>&lt;P&gt;Its already there in the test data?&amp;nbsp; If thats an error on your part then its still the same thing.&amp;nbsp; Reverse sort the data so that the row you want to retain is above the row you want to put the data into.&amp;nbsp; Then retain at the point you want (or you could just retain each rows values), and then replace when missing.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 10:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405459#M98650</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-19T10:15:59Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining values by a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405524#M98666</link>
      <description>&lt;P&gt;First of all: Please, and I mean &lt;FONT size="5"&gt;&lt;STRONG&gt;PLEASE!&lt;/STRONG&gt;&lt;/FONT&gt; use either the {i} or "little running man" icons (6th and 7th above the posting window) for code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your example data code looks like this after pasting into my Enterprise Guide:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

 

input account_id reporting_date sales_date sale_amount admin_cost fines interest ;

 

　

INFORMAT reporting_date sales_date date9.;

format reporting_date sales_date date9.;

 

cards;

 

1000 31JAN2017 . . . . .

 

1000 28FEB2017 . . . . .

 

1000 31MAR2017 06MAR2017 100000 1000 500 200

 

1000 30APR2017 06MAR2017 100000 1500 600 220

 

1000 31MAY2017 06MAR2017 100000 1100 200 300

 

2000 31JAN2017 . . . . .

 

2000 28FEB2017 24FEB2017 80000 50 200 365

 

2000 31MAR2017 24FEB2017 80000 98 25 36

 

2000 30APR2017 24FEB2017 80000 150 65 96

 

;

 

RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and produces an error when submitted.&lt;/P&gt;
&lt;P&gt;Unusable garbage.&lt;/P&gt;
&lt;P&gt;After going over it, this is what it should look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input account_id reporting_date sales_date sale_amount admin_cost fines interest;
informat reporting_date sales_date date9.;
format reporting_date sales_date date9.;
cards;
1000 31JAN2017 . . . . .
1000 28FEB2017 . . . . .
1000 31MAR2017 06MAR2017 100000 1000 500 200
1000 30APR2017 06MAR2017 100000 1500 600 220
1000 31MAY2017 06MAR2017 100000 1100 200 300
2000 31JAN2017 . . . . .
2000 28FEB2017 24FEB2017 80000 50 200 365
2000 31MAR2017 24FEB2017 80000 98 25 36
2000 30APR2017 24FEB2017 80000 150 65 96
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Given your subsequent post, I think this is what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by account_id;
retain k_admin_cost k_fines k_interest;
if first.account_id
then do;
  k_admin_cost = .;
  k_fines = .;
  k_interest = .;
end;
if k_admin_cost ne .
then admin_cost = k_admin_cost;
else if admin_cost ne .
then k_admin_cost = admin_cost;
if k_fines ne .
then fines = k_fines;
else if fines ne .
then k_fines = fines;
if k_interest ne .
then interest = k_interest;
else if interest ne .
then k_interest = interest;
drop k_admin_cost k_fines k_interest;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Oct 2017 13:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-values-by-a-condition/m-p/405524#M98666</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-10-19T13:20:34Z</dc:date>
    </item>
  </channel>
</rss>

