<?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: Consolidate records from three variables to new variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684490#M207427</link>
    <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
 infile cards dsd  dlm=",";
 input
   ID          $
   EDATE       :mmddyy10.
   RATING      $
   OUTLOOK     :$50.
   CREDITWATCH :$50.
 ;
 format
   EDATE mmddyy10.
   ;
 cards;
1,02/20/2020,,positive outlook, 
1,02/20/2020,B-,, 
1,04/08/2020,,,developing watch
1,04/09/2020,CCC+,, 
2,03/28/2018,,negative outlook, 
2,03/28/2018,B,, 
2,02/24/2020,,,negative watch
2,02/24/2020,,stable outlook, 
2,02/24/2020,B-,, 
run;

data WANT;
  merge TABLE1 ;
  by ID EDATE;

  length KEEP_RATING KEEP_OUTLOOK KEEP_CRWATCH $16;
  retain KEEP_RATING KEEP_OUTLOOK KEEP_CRWATCH;
  if first.ID then call missing(KEEP_RATING);
  if first.EDATE then call missing(KEEP_OUTLOOK, KEEP_CRWATCH);
  KEEP_RATING =coalescec(RATING     ,KEEP_RATING );
  KEEP_OUTLOOK=coalescec(OUTLOOK    ,KEEP_OUTLOOK);
  KEEP_CRWATCH=coalescec(CREDITWATCH,KEEP_CRWATCH);

  if last.EDATE then do;
    RATING         = KEEP_RATING;          
    OUTLOOKORWATCH = ifc( KEEP_OUTLOOK^=' ' and KEEP_CRWATCH^=' ', 'wrong'
                   , ifc( KEEP_OUTLOOK =' ' and KEEP_CRWATCH =' ', 'stable outlook' 
                   ,                                               coalescec(KEEP_OUTLOOK ,KEEP_CRWATCH)));
   output;
  end;                               
  keep ID EDATE RATING OUTLOOKORWATCH;
run;
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;&lt;A name="IDX" target="_blank"&gt;&lt;/A&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt;&lt;/COLGROUP&gt; &lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;ID&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;edate&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;Rating&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;OUTLOOKORWATCH&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;02/20/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;B-&lt;/TD&gt;
&lt;TD class="l data"&gt;positive outlook&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;04/08/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;B-&lt;/TD&gt;
&lt;TD class="l data"&gt;developing watch&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;3&lt;/TH&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;04/09/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;CCC+&lt;/TD&gt;
&lt;TD class="l data"&gt;stable outlook&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;4&lt;/TH&gt;
&lt;TD class="l data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;03/28/2018&lt;/TD&gt;
&lt;TD class="l data"&gt;B&lt;/TD&gt;
&lt;TD class="l data"&gt;negative outlook&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;5&lt;/TH&gt;
&lt;TD class="l data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;02/24/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;B-&lt;/TD&gt;
&lt;TD class="l data"&gt;wrong&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;Note that there are tabs in your code. They mess up the data. Do not do that.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Sep 2020 03:54:43 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-09-17T03:54:43Z</dc:date>
    <item>
      <title>Consolidate records from three variables to new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684382#M207385</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to transfer table 1 to table 2.&lt;/P&gt;&lt;P&gt;Table 1&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;obs&lt;/TD&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;edate&lt;/TD&gt;&lt;TD&gt;Rating&lt;/TD&gt;&lt;TD&gt;Outlook&lt;/TD&gt;&lt;TD&gt;CreditWatch&lt;/TD&gt;&lt;/TR&g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;/TD&gt;&lt;TD&gt;B-&lt;/TD&gt;&lt;TD&gt;developing watch&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;04/09/2020&lt;/TD&gt;&lt;TD&gt;CCC+&lt;/TD&gt;&lt;TD&gt;stable outlook&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;03/28/2018&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;negative outlook&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;02/24/2020&lt;/TD&gt;&lt;TD&gt;B-&lt;/TD&gt;&lt;TD&gt;wrong&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. for each ID group, if the rows have the same&amp;nbsp;'edate' in table 1, then convert them to the same row in table 2.&lt;/P&gt;&lt;P&gt;For example,&lt;/P&gt;&lt;P&gt;1)the observation 1,2 in Table 1 are both ID '1' and have the same 'edate', then the value in 'Rating' in Table 1 becomes the value in 'Rating' in Table 2.&lt;/P&gt;&lt;P&gt;2) the value in 'outlook' variable in 'positive outlook' and the non-value exist in 'CreditWatch' variable, then file ''positive outlook' in variable 'outlookorwatch' in Table 2.&lt;/P&gt;&lt;P&gt;2. &lt;SPAN&gt;The value in ‘Rating’ variable does not change until the next record in ‘rating’ variable happens.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;For example,&lt;/P&gt;&lt;P&gt;the rating is 'B-' in obs 2 in Table 2, because no new 'rating' record happens in&amp;nbsp;04/08/2020.&lt;/P&gt;&lt;P&gt;3. if there is no record of ‘CreditWatch’ or ‘Outlook’, the value in ‘outlookorwatch’ is ‘stable outlook’.&lt;/P&gt;&lt;P&gt;For example,&lt;/P&gt;&lt;P&gt;the&amp;nbsp;outlookorwatch is&amp;nbsp;‘stable outlook’ in obs 3 in Table 2, because&amp;nbsp;no record of ‘CreditWatch’ or ‘Outlook’ happen in 04/09/2020。&lt;/P&gt;&lt;P&gt;4. if ‘CreditWatch’ record and ‘Outlook’ happen at the same time, I would like to set ‘outlookorwatch’ as 'wrong'.&lt;/P&gt;&lt;P&gt;For example,&lt;/P&gt;&lt;P&gt;Record in variable ‘CreditWatch’ and ‘Outlook’ happens at the same date (i.e., 02/24/2020) in obs 8,9 of table 1, then the value in ‘outlookorwatch’ variable in table 2 is 'wrong'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please give me some suggestion about this ?&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;PRE&gt;data table1;
	infile cards dsd  dlm=",";
	input
	ID $
	edate :mmddyy10.
	Rating $
	Outlook :$50.
	CreditWatch :$50.
	;
	format
   edate mmddyy10.
   ;
	cards;
1,02/20/2020,,positive outlook, 
1,02/20/2020,B-,, 
1,04/08/2020,,,developing watch
1,04/09/2020,CCC+,, 
2,03/28/2018,,negative outlook, 
2,03/28/2018,B,,	
2,02/24/2020,,,negative watch
2,02/24/2020,,stable outlook, 
2,02/24/2020,B-,,	
	;;;;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 21:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684382#M207385</guid>
      <dc:creator>Alexxxxxxx</dc:creator>
      <dc:date>2020-09-16T21:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: Consolidate records from three variables to new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684398#M207393</link>
      <description>&lt;P&gt;This is a complicated problem and it looks like this is the second time you have posted it. I suspect few will want to write it for you.&lt;/P&gt;
&lt;P&gt;It can be solved using this type of code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sort data=table1;
  by ID edate;
run;
data results;
  format 	ID $1.
        	edate mmddyy10.
        	Rating $2.
        	outlookorwatch $50.;
  retain Rating outlookorwatch;
  set table1(rename=(Rating = Rating_in));
  by ID edate;
  if first.ID then outlookorwatch = "stable outlook";
  if first.edate then ... 
  Rating = coalescec(Rating_in,Rating);
  outlookorwatch = coalescec(Outlook,CreditWatch);
  if last.ID then output;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 22:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684398#M207393</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-09-16T22:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Consolidate records from three variables to new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684439#M207408</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/262815"&gt;@Alexxxxxxx&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a very complicated request.&amp;nbsp; Yipes.&amp;nbsp; I have a solution.&amp;nbsp; Here's the code, and then I'll discuss it below the code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
	infile cards dsd  dlm=",";
	input
	ID $
	edate :mmddyy10.
	Rating $
	Outlook :$50.
	CreditWatch :$50.
	;
	format
   edate mmddyy10.
   ;
cards;
1,02/20/2020,,positive outlook,
1,02/20/2020,B-,,
1,04/08/2020,,,developing watch
1,04/09/2020,CCC+,, 
2,03/28/2018,,negative outlook,
2,03/28/2018,B,,
2,02/24/2020,,,negative watch
2,02/24/2020,,stable outlook,
2,02/24/2020,B-,,
;;;;
run;

PROC SORT DATA=Table1;
	By ID	eDATE;
Run;

DATA	Table2;
	LENGTH	ID						$8
			Prior_ID				$8
			edate					8
			prior_edate				8
			Rating					$8
			Prior_Rating			$8
			Outlook					$50
			CreditWatch				$50
			OutlookOrWatch			$100
			Prior_OutlookOrWatch	$100
			Type					$8
			Prior_Type				$8
			;
	DROP	Outlook
			CreditWatch
			prior_edate
			Prior_Rating
			Prior_ID
			Prior_OutlookOrWatch
			Type
			Prior_Type
			;
	RETAIN	Prior_ID				' '
			prior_edate				0
			prior_rating			' '	
			OutlookOrWatch			' '
			Prior_OutlookOrWatch	' '
			Prior_Type				' '
			;
	FORMAT	Prior_edate				MMDDYYS10.;

	SET	Table1;
		BY	ID	Edate;

	IF	NOT	MISSING(Outlook)									THEN
		DO;
			OutlookOrWatch					=	Outlook;
			Type							=	'RATE';
		END;
	ELSE
	IF	NOT	MISSING(CreditWatch)								THEN
		DO;
			OutlookOrWatch					=	CreditWatch;
			Type							=	'RATE';
		END;
	ELSE
		DO;
			Type							=	'NON-RATE';
		END;

	IF	MISSING(Rating)											AND
		ID									=	Prior_ID		THEN
		Rating								=	Prior_Rating;

	IF	Prior_OutlookOrWatch				^=	OutlookOrWatch	AND
		NOT	MISSING(OutlookOrWatch)								AND
		NOT	MISSING(Rating)										THEN
		DO;
			LINK	Output_Routine;
		END;
	ELSE
	IF	Rating								^=	Prior_Rating	AND
		NOT	MISSING(Rating)										THEN
		DO;
			LINK	Output_Routine;
		END;
	ELSE
	IF	NOT	MISSING(OutlookOrWatch)								AND
		NOT	MISSING(Rating)										THEN
		DO;
			LINK	Output_Routine;
		END;

	IF	LAST.ID													THEN
		DO;
			CALL	MISSING(prior_ID, Prior_Rating, prior_edate, OutlookOrWatch, Prior_OutlookOrWatch, Prior_Type);
		END;
	ELSE
		DO;
			Prior_ID						=	ID;
			Prior_edate						=	edate;
			Prior_Rating					=	Rating;
			Prior_OutlookOrWatch			=	OutlookOrWatch;
			Prior_Type						=	Type;
		END;

	******;
	RETURN;
	******;

	**************;
	Output_Routine:
	**************;
		IF	MISSING(OutlookOrWatch)								THEN
			DO;
				OutlookOrWatch				=	'stable outlook';
			END;
		ELSE
		IF	Type							=	'RATE'			AND
			Prior_Type						=	'RATE'			AND
			edate							=	prior_edate		THEN
			DO;
				OutlookOrWatch				=	'wrong';
			END;

		OUTPUT;
		CALL	MISSING(OutlookOrWatch);

	******;
	RETURN;
	******;
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;Here are the results, which I put into Table2:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1600301793225.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49461iC7FBFAE46A71FEBF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1600301793225.png" alt="jimbarbour_0-1600301793225.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Now, these results look slightly different than yours.&amp;nbsp; Here's what's going on:&lt;/P&gt;
&lt;P&gt;When record 7 comes in from the source data, SAS doesn't know that record 7 is an error yet.&amp;nbsp; SAS can't know that until it reads record 8.&amp;nbsp; At the point of record 7, SAS has a rating and an outlook/watch value, so the proper response is to write out "B - Negative Watch."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When record 8 comes in from the source data, now SAS knows an error has occurred and writes out "B - Wrong".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's nothing wrong with record 9 per your specifications, but SAS doesn't have a outlook/watch for record 9, so SAS defaults to "stable outlook".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code that I have written meets your specifications except that on record 7 where SAS doesn't know yet what is in record 8.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, you can get SAS to know about record 8 by:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; Deferred output processing.&amp;nbsp; Save each record and do not write until the following record has been read and evaluated in light of the prior record.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; Read ahead processing.&amp;nbsp; Write code to do a direct read on the next record in the Table1 SAS data set and evaluate the current record in light of the values from the read ahead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whether you do deferred output processing or read ahead processing, you're asking for a fairly complicated process.&amp;nbsp; If you really want that, go for it, but I think I've written enough code for this thread already.&amp;nbsp;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&amp;nbsp; The code I have written will identify "wrong" data.&amp;nbsp; I would argue that a program that identifies "wrong" data is sufficient and that we shouldn't waste time writing code to identify "wrong" data in a fancier way.&amp;nbsp; Instead, when "wrong" data is identified, the data should be corrected and the program re-run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hopefully we can consider this the solution to your problem.&amp;nbsp; If however you really need for some reason the final output to be just exactly as you stated in your 2nd table, I can give you some tips.&amp;nbsp; For one,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51532"&gt;@LeonidBatkhan&lt;/a&gt;&amp;nbsp;wrote an excellent blog post on read ahead type processing.&amp;nbsp; I don't recommend that you use read ahead processing in this case (just correct the data and re-run!), but the post itself is very worthwhile.&amp;nbsp; Link:&amp;nbsp;&amp;nbsp;&lt;A href="https://blogs.sas.com/content/sgf/2017/11/01/hopping-for-the-best-calculations-across-sas-dataset-observations/" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/sgf/2017/11/01/hopping-for-the-best-calculations-across-sas-dataset-observations/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 02:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684439#M207408</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-17T02:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: Consolidate records from three variables to new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684490#M207427</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
 infile cards dsd  dlm=",";
 input
   ID          $
   EDATE       :mmddyy10.
   RATING      $
   OUTLOOK     :$50.
   CREDITWATCH :$50.
 ;
 format
   EDATE mmddyy10.
   ;
 cards;
1,02/20/2020,,positive outlook, 
1,02/20/2020,B-,, 
1,04/08/2020,,,developing watch
1,04/09/2020,CCC+,, 
2,03/28/2018,,negative outlook, 
2,03/28/2018,B,, 
2,02/24/2020,,,negative watch
2,02/24/2020,,stable outlook, 
2,02/24/2020,B-,, 
run;

data WANT;
  merge TABLE1 ;
  by ID EDATE;

  length KEEP_RATING KEEP_OUTLOOK KEEP_CRWATCH $16;
  retain KEEP_RATING KEEP_OUTLOOK KEEP_CRWATCH;
  if first.ID then call missing(KEEP_RATING);
  if first.EDATE then call missing(KEEP_OUTLOOK, KEEP_CRWATCH);
  KEEP_RATING =coalescec(RATING     ,KEEP_RATING );
  KEEP_OUTLOOK=coalescec(OUTLOOK    ,KEEP_OUTLOOK);
  KEEP_CRWATCH=coalescec(CREDITWATCH,KEEP_CRWATCH);

  if last.EDATE then do;
    RATING         = KEEP_RATING;          
    OUTLOOKORWATCH = ifc( KEEP_OUTLOOK^=' ' and KEEP_CRWATCH^=' ', 'wrong'
                   , ifc( KEEP_OUTLOOK =' ' and KEEP_CRWATCH =' ', 'stable outlook' 
                   ,                                               coalescec(KEEP_OUTLOOK ,KEEP_CRWATCH)));
   output;
  end;                               
  keep ID EDATE RATING OUTLOOKORWATCH;
run;
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;&lt;A name="IDX" target="_blank"&gt;&lt;/A&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt;&lt;/COLGROUP&gt; &lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;ID&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;edate&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;Rating&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;OUTLOOKORWATCH&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;02/20/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;B-&lt;/TD&gt;
&lt;TD class="l data"&gt;positive outlook&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;04/08/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;B-&lt;/TD&gt;
&lt;TD class="l data"&gt;developing watch&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;3&lt;/TH&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;04/09/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;CCC+&lt;/TD&gt;
&lt;TD class="l data"&gt;stable outlook&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;4&lt;/TH&gt;
&lt;TD class="l data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;03/28/2018&lt;/TD&gt;
&lt;TD class="l data"&gt;B&lt;/TD&gt;
&lt;TD class="l data"&gt;negative outlook&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;5&lt;/TH&gt;
&lt;TD class="l data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;02/24/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;B-&lt;/TD&gt;
&lt;TD class="l data"&gt;wrong&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;Note that there are tabs in your code. They mess up the data. Do not do that.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 03:54:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-records-from-three-variables-to-new-variables/m-p/684490#M207427</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-09-17T03:54:43Z</dc:date>
    </item>
  </channel>
</rss>

