<?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: Conditionally calculate variables by ID conditional on how many times that ID is present in data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797704#M81607</link>
    <description>&lt;P&gt;Your calendar_time 117 is more than 6 months from 110. Your illustration for 117 violates 6-month interval.&lt;/P&gt;</description>
    <pubDate>Tue, 22 Feb 2022 04:41:10 GMT</pubDate>
    <dc:creator>KachiM</dc:creator>
    <dc:date>2022-02-22T04:41:10Z</dc:date>
    <item>
      <title>Conditionally calculate variables by ID conditional on how many times that ID is present in data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797700#M81606</link>
      <description>&lt;P&gt;I am not sure how exactly to explain this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset of loans with ids, monthly indicators of time, and monthly indicators of how much is owed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to count how many times a given loan occurs in the dataset for the previous 6 months. I also want to calculate the difference between the current observation's amount owed and the mean amount owed over those six months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what the data looks like. I want to calculate pmts and omb_n_change.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;loan_id&lt;/TD&gt;&lt;TD&gt;calendar_time&lt;/TD&gt;&lt;TD&gt;omb_n&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;pmts&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;omb_n_change&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;96&lt;/TD&gt;&lt;TD&gt;165836&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;97&lt;/TD&gt;&lt;TD&gt;165646&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;165646 - 165836&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;98&lt;/TD&gt;&lt;TD&gt;165646&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;165646 - average of (165646,165836)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;99&lt;/TD&gt;&lt;TD&gt;165646&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;165646 - average of (165646,165646,165836)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;110&lt;/TD&gt;&lt;TD&gt;161957&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;111&lt;/TD&gt;&lt;TD&gt;161748&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;161748 - 161957&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;112&lt;/TD&gt;&lt;TD&gt;161537&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;161537 - average of (161748,161957)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;113&lt;/TD&gt;&lt;TD&gt;161537&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;161537 - average of (161537,161748,161957)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;114&lt;/TD&gt;&lt;TD&gt;161537&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;161537 - average of (161537,161537,161748,161957)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;115&lt;/TD&gt;&lt;TD&gt;161326&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;161326 - average of (161537,161537,161537,161748,161957)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;116&lt;/TD&gt;&lt;TD&gt;161326&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;161326 - average of (161326,161537,161537,161537,161748,161957)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;117&lt;/TD&gt;&lt;TD&gt;161113&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;161113 - average of (161326,161326,161537,161537,161537,161748)&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Loan_id is an id for each loan, calendar_time indicates what month that payment was made, and omb_n indicates how much was still owed that month. So basically I want to count how many times loan_id "1" occurred in my data over calendar_time - 6 as well as the mean value of omb_n over that time period (then take the difference between omb_n and that mean).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried using a hash iterator but am a rookie with those so I got lost before I could really get anything coherent.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 02:51:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797700#M81606</guid>
      <dc:creator>uwabe3</dc:creator>
      <dc:date>2022-02-22T02:51:42Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally calculate variables by ID conditional on how many times that ID is present in data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797704#M81607</link>
      <description>&lt;P&gt;Your calendar_time 117 is more than 6 months from 110. Your illustration for 117 violates 6-month interval.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 04:41:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797704#M81607</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2022-02-22T04:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally calculate variables by ID conditional on how many times that ID is present in data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797706#M81608</link>
      <description>Alright well I guess that would be a different number then.</description>
      <pubDate>Tue, 22 Feb 2022 04:43:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797706#M81608</guid>
      <dc:creator>uwabe3</dc:creator>
      <dc:date>2022-02-22T04:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally calculate variables by ID conditional on how many times that ID is present in data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797719#M81609</link>
      <description>&lt;P&gt;Title, description and table don't match. I don't see "calculate ... on how many times that ID is present in data". Please explain. And when you do that, post data in usable form and show exactly, what you expect as result.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 07:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797719#M81609</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-02-22T07:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally calculate variables by ID conditional on how many times that ID is present in data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797783#M81610</link>
      <description>&lt;P&gt;Assuming your table is not big. Otherwise, you need Hash Table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input loan_id calendar_time omb_n;
cards;
1 96 165836
1 97 165646
1 98 165646
1 99 165646
1 110 161957
1 111 161748
1 112 161537
1 113 161537
1 114 161537
1 115 161326
1 116 161326
1 117 161113
;

proc sql;
create table want as
select *,
(select count(*) from have where loan_id=a.loan_id and calendar_time between a.calendar_time-1 and a.calendar_time-6) as pmts,
(select mean(omb_n) from have where loan_id=a.loan_id and calendar_time between a.calendar_time-1 and a.calendar_time-6) as average,
 omb_n-calculated average as  omb_n_change
 from have as a
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Feb 2022 12:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797783#M81610</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-22T12:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally calculate variables by ID conditional on how many times that ID is present in data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797832#M81611</link>
      <description>&lt;P&gt;This is perfect, thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 15:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Conditionally-calculate-variables-by-ID-conditional-on-how-many/m-p/797832#M81611</guid>
      <dc:creator>uwabe3</dc:creator>
      <dc:date>2022-02-22T15:27:45Z</dc:date>
    </item>
  </channel>
</rss>

