<?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: Selecting first and last value Based on a Character Variable Value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485654#M126200</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; /** GROUP BASED ON VALUES **/ 

data have;
	set have;
	by accno;

	if first.accno and substr(def_type,1,1) eq "L" then grp="F"; /** L denotes Loss**/
	else if first.accno and substr(def_type,1,1) eq "F" then grp="L"; /** F detones Fees **/
	else grp=" ";

run;

/** LOCF CONCEPT ***/ 

data have;
	set have;
	retain locf;/** LAST OBS CARRY FORWARD **/
	by accno;

	if first.accno then locf=grp;
	if locf eq  " " then grp=locf;

	drop grp;
	rename locf=grp;
run;

/** DIVIDED IN TO TWO SEPARATE DATASET FOR FURTHER PROCESS **/ 

data first last;
	set have;
	if grp eq "F" then output first;
	else output last;
run;

/** AS PER NEED FIRST.ACCNO**/ 

data first;
	set first;
	by accno;
	if first.accno;
run;

/** AS PER NEED LAST.ACCNO ***/ 

data last;
	set last;
	by accno;
	if last.accno;
run;

/** SET ALL TOGHER AND FINAL OUTPUT **/ 

data final;
	set first last;
	by accno;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;To use first.&amp;lt;variable&amp;gt; or last.&amp;lt;variable&amp;gt; yo need a &lt;STRONG&gt;BY&lt;/STRONG&gt; statement.&lt;/P&gt;&lt;P&gt;The outcome value of either first.&amp;lt;var&amp;gt; or last.&amp;lt;var&amp;gt; is either 1 or 0, depending on the observation in group,&lt;/P&gt;&lt;P&gt;it does not contain the value of the variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your code you defined &lt;STRONG&gt;retain EAD&lt;/STRONG&gt; statement but never used it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What was your intention by it ? Didn't you got EAD missing in all observations?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the 2nd step there are at most 2 obs per ACCNO, so any obs may be either first.accno or last.accno or both at same time&lt;/P&gt;&lt;P&gt;in case there is only 1 obs per accno.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test (keep = accno date def_type Adj_Bal);
 set exposure;
 by accno date;
 if first.accno or last.accno;
run;

/* the outcome of 1st step is the most 2 obs per accno */

data test2; 
 set test;
  by accno;         /* &amp;lt;&amp;lt;&amp;lt; line added */

 retain EAD;
 Bal_prev = lag(Adj_Bal);
 Type_prev = lag(def_type);

 if first.accno and def_type = 'Loss'
 then final_bal = Adj_Bal;

 if last.accno and Type_prev = 'Loss'
 then final_bal = Bal_prev;
 else if last.accno and Type_prev = 'Fee' 
 then final_bal = Adj_Bal;
 else if first.accno and def_type = 'Fee' 
 then final_bal = .;
run;&lt;/CODE&gt;&lt;/PRE&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;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;There should many methods do it but this seems fair easy and very quick to understand at any level your experience.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope it helps&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 10 Aug 2018 04:21:37 GMT</pubDate>
    <dc:creator>shahparth260</dc:creator>
    <dc:date>2018-08-10T04:21:37Z</dc:date>
    <item>
      <title>Selecting first and last value Based on a Character Variable Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485645#M126195</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to&amp;nbsp;calculate a value (final_bal) using a sorted dataset I have. The dataset exposure has a structure like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Accno&amp;nbsp; &amp;nbsp;Date&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;Def_type&amp;nbsp; &amp;nbsp;Adj_bal&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1001&amp;nbsp; &amp;nbsp;2008/01&amp;nbsp; &amp;nbsp;Loss&amp;nbsp; &amp;nbsp;$3500&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1001&amp;nbsp; &amp;nbsp;2008/06&amp;nbsp; &amp;nbsp;Loss&amp;nbsp; &amp;nbsp;$1100&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1001&amp;nbsp; &amp;nbsp;2008/09&amp;nbsp; &amp;nbsp;Fee&amp;nbsp; &amp;nbsp; $260&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1002&amp;nbsp; &amp;nbsp;2008/03&amp;nbsp;&amp;nbsp; Fee&amp;nbsp; &amp;nbsp; $2200&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1002&amp;nbsp; &amp;nbsp;2008/09&amp;nbsp; &amp;nbsp;Loss&amp;nbsp; &amp;nbsp;$800&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1002&amp;nbsp; &amp;nbsp;2009/01&amp;nbsp; &amp;nbsp; Fee &amp;nbsp; $260&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1003&amp;nbsp; &amp;nbsp;2016/11&amp;nbsp;&amp;nbsp;&amp;nbsp;Fee&amp;nbsp; &amp;nbsp; $15000&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1003&amp;nbsp; &amp;nbsp;2017/01&amp;nbsp; &amp;nbsp;Loss&amp;nbsp; &amp;nbsp;8200&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1003&amp;nbsp; &amp;nbsp;2017/03&amp;nbsp; &amp;nbsp;Loss &amp;nbsp;40&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The dataset is sorted by account-number, then by date. What I'm to do is to come up with a new column that's called final_bal, and it will be the adj_bal value&amp;nbsp;dependent on the &lt;STRONG&gt;first&lt;/STRONG&gt;&amp;nbsp;Def_type is. Here is the logic I'm trying to implement into my code:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If the first Def_type of the account is called 'Loss', then I'll pick the value of that date (ex. $3500 for account 1001) regardless what status the later dates have. However if the first value of the account is called 'Fee', then I'll pick the &lt;STRONG&gt;last&lt;/STRONG&gt; value&amp;nbsp;(ex. $40 for account 1003) regardless what status the later dates have. How do I get this done? I tried following approaches:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data select;
	set exposure;
	retain final_bal;
	if first.accno then do;
		if def_type = 'Loss' then final_bal = first.Adj_Bal;
		else if def_type = 'Fee' then final_bal = last.Adj_Bal;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;That didn't work. Also I tried something like the following but it doesn't work (desperate attempt) the logic also failed.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;data test (keep = accno date def_type Adj_Bal);&lt;BR /&gt; set exposure;&lt;BR /&gt; by accno date;&lt;BR /&gt; if first.accno or last.accno;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data test2; &lt;BR /&gt; set test;&lt;BR /&gt; retain EAD;&lt;BR /&gt; Bal_prev = lag(Adj_Bal);&lt;BR /&gt; Type_prev = lag(def_type);&lt;BR /&gt; if first.accno and def_type = 'Loss'&lt;BR /&gt; then final_bal = Adj_Bal;&lt;BR /&gt; if not first.accno and Type_prev = 'Loss'&lt;BR /&gt; then final_bal = Bal_prev;&lt;BR /&gt; else if not first.accno and Type_prev = 'Fee' &lt;BR /&gt; then final_bal = Adj_Bal;&lt;BR /&gt; else if first.accno and def_type = 'Fee' &lt;BR /&gt; then final_bal = .;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What am I doing wrong? Most importantly, what's the best way to get this done, whether in a data step or proc sql?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I hope I made this explanation clear. Thank you for your contribution to this question I have.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 03:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485645#M126195</guid>
      <dc:creator>Bankshot</dc:creator>
      <dc:date>2018-08-10T03:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first and last value Based on a Character Variable Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485647#M126196</link>
      <description>&lt;P&gt;I suspect that's not what you want, but since you haven't given the output you want, here is what you asked for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  input ACCNO   DATE $  DEF_TYPE  $ ADJ_BAL dollar8.;
cards;
1001   2008/01   Loss   $3500
1001   2008/06   Loss   $1100
1001   2008/09   Fee    $260
1002   2008/03   Fee    $2200
1002   2008/09   Loss   $800
1002   2009/01   Fee   $260
1003   2016/11   Fee    $15000
1003   2017/01   Loss   8200
1003   2017/03   Loss  40
run;
data WANT;
  set HAVE;
  by ACCNO;
  retain FINAL_BAL;
  if first.ACCNO then FINAL_BAL=ifn(DEF_TYPE='Loss', ADJ_BAL,.);
  if last.ACCNO &amp;amp; FINAL_BAL=. then FINAL_BAL=ADJ_BAL;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 240pt;" border="0" width="320" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;ACCNO&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;DATE&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;DEF_TYPE&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;ADJ_BAL&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;FINAL_BAL&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1001&lt;/TD&gt;
&lt;TD&gt;2008/01&lt;/TD&gt;
&lt;TD&gt;Loss&lt;/TD&gt;
&lt;TD align="right"&gt;3500&lt;/TD&gt;
&lt;TD align="right"&gt;3500&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1001&lt;/TD&gt;
&lt;TD&gt;2008/06&lt;/TD&gt;
&lt;TD&gt;Loss&lt;/TD&gt;
&lt;TD align="right"&gt;1100&lt;/TD&gt;
&lt;TD align="right"&gt;3500&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1001&lt;/TD&gt;
&lt;TD&gt;2008/09&lt;/TD&gt;
&lt;TD&gt;Fee&lt;/TD&gt;
&lt;TD align="right"&gt;260&lt;/TD&gt;
&lt;TD align="right"&gt;3500&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1002&lt;/TD&gt;
&lt;TD&gt;2008/03&lt;/TD&gt;
&lt;TD&gt;Fee&lt;/TD&gt;
&lt;TD align="right"&gt;2200&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1002&lt;/TD&gt;
&lt;TD&gt;2008/09&lt;/TD&gt;
&lt;TD&gt;Loss&lt;/TD&gt;
&lt;TD align="right"&gt;800&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1002&lt;/TD&gt;
&lt;TD&gt;2009/01&lt;/TD&gt;
&lt;TD&gt;Fee&lt;/TD&gt;
&lt;TD align="right"&gt;260&lt;/TD&gt;
&lt;TD align="right"&gt;260&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1003&lt;/TD&gt;
&lt;TD&gt;2016/11&lt;/TD&gt;
&lt;TD&gt;Fee&lt;/TD&gt;
&lt;TD align="right"&gt;1500&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1003&lt;/TD&gt;
&lt;TD&gt;2017/01&lt;/TD&gt;
&lt;TD&gt;Loss&lt;/TD&gt;
&lt;TD align="right"&gt;8200&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1003&lt;/TD&gt;
&lt;TD&gt;2017/03&lt;/TD&gt;
&lt;TD&gt;Loss&lt;/TD&gt;
&lt;TD align="right"&gt;40&lt;/TD&gt;
&lt;TD align="right"&gt;40&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&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;</description>
      <pubDate>Fri, 10 Aug 2018 04:00:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485647#M126196</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-10T04:00:57Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first and last value Based on a Character Variable Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485653#M126199</link>
      <description>&lt;P&gt;To use first.&amp;lt;variable&amp;gt; or last.&amp;lt;variable&amp;gt; yo need a &lt;STRONG&gt;BY&lt;/STRONG&gt; statement.&lt;/P&gt;
&lt;P&gt;The outcome value of either first.&amp;lt;var&amp;gt; or last.&amp;lt;var&amp;gt; is either 1 or 0, depending on the observation in group,&lt;/P&gt;
&lt;P&gt;it does not contain the value of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your code you defined &lt;STRONG&gt;retain EAD&lt;/STRONG&gt; statement but never used it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What was your intention by it ? Didn't you got EAD missing in all observations?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the 2nd step there are at most 2 obs per ACCNO, so any obs may be either first.accno or last.accno or both at same time&lt;/P&gt;
&lt;P&gt;in case there is only 1 obs per accno.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test (keep = accno date def_type Adj_Bal);
 set exposure;
 by accno date;
 if first.accno or last.accno;
run;

/* the outcome of 1st step is the most 2 obs per accno */

data test2; 
 set test;
  by accno;         /* &amp;lt;&amp;lt;&amp;lt; line added */

 retain EAD;
 Bal_prev = lag(Adj_Bal);
 Type_prev = lag(def_type);

 if first.accno and def_type = 'Loss'
 then final_bal = Adj_Bal;

 if last.accno and Type_prev = 'Loss'
 then final_bal = Bal_prev;
 else if last.accno and Type_prev = 'Fee' 
 then final_bal = Adj_Bal;
 else if first.accno and def_type = 'Fee' 
 then final_bal = .;
run;&lt;/CODE&gt;&lt;/PRE&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;</description>
      <pubDate>Fri, 10 Aug 2018 04:17:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485653#M126199</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-08-10T04:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first and last value Based on a Character Variable Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485654#M126200</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; /** GROUP BASED ON VALUES **/ 

data have;
	set have;
	by accno;

	if first.accno and substr(def_type,1,1) eq "L" then grp="F"; /** L denotes Loss**/
	else if first.accno and substr(def_type,1,1) eq "F" then grp="L"; /** F detones Fees **/
	else grp=" ";

run;

/** LOCF CONCEPT ***/ 

data have;
	set have;
	retain locf;/** LAST OBS CARRY FORWARD **/
	by accno;

	if first.accno then locf=grp;
	if locf eq  " " then grp=locf;

	drop grp;
	rename locf=grp;
run;

/** DIVIDED IN TO TWO SEPARATE DATASET FOR FURTHER PROCESS **/ 

data first last;
	set have;
	if grp eq "F" then output first;
	else output last;
run;

/** AS PER NEED FIRST.ACCNO**/ 

data first;
	set first;
	by accno;
	if first.accno;
run;

/** AS PER NEED LAST.ACCNO ***/ 

data last;
	set last;
	by accno;
	if last.accno;
run;

/** SET ALL TOGHER AND FINAL OUTPUT **/ 

data final;
	set first last;
	by accno;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;To use first.&amp;lt;variable&amp;gt; or last.&amp;lt;variable&amp;gt; yo need a &lt;STRONG&gt;BY&lt;/STRONG&gt; statement.&lt;/P&gt;&lt;P&gt;The outcome value of either first.&amp;lt;var&amp;gt; or last.&amp;lt;var&amp;gt; is either 1 or 0, depending on the observation in group,&lt;/P&gt;&lt;P&gt;it does not contain the value of the variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your code you defined &lt;STRONG&gt;retain EAD&lt;/STRONG&gt; statement but never used it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What was your intention by it ? Didn't you got EAD missing in all observations?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the 2nd step there are at most 2 obs per ACCNO, so any obs may be either first.accno or last.accno or both at same time&lt;/P&gt;&lt;P&gt;in case there is only 1 obs per accno.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test (keep = accno date def_type Adj_Bal);
 set exposure;
 by accno date;
 if first.accno or last.accno;
run;

/* the outcome of 1st step is the most 2 obs per accno */

data test2; 
 set test;
  by accno;         /* &amp;lt;&amp;lt;&amp;lt; line added */

 retain EAD;
 Bal_prev = lag(Adj_Bal);
 Type_prev = lag(def_type);

 if first.accno and def_type = 'Loss'
 then final_bal = Adj_Bal;

 if last.accno and Type_prev = 'Loss'
 then final_bal = Bal_prev;
 else if last.accno and Type_prev = 'Fee' 
 then final_bal = Adj_Bal;
 else if first.accno and def_type = 'Fee' 
 then final_bal = .;
run;&lt;/CODE&gt;&lt;/PRE&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;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;There should many methods do it but this seems fair easy and very quick to understand at any level your experience.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope it helps&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 04:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485654#M126200</guid>
      <dc:creator>shahparth260</dc:creator>
      <dc:date>2018-08-10T04:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first and last value Based on a Character Variable Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485658#M126204</link>
      <description>&lt;P&gt;I would do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  input ACCNO   DATE $  DEF_TYPE  $ ADJ_BAL dollar8.;
cards;
1001   2008/01   Loss   $3500
1001   2008/06   Loss   $1100
1001   2008/09   Fee    $260
1002   2008/03   Fee    $2200
1002   2008/09   Loss   $800
1002   2009/01   Fee   $260
1003   2016/11   Fee    $15000
1003   2017/01   Loss   8200
1003   2017/03   Loss  40
;

data want;
do until (last.accno);
    set have; by accno;
    if first.accno then first_type = def_type;
    if first_type = "Loss" and first.accno then final_bal = adj_bal;
    if first_type = "Fee" and last.accno then final_bal = adj_bal;
    end;
do until (last.accno);
    set have; by accno;
    output;
    end;
drop first_type;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Aug 2018 04:57:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485658#M126204</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-08-10T04:57:41Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first and last value Based on a Character Variable Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485870#M126288</link>
      <description>&lt;P&gt;Thank you PGStats! This worked like a charm!&lt;/P&gt;
&lt;P&gt;One more follow-up question, just for the sake of my personal understanding, why did you put&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set test; by accno;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;within the do loop rather than outside of the loop? Also, why divide the work into two loops? In another word, how come combining the two doesn't work?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; 
do until (last.accno);
	set have; by accno;
	if first.accno then first_type = def_type;
	if first_type = 'Loss' and first.accno then final_bal= adj_bal;
	if first_type = 'Waiver' and last.accno then final_bal = adj_bal;
	output; 
	end;
drop first_type;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Aug 2018 17:04:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485870#M126288</guid>
      <dc:creator>Bankshot</dc:creator>
      <dc:date>2018-08-10T17:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first and last value Based on a Character Variable Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485892#M126297</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/41542"&gt;@Bankshot&lt;/a&gt;, The &lt;STRONG&gt;do until(last...)&lt;/STRONG&gt; loop is a very useful technique for gathering information about by-groups. If you only need the summary, you&amp;nbsp;can use&amp;nbsp;a single loop. If you want to redistribute the summary information on the original data, you need a second loop, the first one to gather the summary, the second to associate it with the original data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;STRONG&gt;set...; by...;&lt;/STRONG&gt; statements must reside inside the loop to change the value of the &lt;STRONG&gt;last.xxx&lt;/STRONG&gt; variable before it is tested at the end of the loop against the &lt;STRONG&gt;until()&lt;/STRONG&gt; condition.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 17:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485892#M126297</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-08-10T17:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting first and last value Based on a Character Variable Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485907#M126305</link>
      <description>&lt;P&gt;This is very helpful. Thanks again&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 18:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-first-and-last-value-Based-on-a-Character-Variable/m-p/485907#M126305</guid>
      <dc:creator>Bankshot</dc:creator>
      <dc:date>2018-08-10T18:32:03Z</dc:date>
    </item>
  </channel>
</rss>

