<?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: Help in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886792#M350400</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data transactions;
  input yearmonth :yymmn6. acct_no $ status $;
  format yearmonth yymmn6.;
  datalines;
202301 1 inactive
202301 2 active
202302 1 inactive
202302 2 active
202302 3 inactive
202303 3 active
202302 4 active
;

data want;
 if _n_=1 then do;
   if 0 then set transactions(rename=(status=_status));
   declare hash h(dataset:'transactions(rename=(status=_status))');
   h.definekey('yearmonth','acct_no');
   h.definedata('_status');
   h.definedone();
 end;
set transactions;
length StatusNew $ 40;
StatusNew=status;
prev_yearmonth=intnx('month',yearmonth,-1);
if h.find(key:prev_yearmonth,key:acct_no)=0 then do;
 if status='active' and _status='active' then StatusNew='active    ';
 if status='active' and _status='inactive' then StatusNew='re-active';
end;
drop _status prev_yearmonth;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 28 Jul 2023 11:38:59 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-07-28T11:38:59Z</dc:date>
    <item>
      <title>Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886770#M350393</link>
      <description>&lt;P&gt;&lt;BR /&gt;I have data i with columns:status(active or inactive),yearmonth(202301..etc) and acct_no so i need to execute below logic in sas:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;• StatusNew = &lt;STRONG&gt;Active&lt;/STRONG&gt; if current yearmonth status=active, AND was also active in the previous month&lt;BR /&gt;• Status New= &lt;STRONG&gt;Re-activated&lt;/STRONG&gt; if current yearmonth status=active is&amp;nbsp; AND was inactive in the previous yearmonth. &lt;BR /&gt;If account did not exist in the previous month, then just flag them as inactive or active based on their current month’s status.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Issue that i am having with my output from below code is that StatusNew for acct_no =2 in 202302 is &lt;STRONG&gt;Re-activated&lt;/STRONG&gt; but it should be &lt;STRONG&gt;active&lt;/STRONG&gt; based on above rules&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data transactions;
  input yearmonth acct_no $ status $;
  datalines;
202301 1 inactive
202301 2 active
202302 1 inactive
202302 2 active
202302 3 inactive
202303 3 active
202302 4 active
;

proc sort data=transactions;
  by acct_no  yearmonth;
run;

data results;
  set transactions;
  by acct_no;
  length StatusNew $12;

  retain statusnew ' ';


  if first.acct_no then do;
    statusnew = status;
  end;
  else do;
  
  if lag(status)="active" and status = "active" then
    StatusNew = "Active";
  else if lag(status)="inactive" and status = "active" then
    StatusNew = "Re-activated";
  else
    StatusNew = status;
  end;

  if last.acct_no then output;

  drop lag_status;
run;

  &lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Jul 2023 08:43:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886770#M350393</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2023-07-28T08:43:38Z</dc:date>
    </item>
    <item>
      <title>Re: Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886775#M350396</link>
      <description>&lt;P&gt;Help is not a very descriptive subject. Please on subsequent posts use a better subject so that readers get a good idea on what you want help with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the issue is a failure in the code that implements your rule for the first obs in the by group. In your code on the first.acct_no you only set statusnew. I believe you also need to do something with the lag(status) because otherwise the lag(status) is the status of last.acc_no.&lt;/P&gt;
&lt;P&gt;Give the code below a try to verify if that solves your rule logic issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data results;
   set transactions;
   by acct_no;
   length statusnew $12;
   retain statusnew ' ';
   lagstatus = lag(status);
   if first.acct_no then do;
      statusnew = status;
      lagstatus = " ";
      end;
   else do;  
      if lagstatus = "active" and status = "active" then statusnew = "Active";
      else if lagstatus = "inactive" and status = "active" then statusnew = "Re-activated";
           else statusnew = status;
   end;
   if last.acct_no then output;
   drop lagstatus;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 09:49:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886775#M350396</guid>
      <dc:creator>JosvanderVelden</dc:creator>
      <dc:date>2023-07-28T09:49:56Z</dc:date>
    </item>
    <item>
      <title>Re: Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886778#M350397</link>
      <description>&lt;P&gt;Please go back to your ORIGINAL message and provide a meaningful subject line that briefly describes the problem. Help is not meaningful (in every thread someone needs help) and does not describe the problem.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 10:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886778#M350397</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-28T10:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886792#M350400</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data transactions;
  input yearmonth :yymmn6. acct_no $ status $;
  format yearmonth yymmn6.;
  datalines;
202301 1 inactive
202301 2 active
202302 1 inactive
202302 2 active
202302 3 inactive
202303 3 active
202302 4 active
;

data want;
 if _n_=1 then do;
   if 0 then set transactions(rename=(status=_status));
   declare hash h(dataset:'transactions(rename=(status=_status))');
   h.definekey('yearmonth','acct_no');
   h.definedata('_status');
   h.definedone();
 end;
set transactions;
length StatusNew $ 40;
StatusNew=status;
prev_yearmonth=intnx('month',yearmonth,-1);
if h.find(key:prev_yearmonth,key:acct_no)=0 then do;
 if status='active' and _status='active' then StatusNew='active    ';
 if status='active' and _status='inactive' then StatusNew='re-active';
end;
drop _status prev_yearmonth;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Jul 2023 11:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help/m-p/886792#M350400</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-07-28T11:38:59Z</dc:date>
    </item>
  </channel>
</rss>

