<?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: Add a new row based on condition from previous rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651181#M195344</link>
    <description>&lt;P&gt;If something more elegant occurs, I will post later. I am sure you will hear from some more people with nifty solutions.&amp;nbsp;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;In the meantime try this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want ;
   do _n_ = 1 by 1 until (last.id) ;
      set demo end = eof1 ;
      by id ;
      array r(2) ;
      array rsn_cd_ (2) $ ;
      array rsn_amt_ (2) ;
      array chkc (2) $ ("XA" "XZ") ;
      r(_n_) = (rsn_cd_(_n_) = chkc(_n_)) * rsn_amt_(_n_) ;
      maxdt = max(maxdt,svc_dt) ;
   end ;
   
   do _n_ = 1 by 1 until (last.id) ;
      set demo end = eof2 ;
      by id ;
      final_amt = 0 ;
      output ;
      if r(_n_) then 
         do ;
            line = 1000 + _n_ ;
            svc_dt = maxdt ;
            final_amt = rsn_amt_(_n_) ;
            call missing (of rsn_cd_(*)) ;
            do i = 1 to 2 ; rsn_amt_(i) = min(0,rsn_amt_(i)) ; end ;
            output ;
         end ;
   end ;
   drop maxdt chkc1 chkc2 r1 r2 i ;
run ;

proc sort ; 
  by id line ;
run ;
&lt;/PRE&gt;</description>
    <pubDate>Wed, 27 May 2020 19:07:51 GMT</pubDate>
    <dc:creator>biopharma</dc:creator>
    <dc:date>2020-05-27T19:07:51Z</dc:date>
    <item>
      <title>Add a new row based on condition from previous rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651143#M195327</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following table&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shanmukh2_0-1590598719308.png" style="width: 1009px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/40041i2DB0A2A15619FF35/image-dimensions/1009x238?v=v2" width="1009" height="238" role="button" title="shanmukh2_0-1590598719308.png" alt="shanmukh2_0-1590598719308.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For a given id, the RSN codes and RSN amounts remain constant across all the lines (line). I need to create additional lines based on following conditions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For an ID, If RSN_CD_1 = XA and RSN_AMT_1 is not NULL , insert a row with line = 1001, SVC_DT as max(svc_dt) of that ID and create a new column FINAL_AMT and populate it with RSN_AMT_1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For an ID, If RSN_CD_2 = XZ and RSN_AMT_2 is not NULL, insert a row with line = 1002, SVC_DT as max(svc_dt) of that ID and create a new column FINAL_AMT and populate it with RSN_AMT_2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For an ID, If RSN_CD_1 and RSN_CD_2 are populated with different values (XY and XZ) including their amounts, then insert 2 rows with line = 1001 and 1002, SVC_DT as max(svc_dt) of that ID and create a new column FINAL_AMT and populate it with individual RSN_AMTs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For an ID, If RSN_CD_1 and RSN_CD_2 are populated with same values (XZ for eg) including their amounts, then insert 1 row with line = 1002, SVC_DT as max(svc_dt) of that ID and create a new column FINAL_AMT and populate it with sum of&amp;nbsp; RSN_AMT_1 and RSN_AMT_2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the new lines created, the rest of the amt columns/ SVC_CD columns are set to NULL or 0. Below is the final output I am looking for&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shanmukh2_1-1590598810581.png" style="width: 1107px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/40042i2535029B6A3A394A/image-dimensions/1107x367?v=v2" width="1107" height="367" role="button" title="shanmukh2_1-1590598810581.png" alt="shanmukh2_1-1590598810581.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can some one please help me out how to do it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2020 17:01:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651143#M195327</guid>
      <dc:creator>shanmukh2</dc:creator>
      <dc:date>2020-05-27T17:01:13Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row based on condition from previous rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651153#M195331</link>
      <description>Can you post code to create the dataset that you HAVE? For example see how this poster has created. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Covert-date-from-character-or-numeric-before-append/m-p/648986" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Covert-date-from-character-or-numeric-before-append/m-p/648986&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;General instructions to create code out of a dataset are here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;</description>
      <pubDate>Wed, 27 May 2020 17:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651153#M195331</guid>
      <dc:creator>biopharma</dc:creator>
      <dc:date>2020-05-27T17:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row based on condition from previous rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651165#M195335</link>
      <description>&lt;P&gt;Sure, here you go&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data demo;&lt;BR /&gt;infile datalines missover dlm='';&lt;BR /&gt;input @1 id $1-4 @6 line @8 rsn_cd_1 $8-9 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30987"&gt;@11&lt;/a&gt; rsn_cd_2 $11-12 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/189370"&gt;@14&lt;/a&gt; svc_cd $14-16 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/71462"&gt;@18&lt;/a&gt; svc_dt date9. amt_1 amt_2 amt_3 RSN_AMT_1 RSN_AMT_2;&lt;BR /&gt;format svc_dt mmddyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;D100 1 XA ABC 11JAN2020 10 20 30 40 0&lt;BR /&gt;D100 2 XA ABC 15JAN2020 0 0 0 40 0&lt;BR /&gt;D200 1 XZ DEF 13MAY2020 0 0 0 0 50&lt;BR /&gt;D200 2 XZ GHI 25MAY2020 0 0 0 0 50&lt;BR /&gt;D500 1 XA XZ PQR 20JUN2020 0 0 0 10 30&lt;BR /&gt;D500 2 XA XZ GHI 28JUN2020 0 0 0 10 30&lt;BR /&gt;D800 1 XZ XZ ABC 27MAY2020 0 0 0 20 40&lt;BR /&gt;D800 2 XZ XZ PQR 20JUN2020 0 0 0 20 40&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2020 18:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651165#M195335</guid>
      <dc:creator>shanmukh2</dc:creator>
      <dc:date>2020-05-27T18:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row based on condition from previous rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651175#M195340</link>
      <description>&lt;P&gt;Sorry, this is much better formatted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data demo;&lt;BR /&gt;infile datalines missover dlm='';&lt;BR /&gt;input @1 id $1-4 @6 line @8 rsn_cd_1 $8-9 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30987"&gt;@11&lt;/a&gt; rsn_cd_2 $11-12 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/189370"&gt;@14&lt;/a&gt; svc_cd $14-16 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/71462"&gt;@18&lt;/a&gt; svc_dt date9. amt_1 amt_2 amt_3 RSN_AMT_1 RSN_AMT_2;&lt;BR /&gt;format svc_dt mmddyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;D100 1 XA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ABC 11JAN2020 10 20 30 40 0&lt;BR /&gt;D100 2 XA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ABC 15JAN2020 0 0 0 40 0&lt;BR /&gt;D200 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;XZ DEF 13MAY2020 0 0 0 0 50&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;D200 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;XZ GHI 25MAY2020 0 0 0 0 50&lt;BR /&gt;D500 1 XA XZ PQR 20JUN2020 0 0 0 10 30&lt;BR /&gt;D500 2 XA XZ GHI 28JUN2020 0 0 0 10 30&lt;BR /&gt;D800 1 XZ XZ ABC 27MAY2020 0 0 0 20 40&lt;BR /&gt;D800 2 XZ XZ PQR 20JUN2020 0 0 0 20 40&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2020 18:47:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651175#M195340</guid>
      <dc:creator>shanmukh2</dc:creator>
      <dc:date>2020-05-27T18:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row based on condition from previous rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651180#M195343</link>
      <description>I have formatted better, in the below message</description>
      <pubDate>Wed, 27 May 2020 19:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651180#M195343</guid>
      <dc:creator>shanmukh2</dc:creator>
      <dc:date>2020-05-27T19:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row based on condition from previous rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651181#M195344</link>
      <description>&lt;P&gt;If something more elegant occurs, I will post later. I am sure you will hear from some more people with nifty solutions.&amp;nbsp;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;In the meantime try this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want ;
   do _n_ = 1 by 1 until (last.id) ;
      set demo end = eof1 ;
      by id ;
      array r(2) ;
      array rsn_cd_ (2) $ ;
      array rsn_amt_ (2) ;
      array chkc (2) $ ("XA" "XZ") ;
      r(_n_) = (rsn_cd_(_n_) = chkc(_n_)) * rsn_amt_(_n_) ;
      maxdt = max(maxdt,svc_dt) ;
   end ;
   
   do _n_ = 1 by 1 until (last.id) ;
      set demo end = eof2 ;
      by id ;
      final_amt = 0 ;
      output ;
      if r(_n_) then 
         do ;
            line = 1000 + _n_ ;
            svc_dt = maxdt ;
            final_amt = rsn_amt_(_n_) ;
            call missing (of rsn_cd_(*)) ;
            do i = 1 to 2 ; rsn_amt_(i) = min(0,rsn_amt_(i)) ; end ;
            output ;
         end ;
   end ;
   drop maxdt chkc1 chkc2 r1 r2 i ;
run ;

proc sort ; 
  by id line ;
run ;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 May 2020 19:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651181#M195344</guid>
      <dc:creator>biopharma</dc:creator>
      <dc:date>2020-05-27T19:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row based on condition from previous rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651185#M195347</link>
      <description>&lt;P&gt;Earlier code was copied and pasted code from one version too early:&lt;/P&gt;
&lt;PRE&gt;data want ;
   do _n_ = 1 by 1 until (last.id) ;
      set demo end = eof1 ;
      by id ;
      array r(2) ;
      array rsn_cd_ (2) $ ;
      array rsn_amt_ (2) ;
      array chkc (2) $ ("XA" "XZ") ;
      r(_n_) = (rsn_cd_(_n_) = chkc(_n_)) * rsn_amt_(_n_) ;
      maxdt = max(maxdt,svc_dt) ;
   end ;
   
   do _n_ = 1 by 1 until (last.id) ;
      set demo end = eof2 ;
      by id ;
      final_amt = 0 ;
      output ;
      if r(_n_) then 
         do ;
            line = 1000 + _n_ ;
            svc_dt = maxdt ;
            final_amt = rsn_amt_(_n_) ;
            if rsn_cd_1 = rsn_cd_2 then final_amt = sum(of rsn_amt_(*)) ;
            call missing (of rsn_cd_(*)) ;
            do i = 1 to 2 ; rsn_amt_(i) = min(0,rsn_amt_(i)) ; end ;
            output ;
         end ;
   end ;
   drop maxdt chkc1 chkc2 r1 r2 i ;
run ;

proc sort ; 
  by id line ;
run ;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2020 19:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651185#M195347</guid>
      <dc:creator>biopharma</dc:creator>
      <dc:date>2020-05-27T19:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row based on condition from previous rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651224#M195363</link>
      <description>&lt;P&gt;Not recommending this approach, however methinks this is no more than a solution that encompasses a one pass of by group for the Max(svc date) and then followed by boring IF THEN verbose, which the following is little incomplete for the non missing RSN_CD series as I am very lazy. Feel free to play around and complete if you would like to try.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data demo;
infile datalines missover dlm='';
input @1 id $ line  rsn_cd_1 $  rsn_cd_2 $ svc_cd $ svc_dt date9. amt_1 amt_2 amt_3 RSN_AMT_1 RSN_AMT_2;
format svc_dt mmddyy10.;
datalines;
D100 1 XA  .  ABC 11JAN2020 10 20 30 40 0
D100 2 XA  .  ABC 15JAN2020 0 0 0 40 0
D200 1  .  XZ DEF 13MAY2020 0 0 0 0 50
D200 2  .  XZ GHI 25MAY2020 0 0 0 0 50
D500 1 XA XZ PQR 20JUN2020 0 0 0 10 30
D500 2 XA XZ GHI 28JUN2020 0 0 0 10 30
D800 1 XZ XZ ABC 27MAY2020 0 0 0 20 40
D800 2 XZ XZ PQR 20JUN2020 0 0 0 20 40
;
run;

data want;
 do until(last.id);
  set demo;
  by id;
  _m=svc_dt max _m;
  Final_amt=0;
  output;
 end;
 retain _iorc_ 1000;
 array t RSN_AMT_:;
 if cmiss(of rsn_cd_:) then do;
  _n_=whichc(coalescec(of rsn_cd_:),of rsn_cd_:);
  line=_iorc_+_n_;
  Final_amt=t(_n_);
  svc_dt=_m;
  call missing(of rsn_cd_:,of t(*),svc_cd);
  call pokelong(repeat(put(0,rb8.),1),addrlong(t(1)),16);
  output;
 end;
/*One can continue with else do; for non missing rsn_cd: series*/
/*blah blah blah and so forth*/
 drop _:;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2020 21:22:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-based-on-condition-from-previous-rows/m-p/651224#M195363</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-27T21:22:20Z</dc:date>
    </item>
  </channel>
</rss>

