<?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: SAS query in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445445#M283115</link>
    <description>&lt;P&gt;thanks Kurt. it worked perfectly. One last question. how do I keep the default date&amp;nbsp; in all observations after default?&lt;/P&gt;</description>
    <pubDate>Wed, 14 Mar 2018 12:08:51 GMT</pubDate>
    <dc:creator>NTR</dc:creator>
    <dc:date>2018-03-14T12:08:51Z</dc:date>
    <item>
      <title>SAS query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445395#M283108</link>
      <description>&lt;P&gt;good morning guys, I would really appreciate some assistance. I have a record of monthly accounts from 2012 to 2018 tracking their performance. what I need to do is to identify when the account first defaulted which I managed to do. Now I need help with counting the number of months since default. how do I go about doing that. on SAS...in the below example months since default is 10&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example of data&lt;/P&gt;&lt;P&gt;Date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Act number&amp;nbsp;&amp;nbsp;Default Flag&lt;/P&gt;&lt;P&gt;201201 5203370&amp;nbsp;&amp;nbsp;&amp;nbsp;N&lt;BR /&gt;201202 5203370 &amp;nbsp;N&lt;BR /&gt;201203 5203370 &amp;nbsp;N&lt;BR /&gt;201204 5203370 &amp;nbsp;N&lt;BR /&gt;201205 5203370 &amp;nbsp;N&lt;BR /&gt;201206 5203370 Y&lt;BR /&gt;201207 5203370 &amp;nbsp;N&lt;BR /&gt;201208 5203370 &amp;nbsp;N&lt;BR /&gt;201209 5203370 &amp;nbsp;N&lt;BR /&gt;201210 5203370&amp;nbsp;N&lt;BR /&gt;201211 5203370 &amp;nbsp;N&lt;BR /&gt;201212 5203370&amp;nbsp;N&lt;BR /&gt;201301 5203370&amp;nbsp;N&lt;BR /&gt;201302 5203370&amp;nbsp;N&lt;BR /&gt;201303 5203370&amp;nbsp;N&lt;BR /&gt;201304 5203370&amp;nbsp;N&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 07:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445395#M283108</guid>
      <dc:creator>NTR</dc:creator>
      <dc:date>2018-03-14T07:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: SAS query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445400#M283109</link>
      <description>&lt;P&gt;You can calculate the duration of an interval with the INTCK() function. Assuming that your variable Date is truly a date (numeric), you would use it like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Months = INTCK('MONTH',Date,today())&lt;/PRE&gt;
&lt;P&gt;If Date is a character (as I suspect; its header is left aligned in your preview) you will first have to convert:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Date_num= input(Date, YYMMN6.);&lt;/PRE&gt;
&lt;P&gt;These two lines can be compounded as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Months = INTCK('MONTH',input(Date, YYMMN6.), today());&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 07:51:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445400#M283109</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2018-03-14T07:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: SAS query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445404#M283110</link>
      <description>&lt;P&gt;You do that by using a retained variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date $ act_number $ default_flag $;
cards;
201201 5203370 N
201202 5203370 N
201203 5203370 N
201204 5203370 N
201205 5203370 N
201206 5203370 Y
201207 5203370 N
201208 5203370 N
201209 5203370 N
201210 5203370 N
201211 5203370 N
201212 5203370 N
201301 5203370 N
201302 5203370 N
201303 5203370 N
201304 5203370 N
;
run;

data want;
set have;
retain count .;
if count ne . then count + 1;
if default_flag = 'Y' then count = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how I presented example data in a data step. Anybody can copy/paste the code, run it, and will get the same dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need to run this for different accounts, you need to use by-group processing and reset your counter to missing at first.account.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 07:55:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445404#M283110</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-14T07:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: SAS query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445407#M283111</link>
      <description>Ah yes looks like I completely misread the question.</description>
      <pubDate>Wed, 14 Mar 2018 07:58:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445407#M283111</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2018-03-14T07:58:29Z</dc:date>
    </item>
    <item>
      <title>Re: SAS query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445420#M283112</link>
      <description>&lt;P&gt;hi Kurt, thanks for this....I'm not sure if I get you. you mean I need to&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token procnames"&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;data&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;set&lt;/FONT&gt;&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;by act_number;&lt;/P&gt;&lt;P&gt;if first.act_number then do;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;retain&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;count&lt;/FONT&gt;&lt;/SPAN&gt; &lt;FONT color="#999999"&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;if&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;count&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;ne&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;.&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;then&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;count&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;+&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;&lt;STRONG&gt;&lt;FONT color="#008080"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;if&lt;/FONT&gt;&lt;/SPAN&gt; default_flag &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;=&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;&lt;FONT color="#800000"&gt;'Y'&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;then&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;count&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;=&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;&lt;STRONG&gt;&lt;FONT color="#008080"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;&lt;SPAN class="token procnames"&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;looking forward to hear from you.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 09:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445420#M283112</guid>
      <dc:creator>NTR</dc:creator>
      <dc:date>2018-03-14T09:16:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445424#M283113</link>
      <description>&lt;P&gt;Counting months will work as long as you can be 100% sure that there won't be any missing records (a month missing) in your data. It shouldn't with financial data but just in case below a coding variant which will also work if there are missing months.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date :yymmn6. act_number $ default_flag $;
  format date date9.;
  cards;
201201 5203370 N
201202 5203370 N
201203 5203370 N
201204 5203370 N
201205 5203370 N
201206 5203370 Y
201207 5203370 N
201208 5203370 N
201209 5203370 N
201210 5203370 N
201211 5203370 N
201212 5203370 N
201301 5203370 N
201302 5203370 N
201303 5203370 N
201304 5203370 N
;
run;

data want;
  if _n_=1 then
    do;
      dcl hash defFlg(dataset:'have(keep=act_number date default_flag rename=(date=defDate) where=(default_flag="Y"))');
      defFlg.defineKey('act_number');
      defFlg.defineData('defDate');
      defFlg.defineDone();
    end;
  set have;
  
  if defFLG.find()=0 then
    do;
      month=intck('month',defDate,date);
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;N.B: Above code doesn't require dataset&amp;nbsp;Have to be sorted. It does require though&amp;nbsp;that each account doesn't have more than one default month and that variable date contains a SAS date value so we can use SAS calendar functions like intck().&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 09:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445424#M283113</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-03-14T09:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: SAS query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445432#M283114</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/199014"&gt;@NTR&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;hi Kurt, thanks for this....I'm not sure if I get you. you mean I need to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token procnames"&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;data&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;set&lt;/FONT&gt;&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;by act_number;&lt;/P&gt;
&lt;P&gt;if first.act_number then do;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;retain&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;count&lt;/FONT&gt;&lt;/SPAN&gt; &lt;FONT color="#999999"&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;if&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;count&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;ne&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;.&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;then&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;count&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;+&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;&lt;STRONG&gt;&lt;FONT color="#008080"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;if&lt;/FONT&gt;&lt;/SPAN&gt; default_flag &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;=&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;&lt;FONT color="#800000"&gt;'Y'&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;&lt;FONT color="#0000ff"&gt;then&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;&lt;FONT color="#0000ff"&gt;count&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&lt;FONT color="#a67f59"&gt;=&lt;/FONT&gt;&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;&lt;STRONG&gt;&lt;FONT color="#008080"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;end;&lt;BR /&gt;&lt;SPAN class="token procnames"&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;&lt;FONT color="#999999"&gt;looking forward to hear from you.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When you have to work with groups, don't use the retain statement for initialization.&lt;/P&gt;
&lt;P&gt;Do it with the first. automatic variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by act_number;
retain count;
if first.act_number then count = .;
if count ne . then count + 1;
if default_flag = 'Y' then count = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Mar 2018 10:08:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445432#M283114</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-14T10:08:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445445#M283115</link>
      <description>&lt;P&gt;thanks Kurt. it worked perfectly. One last question. how do I keep the default date&amp;nbsp; in all observations after default?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 12:08:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/445445#M283115</guid>
      <dc:creator>NTR</dc:creator>
      <dc:date>2018-03-14T12:08:51Z</dc:date>
    </item>
  </channel>
</rss>

