<?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: Using a Loop in a data step to populate values in other fields in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598761#M172704</link>
    <description>&lt;P&gt;Sure you can! You'll want to add your new OpenDt field to the RETAIN statement. I suspect you'll also want to clear out that value for each new ClaimNumber. If you don't clear out OpenDt like you do for CloseDt and reopenDT, it will just persist&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You'll want to consider how to handle 2 'Open's in a row like you have in rows 38 and 39. If you only want the first one, then you'll need to use some first-DOT processing.&lt;/P&gt;</description>
    <pubDate>Wed, 23 Oct 2019 15:41:11 GMT</pubDate>
    <dc:creator>noling</dc:creator>
    <dc:date>2019-10-23T15:41:11Z</dc:date>
    <item>
      <title>Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597713#M172240</link>
      <description>&lt;P&gt;I am trying to use a data step to populate two fields, based on the values of two other fields and I am running into issues.&amp;nbsp; I have attached a sample of what I want my output to look like.&amp;nbsp; I have highlighted the values that I want to populate and what I want those values to equal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically, I have a data set that is sorted by a claim number and a sequence number, so that the data is in chronological (transaction) order.&amp;nbsp; For each claim number, when the status is 'Open', I want the Closed Date and the Reopen Date fields to be null.&amp;nbsp; When the status changes from Open to Closed, I want to take the Book Date, which is a transaction date, and put it into the Closed Date field.&amp;nbsp; Then, for each record after that where the status is = 'Closed', I want to use that first Book Date in the Closed Date field, until the status changes again.&amp;nbsp; When the status changes to Reopen, I want to make the Close Date = null and the reopen date to equal the Book Date.&amp;nbsp; I will then use that first Book Date value in each following record until the status changes again.&amp;nbsp; Each time the status changes, I need to basically reset the date I am using to the value in the current record of the first change.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS EG&amp;nbsp;7.15 HF7 (7.100.5.6177) (64-bit)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I started with this Do While Loop, but the last END; keeps flagging an error "No Matching DO/SELECT statement".&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;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.STATUS_LIST_1;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET WORK.STATUS_LIST;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clmnum = ClaimNumber;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DO WHILE (clmnum = ClaimNumber);

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If ClaimStatusCd NE 'Closed' THEN
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUTPUT;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ELSE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keepClosedDt = BookDt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keepStatusCd = ClaimStatusCd;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ClosedDt = keepClosedDt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUTPUT;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DO WHILE (keepStatusCd = ClaimStatusCd);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ClosedDt = keepClosedDt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUTPUT;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 13:43:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597713#M172240</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-18T13:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597717#M172241</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to execute several instructions after an if or an else you have to enclose&lt;/P&gt;
&lt;P&gt;them in do; ... end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here your else should be "else do;"&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 13:50:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597717#M172241</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-10-18T13:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597729#M172246</link>
      <description>&lt;P&gt;This is going to give you an infinite loop since you're setting clmnum = ClaimNumber, using those same values in the WHILE loop, and not changing their values in the loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm assuming that you probably want to look at the ClaimNumber value of a different record. You could use the LAG function, a RETAIN statement, or first-dot last-dot by group processing.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 14:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597729#M172246</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-10-18T14:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597773#M172270</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.STATUS_LIST_1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET WORK.STATUS_LIST;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clmnum = ClaimNumber;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DO WHILE (clmnum = ClaimNumber);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If ClaimStatusCd NE 'Closed' THEN
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUTPUT;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ELSE&lt;STRONG&gt;&lt;FONT color="#ff0000" size="4"&gt; DO&lt;/FONT&gt;;&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keepClosedDt = BookDt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keepStatusCd = ClaimStatusCd;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ClosedDt = keepClosedDt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUTPUT;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DO WHILE (keepStatusCd = ClaimStatusCd);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ClosedDt = keepClosedDt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUTPUT;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&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;Cause of error missing DO.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A do while needs the value(s) used in the comparison to change or they won't end. So I suspect that you are going to have an infinite loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BEST, provide a small example data set of what you have, only include variables needed, and an example of how the result should look.&lt;/P&gt;
&lt;P&gt;"Status Change" means that you need to compare a current record value to a previous record value, which would imply use of either the LAG function, to look back,&amp;nbsp;or the RETAIN, to hold a previous value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 16:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597773#M172270</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-18T16:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597779#M172272</link>
      <description>&lt;P&gt;Thank you everyone.&amp;nbsp; The DO did correct the error I was getting but the logic I created, did in fact, create an infinite loop.&amp;nbsp; I reviewed the LAG and retain logic online and wrote the below.&amp;nbsp; This is close to what I need, but I need to finish it out.&amp;nbsp; The below code works for the first record of each grouping of claimnumber and status code.&amp;nbsp; I would like to take CloseDt value and pass it on to each following record until the status changes again.&amp;nbsp; Once a claim is in Open status, the CloseDt would be null.&amp;nbsp; I am trying to figure out how to apply the retain and or lag to this.&amp;nbsp; I am thinking that if it is the first record combo, then use the below code, if it is the second or greater combo, retain the first result until the next combo.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.STATUS_LIST_1; 


	SET WORK.STATUS_LIST;

	format CloseDT DATE9.;

	BY ClaimNumber;

	IF ClaimStatusCd = 'Closed' THEN
		CloseDT = BookDt;
		OUTPUT;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 17:28:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597779#M172272</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-18T17:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597790#M172281</link>
      <description>&lt;P&gt;Some points:&lt;/P&gt;&lt;P&gt;1. If you want several statements to happen as a result of an IF condition, you need to put them in a DO/END block:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF claimNumber_lag = claimNumber and ClaimStatusCd = 'Closed' then do;
	CloseDT = BookDt;
	output;
end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. You don't need to use an OUTPUT statement without any conditions - SAS will output automatically at the bottom of each datastep if you don't use any OUTPUT statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. Using a BY statement without referencing the variables in the statement doesn't do anything. In the example below, first.claimnumber = 1 when the value of claimNumber is different from the previous value of claimNumber (otherwise = 0)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4. If you want to use a LAG function, create it outside of conditional logic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;---&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think this may be a cleaner solution for you. The RETAIN statements holds values across records, so in this example you clear them out for each new claim, then adjust them according to your desired specifications. Hopefully this is close:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.STATUS_LIST_1; 
	set WORK.sample;
	format  closeDt reopenDt date9.;

	/*carry over values of closeDt and reopenDate over records*/
	retain closeDt reopenDt;

	by ClaimNumber claimStatusCd notsorted;

	/*clear retained values for each new claim*/
	/*call missing() sets variables to missing/null*/
	if first.claimNumber then call missing(closeDt, reopenDt);

	/*first.status = 1 when claimStatusCd has changed since the previous record*/
	if first.claimStatusCd and claimStatusCd = 'Open' then call missing(closeDt, reopenDt);
	if first.claimStatusCd and claimStatusCd = 'Closed' then do;
		closeDt = bookDt;
		call missing(reopenDt);
	end;
	if first.claimStatusCd and claimStatusCd = 'Reopen' then do; 
		call missing(closeDt);
		reopenDt = bookDt;
	end;

	/*SAS outputs automatically if you do not have OUTPUT statements*/
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Oct 2019 18:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597790#M172281</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-10-18T18:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597810#M172290</link>
      <description>&lt;P&gt;Thank you very much.&amp;nbsp; That is the exact result I was looking for.&amp;nbsp; I know how to do this in VBA but have never attempted this in SAS before.&amp;nbsp; I really appreciate the assistance and now I have a model that I can use going forward with similar data sets that I will have to complete.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 18:36:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597810#M172290</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-18T18:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597811#M172291</link>
      <description>&lt;P&gt;You're welcome! I struggled with the VBA-to-SAS transition not being able to just go back up a row and easily reference previous values.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 18:39:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/597811#M172291</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-10-18T18:39:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598410#M172569</link>
      <description>&lt;P&gt;So, I am doing the exact same thing, but I now need to use 4 columns instead of two.&amp;nbsp; I took the code above and tried to modify it but I the results were not correct.&amp;nbsp; Below was how I edited the code, so it appears that I do not understand the code as much as I thought I did.&amp;nbsp; I have attached another sample with the results I am looking for.&amp;nbsp; Any additional assistance would be appreciated&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*This Data Step will assign the first Book Date to each change in status, at the exposure level, until a new status change is detected on the exposure.*/
DATA WORK.STATUS_LIST_EXPOSURE_1; 
	set WORK.STATUS_LIST_EXPOSURE;

	format  closeDt reopenDt date9.;

	/*carry over values of closeDt and reopenDate over records*/
	retain closeDt reopenDt;

	by ClaimNumber ClaimantCd CoverageCd FeatureStatusCd notsorted;

	/*clear retained values for each new claim*/
	/*call missing() sets variables to missing/null*/
	if first.claimNumber then call missing(closeDt, reopenDt);

	/*first.status = 1 when FeatureStatusCd has changed since the previous record*/
	if first.claimNumber AND first.ClaimantCd AND first.CoverageCd AND FeatureStatusCd = 'Open' then call missing(closeDt, reopenDt);
	if first.claimNumber AND first.ClaimantCd AND first.CoverageCd AND FeatureStatusCd = 'Closed' then do;
		closeDt = bookDt;
		call missing(reopenDt);
	end;
	if first.claimNumber AND first.ClaimantCd AND first.CoverageCd AND FeatureStatusCd = 'Reopen' then do; 
		call missing(closeDt);
		reopenDt = bookDt;
	end;

	/*SAS outputs automatically if you do not have OUTPUT statements*/
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Oct 2019 14:41:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598410#M172569</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-22T14:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598415#M172570</link>
      <description>&lt;P&gt;So there's 2 concepts here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. RETAIN statement - this holds values across rows. This is how you can have the same value of closeDt many times without having to set it each row.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. First-DOT processing. We're assuming that your records in your BY statement are in the correct order (the notsorted option at the end allows records to not be sorted, if desired). The value of first.&amp;lt;variable&amp;gt; will be 1 when the field is different from the previous value of that field (if it's in the BY statement). Otherwise 0. The same is true of last.&amp;lt;variable&amp;gt;, except this is true when the field is the last occurrence of the current value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is easier to see - note how the fields are 1 or 0 depending on their first/last values by column order:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
	set STATUS_LIST_EXPOSURE;

	by ClaimNumber ClaimantCd CoverageCd FeatureStatusCd notsorted;

	first_claimNumber=first.ClaimNumber;
	last_ClaimNumber=last.ClaimNumber;

	first_ClaimantCd=first.ClaimantCd;
	last_ClaimantCd=last.ClaimantCd;

	first_CoverageCd=first.CoverageCd;
	last_CoverageCd=last.CoverageCd;
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Check your first-DOT logic against the values using this new dataset as a visual.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hint: check your first.CoverageCd.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 14:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598415#M172570</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-10-22T14:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598501#M172596</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp; I can see the data now and it does make sense now.&amp;nbsp; So, looking at the ones and zeros in the data, the records that I want to see are all 0's, so my results come back blank.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I guess the best way to put this is that I want the first combination of all of those fields where the one field is Open, etc....&amp;nbsp; Should I concatenate those three fields and use the first code you sent me on the concatenated field, in place of the claimnumber?&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 19:34:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598501#M172596</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-22T19:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598509#M172598</link>
      <description>&lt;P&gt;You probably don't need to concatenate anything. Has your business logic changed? I now see that your StatSequence and BookDt is out of order. What are you trying to accomplish with each column? Your new sample data makes much less sense to me.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 19:51:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598509#M172598</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-10-22T19:51:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598671#M172665</link>
      <description>&lt;P&gt;In the first data set, I am taking the claim, the status and finding the first time the status changes and applying that book date to all transactions after that until the status changes again.&amp;nbsp; In this new data set, I am doing the exact same thing, except that I am breaking out a claim into its different parts.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In a claim, we have parts of the claim that can open and close while the claim, as a whole, remains open.&amp;nbsp; In my sample data, each part is defined as the combination of claimant number and coverage code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each claimant/coverage combination within a claim, I need to populate those closed and reopen dates using the same logic as my first data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can ignore the statsequ number.&amp;nbsp; That field simply numbers all transaction across all claims, in order of when the transactions occurred.&amp;nbsp; It is primarily used to order transactions that occur on the same date.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 11:49:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598671#M172665</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-23T11:49:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598688#M172673</link>
      <description>&lt;P&gt;I think this is what you need. This doesn't use the CoverageCd in the BY statement. I see that you carry over the 20-Feb-14 value from the last AA coverageCd to the first BB coverageCd. This clears values when you 1.) start a new claimnumber, 2.) start a new claimantCd, or 3.) Reach an 'Open'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This matches your excel sheet, but I'm not 100% if if matches your business logic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	format closeDt reopenDt date9.;

	/*carry over values of closeDt and reopenDate over records*/
	retain closeDt reopenDt;

	/*I removed coverageCd here since it looks like you carry over the value of 
	20-Feb-14 from line 8 to 9 (AA to BB)*/
	by ClaimNumber ClaimantCd FeatureStatusCd notsorted;

	/*looks like you clear values for new claimantCd, and not CoverageCds*/
	if first.claimNumber or first.claimantCd then call missing(closeDt, reopenDt);

	/*clear values if 'Open'*/
	if FeatureStatusCd = 'Open' then call missing(closeDt, reopenDt);

	/*if a new 'Closed'*/
	if FeatureStatusCd = 'Closed' and first.FeatureStatusCd then do;
		closeDt = bookDt;
		call missing(reopenDt);
	end;
	/*if a new 'reopen'*/
	if FeatureStatusCd = 'Reopen' and first.FeatureStatusCd then do; 
		call missing(closeDt);
		reopenDt = bookDt;
	end;

	/*SAS outputs automatically if you do not have OUTPUT statements*/
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Oct 2019 12:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598688#M172673</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-10-23T12:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598733#M172692</link>
      <description>&lt;P&gt;Actually, that AA to BB carry of the date was an error.&amp;nbsp; I need it to reset with each Claim, Claimant and Coverage combo.&amp;nbsp; I reattached the excel file with the correction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I added the coveragecd back into the by statement and it appears to work as expected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once again, thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 14:51:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598733#M172692</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-23T14:51:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598744#M172698</link>
      <description>&lt;P&gt;You're welcome! I know the VBA to SAS transition isn't 1-1.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 15:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598744#M172698</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-10-23T15:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598755#M172702</link>
      <description>When I clear out the values if Open, can I also set another field called OpenDt to the book date and keep it regardless of the other status changes for that Claimant/CovCd combo?</description>
      <pubDate>Wed, 23 Oct 2019 15:33:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598755#M172702</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-23T15:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598761#M172704</link>
      <description>&lt;P&gt;Sure you can! You'll want to add your new OpenDt field to the RETAIN statement. I suspect you'll also want to clear out that value for each new ClaimNumber. If you don't clear out OpenDt like you do for CloseDt and reopenDT, it will just persist&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You'll want to consider how to handle 2 'Open's in a row like you have in rows 38 and 39. If you only want the first one, then you'll need to use some first-DOT processing.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 15:41:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598761#M172704</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-10-23T15:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598767#M172707</link>
      <description>&lt;P&gt;I don't totally follow your logic and since you haven't posted data in a usable format (xslx doesn't really count) not going to play with it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that when you have multiple variables in the BY statement the effect is nested.&amp;nbsp; So if you use BY A B C.&amp;nbsp; The flag FIRST.B is the true in the first observation for a set of observations that have the same value of B within the larger set of observations that have the current value of A also.&amp;nbsp; Another way to think about it is that when you start a new A group then you also start a new B and C group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To keep track of the first occurrence of an event within a group you can use a flag that tells you whether the event has occurred yet.&amp;nbsp; If you are setting a variable to missing then setting it to non-missing when the event occurs then you might just be able to use that same variable as the flag.&amp;nbsp; So to save the current date into a new variable named first_date when a particular status is found you could use code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if status='LOOKING FOR THIS' and missing(first_date) then first_date = current_date;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or perhaps more simply:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if status='LOOKING FOR THIS'  then first_date = coalesce(first_date,current_date);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Watch out for observations that meet your status criteria but have missing values of the variable you want to capture. What do you want to do in that case? Should the new variable be left missing?&amp;nbsp; Should we keep looking for another observation with that status that doesn't have a missing value for the other variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To further expand your flexibility google the DOW loop concept in SAS data step code.&amp;nbsp;&amp;nbsp;&lt;A href="https://www.google.com/search?q=%40sas.com+dow+loop" target="_blank"&gt;https://www.google.com/search?q=%40sas.com+dow+loop&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 16:15:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598767#M172707</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-23T16:15:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Loop in a data step to populate values in other fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598804#M172725</link>
      <description>&lt;P&gt;So yes, I would need to do some first logic because I want the first open date to carry until the last occurrence of that claimant/coverage combo within that claim.&amp;nbsp; I added that and it appears to be working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 19:23:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-Loop-in-a-data-step-to-populate-values-in-other-fields/m-p/598804#M172725</guid>
      <dc:creator>gossetm3</dc:creator>
      <dc:date>2019-10-23T19:23:29Z</dc:date>
    </item>
  </channel>
</rss>

