<?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: Several row observations to be collapsed (works) and marked for a specific variable (needed) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690586#M210080</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VERY thankful! Here I try to approximate the data structure I actually have, with easier to look at dates:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DATA	Have;
	INPUT	ID				$
			admission_date	:	DDMMYY10.
			discharge_date	:	DDMMYY10.
			_Diagnosis_Code	$
			;

	FORMAT	Admission_Date	DDMMYYD10.;
	FORMAT	Discharge_Date	DDMMYYD10.;

DATALINES;
1       1/1/2000              1/2/2000                 A
1       1/2/2000              20/2/2000                B
1       1/2/2000              20/2/2000                C
1       1/10/2010             2/10/2010                A
1       1/11/2010             2/11/2010                D
2       1/2/2002              1/2/2002                 B
2       1/2/2005              20/2/2005                C
2       1/10/2010             2/10/2010                A
2       2/10/2010             2/11/2010                D
;
RUN;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If it ends up looking like this, then it is magical:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;min_admission_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; max_discharge_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;In_diag_code1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;In_diag_code2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;In_diag_code3&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/1/2000&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; 20/2/20000&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;A&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; B&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;C&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/10/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/10/2010&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;A&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/11/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/11/2010&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;D&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/2/2002&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;1/2/2002&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;B&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/2/2005&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;20/2/2005&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;C&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/10/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2/11/2010&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; A&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; D&lt;/P&gt;</description>
    <pubDate>Fri, 09 Oct 2020 21:25:33 GMT</pubDate>
    <dc:creator>JJ_211</dc:creator>
    <dc:date>2020-10-09T21:25:33Z</dc:date>
    <item>
      <title>Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690183#M209934</link>
      <description>&lt;P&gt;Hi &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data have:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; admission_date&amp;nbsp; &amp;nbsp; discharge_date&amp;nbsp; &amp;nbsp; &amp;nbsp;Diagnosis_code&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/1/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; D&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to clean these up so that dates on a "loop" are collapsed (note the same admission date as the previous discharge date in the second row, and exact same dates for the remaining rows, I also want them to be considered as one loop if the next admission date is 1 day after previous discharge date). Each row has it's own unique diagnosis code and these are of interest, depending on what they are.&lt;/P&gt;&lt;P&gt;I want it to look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data want:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; min_admission_date&amp;nbsp; &amp;nbsp; max_discharge_date&amp;nbsp; &amp;nbsp; &amp;nbsp;Diag_code_new&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;in_diag_code1&amp;nbsp; &amp;nbsp; &amp;nbsp;in_diag_code2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/1/2000&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;20/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; D&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; A&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; C&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I tried so far gives me what I need except for the last two columns which I'm very interested in getting (in the same row)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I tried is this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&amp;nbsp;&lt;/P&gt;&lt;P&gt;by ID admission_date;&lt;/P&gt;&lt;P&gt;length min_admission_date max_discharge_date 4.;&lt;/P&gt;&lt;P&gt;retain&amp;nbsp;min_admission_date max_discharge_date diag_code_new;&lt;/P&gt;&lt;P&gt;if first.id then do;&lt;/P&gt;&lt;P&gt;max_discharge_date=discharge_date;&lt;/P&gt;&lt;P&gt;min_admission_date=admission_date;&lt;/P&gt;&lt;P&gt;diag_code_new=diagnosis_code;&lt;/P&gt;&lt;P&gt;end:&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; if admission_date&amp;gt; (1+max_discharge_date) then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; output;&lt;/P&gt;&lt;P&gt;min_admission_date=admission_date;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;max_discharge_date=max(discharge_date, max_discharge_date);&lt;/P&gt;&lt;P&gt;diag_code_new=diagnosis_code;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if last.id then do;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;format min_admission_date max_discharge_date ddmmyy10.;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where can I modify to be able to get the last two columns if specific diagnosis codes show up during the loop clean up process?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 20:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690183#M209934</guid>
      <dc:creator>JJ_211</dc:creator>
      <dc:date>2020-10-08T20:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690253#M209988</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/350978"&gt;@JJ_211&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I took your code and modified it a bit to hopefully give you what you want.&amp;nbsp; I also added a little SQL routine to figure out the maximum number of distinct codes for any one ID -- a number I use to build and populate a retained array.&amp;nbsp; I wasn't completely clear on the distinction between Diag_Code_New and the In_Diag_Codes, so I just put everything into In_Diag_Codes.&amp;nbsp; Take a look at the code and the output and adjust to suit.&amp;nbsp; I also wasn't clear on why diagnosis B was not included, so I (for now) just included all diagnoses.&amp;nbsp; If you need to limit the diagnoses to just certain diagnoses, that's easy enough to do.&amp;nbsp; &amp;nbsp; I also added more data just so I had a better idea as to whether or not my code was doing what it was supposed to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&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;
&lt;P&gt;Here's the code and below that the results.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Have;
	INPUT	ID				$
			admission_date	:	DDMMYY10.
			discharge_date	:	DDMMYY10.
			_Diagnosis_Code	$
			;

	FORMAT	Admission_Date	DDMMYYD10.;
	FORMAT	Discharge_Date	DDMMYYD10.;

DATALINES;
1       1/1/2000              1/2/2000                 A
1       1/2/2000              20/2/2000                B
1       1/2/2000              20/2/2000                C
1       1/2/2000              20/2/2000                D
2       2/2/2020              2/2/2020                 E
2       2/2/2020              2/2/2020                 F
2       2/2/2020              2/2/2020                 G
2       2/2/2020              2/2/2020                 H
2       2/2/2020              2/2/2020                 I
;
RUN;

**------------------------------------------------------------------------------**;

**	Get the Max nbr of distinct codes for any one ID.	**;
PROC	SQL	NOPRINT;
	SELECT	MAX(	Nbr_Of_Codes	)
		INTO	:	Nbr_Of_Codes
		FROM	(
			SELECT	DISTINCT	COUNT(_Diagnosis_Code)
				AS		Nbr_Of_Codes
				FROM	Have
				GROUP	BY	ID)
				;
QUIT;

%LET	Nbr_Of_Codes	=	%SYSFUNC(COMPRESS(&amp;amp;Nbr_Of_Codes));
%PUT	NOTE:  &amp;amp;=Nbr_Of_Codes;

**------------------------------------------------------------------------------**;

DATA	Want;
	DROP	_:;

	SET	Have; 
		BY	ID	admission_date;

	length	min_admission_date max_discharge_date 			4;
	LENGTH	In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes	$8;

	ARRAY	Diag	[*]	In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes;

	retain	min_admission_date 
			max_discharge_date 
			In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes
			;

	if	_Diagnosis_Code					NOT	IN	Diag	THEN
		DO;
			DO	_i							=	1	TO	&amp;amp;Nbr_Of_Codes;
				IF	MISSING(Diag[_i])					THEN
					DO;
						Diag[_i]			=	_Diagnosis_Code;
						_i					=	&amp;amp;Nbr_Of_Codes;
					END;
			END;
		END;

	if first.id then 
		do;
			max_discharge_date				=	discharge_date;
			min_admission_date				=	admission_date;
		end;
	else
		do;
			if	admission_date				&amp;gt; 	(1 + max_discharge_date) then 
				do;
					output;
					min_admission_date		=	admission_date;
				end;
			max_discharge_date 				= 	max(discharge_date, max_discharge_date);
		end;

	if	last.id then 
		do;
			output;
			CALL	MISSING(min_admission_date, max_discharge_date);
			CALL	MISSING(of In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes);
		end;

	format min_admission_date max_discharge_date DDMMYYD10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1602213515343.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50406i20219E67F95F12B5/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1602213515343.png" alt="jimbarbour_0-1602213515343.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 03:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690253#M209988</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-09T03:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690303#M210011</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;, I posted a reply earlier that seems to have been removed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for the code! Saved my life indeed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I however get code columns that make it difficult to find which of them are specific to the admission. So to follow up on my data have, after coding, I get the following:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;min_admission_date&amp;nbsp; &amp;nbsp;max_discharge_date&amp;nbsp; In_diag_code1.....................in_diag_code7&amp;nbsp; &amp;nbsp; in_diag_code8&amp;nbsp; &amp;nbsp;in_diag_code9&amp;nbsp; in_diag_code10&amp;nbsp; &amp;nbsp;in_diag_code11&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp;&amp;nbsp;Previous admission dates&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;Code1&amp;nbsp; Code2&amp;nbsp; &amp;nbsp;Code&amp;nbsp; Code&amp;nbsp; Code&amp;nbsp; Code&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; Previous admission dates&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;Code1&amp;nbsp; Code2&amp;nbsp; &amp;nbsp;Code&amp;nbsp; Code&amp;nbsp; Code&amp;nbsp; Code&amp;nbsp; Code7&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; Previous admission dates&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;Code1&amp;nbsp; Code2&amp;nbsp; &amp;nbsp;Code&amp;nbsp; Code&amp;nbsp; Code&amp;nbsp; Code&amp;nbsp; Code7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;1/1/2000&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; 20/2/2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Code1&amp;nbsp; Code2&amp;nbsp; &amp;nbsp;Code&amp;nbsp; Code&amp;nbsp; Code&amp;nbsp; Code&amp;nbsp; Code7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&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;C&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;D&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm interested in having rows (as per your output image), where each admission (after being cleaned up) lists all the codes that were specific to dates in that admission only, whereas now I can see codes from previous admissions of the same ID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where can I modify in the code?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 10:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690303#M210011</guid>
      <dc:creator>JJ_211</dc:creator>
      <dc:date>2020-10-09T10:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690378#M210035</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/350978"&gt;@JJ_211&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wasn't quite clear on exactly what a "loop" meant or which records should be collapsed and consolidated.&amp;nbsp; I did know how to keep the diagnostic codes, a retained array, so I introduced the array and just used ID as the break point to clear the array and re-start accumulation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;			CALL	MISSING(of In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;clears the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Copy that line into whatever point(s) in the code determine(s) the break point in record consolidation, and the accumulation of diagnoses will start afresh.&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>Fri, 09 Oct 2020 13:56:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690378#M210035</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-09T13:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690561#M210071</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That makes sense. I have tried putting that call (missing) code in different places where I thought it should start afresh but with no luck. I am of course doing something wrong. Could you advise on where you would put it, if what you want is described as the following:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each admission row in the original have dataset, there was a diagnosis code, and there are several admission rows for the same ID, where different diagnosis codes are registered. What I meant with loop is those admission and discharge dates which I end up collapsing (when a new min_admission_date and max_discharge_date are created). What I want is that when these admission rows are collapsed, all the diagnoses codes that were registered there appear as in_diag_code1... N (depending on how many).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So if one ID has 4 rows which were collapsed and end up appearing as one row in my want dataset, then the 4 diagnosis codes should appear in 4 columns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then the SAME ID would have another 3 rows which were also collapsed and appear as another row in my want dataset, then for that row I want the 3 diagnosis codes in 3 columns next to it (which are then not necessarily the same as the 4 rows above it).&amp;nbsp;&lt;/P&gt;&lt;P&gt;As such, the same ID ends up having several rows in the want dataset which resulted after being collapsed, and their diagnosis codes are specific to each row (so the diagnosis codes that were part of all admissions which resulted in the one collapsed row in the want dataset).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope you can guide me, this is the final step I need for magic to finally happen &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Janet&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 19:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690561#M210071</guid>
      <dc:creator>JJ_211</dc:creator>
      <dc:date>2020-10-09T19:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690583#M210078</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/350978"&gt;@JJ_211&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Janet, is there a way you can provide me with admission dates for a single ID that would not be within one day of the following admission date?&amp;nbsp; I think that's the data that would really let me know if I've done the programming correctly.&amp;nbsp; Ideally, they would be in the below format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I could mock something up, but then we're testing my understanding, a concept most fraught with peril.&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Have;
	INPUT	ID				$
			admission_date	:	DDMMYY10.
			discharge_date	:	DDMMYY10.
			_Diagnosis_Code	$
			;

	FORMAT	Admission_Date	DDMMYYD10.;
	FORMAT	Discharge_Date	DDMMYYD10.;

DATALINES;
1       1/1/2000              1/2/2000                 A
1       1/2/2000              20/2/2000                B
1       1/2/2000              20/2/2000                C
1       1/2/2000              20/2/2000                D
2       2/2/2020              2/2/2020                 E
2       2/2/2020              2/2/2020                 F
2       2/2/2020              2/2/2020                 G
2       2/2/2020              2/2/2020                 H
2       2/2/2020              2/2/2020                 I
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 20:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690583#M210078</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-09T20:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690586#M210080</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VERY thankful! Here I try to approximate the data structure I actually have, with easier to look at dates:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DATA	Have;
	INPUT	ID				$
			admission_date	:	DDMMYY10.
			discharge_date	:	DDMMYY10.
			_Diagnosis_Code	$
			;

	FORMAT	Admission_Date	DDMMYYD10.;
	FORMAT	Discharge_Date	DDMMYYD10.;

DATALINES;
1       1/1/2000              1/2/2000                 A
1       1/2/2000              20/2/2000                B
1       1/2/2000              20/2/2000                C
1       1/10/2010             2/10/2010                A
1       1/11/2010             2/11/2010                D
2       1/2/2002              1/2/2002                 B
2       1/2/2005              20/2/2005                C
2       1/10/2010             2/10/2010                A
2       2/10/2010             2/11/2010                D
;
RUN;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If it ends up looking like this, then it is magical:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;min_admission_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; max_discharge_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;In_diag_code1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;In_diag_code2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;In_diag_code3&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/1/2000&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; 20/2/20000&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;A&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; B&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;C&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/10/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/10/2010&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;A&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/11/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/11/2010&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;D&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/2/2002&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;1/2/2002&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;B&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/2/2005&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;20/2/2005&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;C&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1/10/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2/11/2010&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; A&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; D&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 21:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690586#M210080</guid>
      <dc:creator>JJ_211</dc:creator>
      <dc:date>2020-10-09T21:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690600#M210082</link>
      <description>&lt;P&gt;OK, the results should look something like this then:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1602281193370.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50510iACE84AB7BAE912E8/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1602281193370.png" alt="jimbarbour_0-1602281193370.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Program code is below.&amp;nbsp; I added a SORT which may not be necessary if you can count on the data always being in ID, admission date, and discharge date order.&amp;nbsp; I also slightly revised the SQL that grabs the max number of unique diagnoses for any one ID.&amp;nbsp; Be sure include the new SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The thing we need to do here before we mark this as "solved" is to run some data through that has records that do roll-up/consolidate and some that don't -- a larger sample size than I have now.&amp;nbsp; I'm sure you're doing this already, but I like to be &lt;STRIKE&gt;paranoid&lt;/STRIKE&gt; careful.&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Have;
	INPUT	ID				$
			_Admission_Date	:	DDMMYY10.
			_Discharge_Date	:	DDMMYY10.
			_Diagnosis_Code	$
			;

	FORMAT	_Admission_Date	DDMMYYD10.;
	FORMAT	_Discharge_Date	DDMMYYD10.;

DATALINES;
1       1/1/2000              1/2/2000                 A
1       1/2/2000              20/2/2000                B
1       1/2/2000              20/2/2000                C
1       1/10/2010             2/10/2010                A
1       1/11/2010             2/11/2010                D
2       1/2/2002              1/2/2002                 B
2       1/2/2005              20/2/2005                C
2       1/10/2010             2/10/2010                A
2       2/10/2010             2/11/2010                D
;
RUN;

**------------------------------------------------------------------------------**;

**	Get the Max nbr of distinct codes for any one ID.	**;
PROC	SQL	NOPRINT;
*PROC	SQL;
	SELECT	MAX(	Nbr_Of_Codes	)
		INTO	:	Nbr_Of_Codes
		FROM	(
			SELECT	ID
					,COUNT(DISTINCT	_Diagnosis_Code)
				AS		Nbr_Of_Codes
				FROM	Have
				GROUP	BY	ID
				)
				;
QUIT;

%LET	Nbr_Of_Codes	=	%SYSFUNC(COMPRESS(&amp;amp;Nbr_Of_Codes));
%PUT	NOTE:  &amp;amp;=Nbr_Of_Codes;

**------------------------------------------------------------------------------**;

PROC	SORT	DATA=Have;
	BY	ID	_Admission_Date	_Discharge_Date;
RUN;

**------------------------------------------------------------------------------**;

DATA	Want;
	DROP	_:;

	SET	Have; 
		BY	ID	_Admission_Date	_Discharge_Date;

	LENGTH	Min_Admission_Date	Max_Discharge_Date 			4;
	LENGTH	In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes	$8;

	ARRAY	Diag	[*]	In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes;

	retain	Min_Admission_Date 
			Max_Discharge_Date 
			In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes
			;

	IF first.id then 
		do;
			Max_Discharge_Date				=	_Discharge_Date;
			Min_Admission_Date				=	_Admission_Date;
		end;
	else
		do;
			IF	_Admission_Date				&amp;gt; 	(1 + Max_Discharge_Date) then 
				do;
					OUTPUT;
					Min_Admission_Date		=	_Admission_Date;
					CALL	MISSING(of In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes);
				end;
			Max_Discharge_Date 				= 	MAX(_Discharge_Date, Max_Discharge_Date);
		end;

	IF	_Diagnosis_Code					NOT	IN	Diag	THEN
		DO;
			DO	_i							=	1	TO	&amp;amp;Nbr_Of_Codes;
				IF	MISSING(Diag[_i])					THEN
					DO;
						Diag[_i]			=	_Diagnosis_Code;
						_i					=	&amp;amp;Nbr_Of_Codes;
					END;
			END;
		END;

	IF	last.id then 
		do;
			OUTPUT;
			CALL	MISSING(Min_Admission_Date, Max_Discharge_Date);
			CALL	MISSING(of In_Diag_Code1	-	In_Diag_Code&amp;amp;Nbr_Of_Codes);
		end;

	format Min_Admission_Date Max_Discharge_Date DDMMYYD10.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 22:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690600#M210082</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-09T22:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690604#M210084</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp; Hi Jim!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the code now on my some 30 million row dataset, and I looked at a few examples of before and after this code was applied. It looks like it IS doing all it's supposed to do, both for data that do roll ups and that don't (one rows and such), for those who had one row per ID and those who had multiple. Also looked at different combinations of codes and repetition of codes, but this DOES IT ALL &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Works perfectly, you are definitely a life saver!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many, many, MANY thanks to you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Janet&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 22:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690604#M210084</guid>
      <dc:creator>JJ_211</dc:creator>
      <dc:date>2020-10-09T22:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690607#M210087</link>
      <description>&lt;P&gt;You're welcome.&amp;nbsp; Glad it worked out.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That retained array trick is a handy one to keep in your "back pocket" for situations like the one you're describing.&amp;nbsp; Notice also the use of IN with an array reference (where I check to see if the diagnosis is already in the array).&amp;nbsp; I don't have to check each position in the array.&amp;nbsp; IN checks them all in a single reference.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 22:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/690607#M210087</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-09T22:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/691002#M210265</link>
      <description>Very useful tip for what I'm about to do next, looking up specific diagnoses in the array (ends up being hundreds of codes for the nbr_of_codes).&lt;BR /&gt;&lt;BR /&gt;Many thanks again!!&lt;BR /&gt;Janet</description>
      <pubDate>Mon, 12 Oct 2020 15:55:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/691002#M210265</guid>
      <dc:creator>JJ_211</dc:creator>
      <dc:date>2020-10-12T15:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/691044#M210284</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/350978"&gt;@JJ_211&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're welcome, Janet.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way:&lt;/P&gt;
&lt;P&gt;The IN operator will tell you if a particular value is in an array without having to index one-by-one through the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The WHICHC function will tell you which position in the array (or list of variables) holds a particular value.&amp;nbsp; Say you have the highly realistic list of ailments as in the below SAS code.&amp;nbsp; WHICHC will find which ordinal position a given value is in.&amp;nbsp; Given the dataset produced by the first Data step, the WHICHC function in the second Data step would give us the following results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1602526558086.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50568i0DD94BEEA6AF4ADF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1602526558086.png" alt="jimbarbour_0-1602526558086.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are but three values in our dataset, but there are four rows in the output.&amp;nbsp; Why?&amp;nbsp; Well, I told WHICHC to look for a value related to but not present in our dataset, and, of course, it's not there.&amp;nbsp; WHICHC returned a zero.&amp;nbsp; This is a nice feature in many ways.&amp;nbsp; For one, WHICHC can be used, just like the IN operator, to tell whether a value is present at all.&amp;nbsp; For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF WHICHC(Ailments[*]) THEN 
    DO;
          ...Code to be executed only if a value exists somewhere within the array...
    END;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If a given value is not present in the array, WHICHC returns 0 which evaluates as False, and the code in the DO - END is skipped.&amp;nbsp; Conversely, if the value is present, WHICHC returns some positive number, any of which evaluates as True, and the code in the DO - END is executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lastly, WHICHC can be used even if there is no array reference by listing the individual variables, in this case,&amp;nbsp;Ailment1 - Ailment&amp;amp;Nbr_Of_Codes.&amp;nbsp; See the third Data step.&amp;nbsp; The output from the second and third Data steps is identical.&amp;nbsp; Which is the better method (array vs. list of variables?).&amp;nbsp; If I'm going to use an array elsewhere anyway, then I'd probably use an array with the WHICHC.&amp;nbsp; Otherwise, I might just go with the list of variables.&amp;nbsp; I don't think there's a significant difference in terms of performance, and I have tested variable lists vs array references up to 500 million records without seeing a material performance gain or loss.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nota bene:&amp;nbsp; The word "of" is very important in functions.&amp;nbsp; If you code Ailment1 - Ailment&amp;amp;Nbr_Of_Codes without the "of," then SAS will assume that you intend to do subtraction.&amp;nbsp; Let's just say your results might be a wee tad different if you leave the "of" out.&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET	Nbr_Of_Codes	=	3;

DATA	Text_Ailments;
	Ailment1	=	'Sick';
	Ailment2	=	'Sicker';
	Ailment3	=	'Sickest';
RUN;

DATA	Coded_Ailments;
	DROP	Ail:;
	SET	Text_Ailments;
	ARRAY	Ailments	[&amp;amp;Nbr_Of_Codes]	$	Ailment1 - Ailment&amp;amp;Nbr_Of_Codes;

	Code	=	WHICHC('Sick', of Ailments[*]);
	OUTPUT;

	Code	=	WHICHC('Sicker', of Ailments[*]);
	OUTPUT;

	Code	=	WHICHC('Sickest', of Ailments[*]);
	OUTPUT;

	Code	=	WHICHC('Sickly', of Ailments[*]);
	OUTPUT;
RUN;

DATA	Coded_Ailments2;
	DROP	Ail:;
	SET	Text_Ailments;
	
	Code	=	WHICHC('Sick', of Ailment1 - Ailment&amp;amp;Nbr_Of_Codes);
	OUTPUT;

	Code	=	WHICHC('Sicker', of Ailment1 - Ailment&amp;amp;Nbr_Of_Codes);
	OUTPUT;

	Code	=	WHICHC('Sickest', of Ailment1 - Ailment&amp;amp;Nbr_Of_Codes);
	OUTPUT;

	Code	=	WHICHC('Sickly', of Ailment1 - Ailment&amp;amp;Nbr_Of_Codes);
	OUTPUT;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2020 18:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/691044#M210284</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-12T18:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Several row observations to be collapsed (works) and marked for a specific variable (needed)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/691511#M210481</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi Jim,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Brilliant! I note the "of, have&amp;nbsp;made some mistakes with leaving that one out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What if, say I have a HUGE list of "ailments", and I want to keep variables in the array only if they include any of the ailments in that huge list I have? Does WHICHC still work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To relate- in the earlier code, we get as a result a number of columns per row which list the diagnosis codes (after collapse): in_diag_code1 - in_diag_code&amp;amp;nbr_of codes (array). I have a list of specific codes, and want to see whether any of the codes in the array are also in the list of the specific codes I'm interested in.. WHICHC would work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Haven't gotten to that step yet, but nice to follow up on the array and look up, and hopefully avoid mistakes and desperation when I get on it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Oct 2020 12:19:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Several-row-observations-to-be-collapsed-works-and-marked-for-a/m-p/691511#M210481</guid>
      <dc:creator>JJ_211</dc:creator>
      <dc:date>2020-10-14T12:19:59Z</dc:date>
    </item>
  </channel>
</rss>

