<?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 Adding non-duplicate value of a row to a new column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685858#M208046</link>
    <description>&lt;P&gt;Using SAS 9.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to get all codes unique by ID and Date into the same row. I have data below to show but basically an ID can have multiple ICD9, ICD10 and/or CPT codes per date. I would like an efficient way to transform the data from long to wide but I need it only to be wide where the ID and Date are the same. As I show below, if there are 2 rows for an ID with the same Date I would like to take the different ICD9/ICD10/CPT code from the row below and move it into a new column so that each unique ID and Date has its own row with all ICD9, ICD10 and CPT listed in wide format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following data:&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code Chg_Procedure_Code;&lt;BR /&gt;datalines;&lt;BR /&gt;1 09222020 1111 2222 3333&lt;BR /&gt;1 09222020 1111 2222 4444&lt;/P&gt;
&lt;P&gt;2 09222020 1111 2222 3333&lt;/P&gt;
&lt;P&gt;2 08222020 1111 2222 4444&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want my data to look like this (As you can see if the ID is the same but the Date is different I do not want the data to transform to wide):&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code Chg_Procedure_Code Chg_Procedure_Code2;&lt;BR /&gt;datalines;&lt;BR /&gt;1 09222020 1111 2222 3333 4444&lt;/P&gt;
&lt;P&gt;2 09222020 1111 2222 3333 .&lt;BR /&gt;2 08012000 1111 2222 4444 .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any thoughts of place to start with this would be helpful. Thank you&lt;/P&gt;</description>
    <pubDate>Tue, 22 Sep 2020 20:01:01 GMT</pubDate>
    <dc:creator>GS2</dc:creator>
    <dc:date>2020-09-22T20:01:01Z</dc:date>
    <item>
      <title>Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685858#M208046</link>
      <description>&lt;P&gt;Using SAS 9.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to get all codes unique by ID and Date into the same row. I have data below to show but basically an ID can have multiple ICD9, ICD10 and/or CPT codes per date. I would like an efficient way to transform the data from long to wide but I need it only to be wide where the ID and Date are the same. As I show below, if there are 2 rows for an ID with the same Date I would like to take the different ICD9/ICD10/CPT code from the row below and move it into a new column so that each unique ID and Date has its own row with all ICD9, ICD10 and CPT listed in wide format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following data:&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code Chg_Procedure_Code;&lt;BR /&gt;datalines;&lt;BR /&gt;1 09222020 1111 2222 3333&lt;BR /&gt;1 09222020 1111 2222 4444&lt;/P&gt;
&lt;P&gt;2 09222020 1111 2222 3333&lt;/P&gt;
&lt;P&gt;2 08222020 1111 2222 4444&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want my data to look like this (As you can see if the ID is the same but the Date is different I do not want the data to transform to wide):&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code Chg_Procedure_Code Chg_Procedure_Code2;&lt;BR /&gt;datalines;&lt;BR /&gt;1 09222020 1111 2222 3333 4444&lt;/P&gt;
&lt;P&gt;2 09222020 1111 2222 3333 .&lt;BR /&gt;2 08012000 1111 2222 4444 .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any thoughts of place to start with this would be helpful. Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 22 Sep 2020 20:01:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685858#M208046</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-09-22T20:01:01Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685877#M208051</link>
      <description>&lt;P&gt;My thoughts have been to use the following code but I am not capturing what I would like but hopefully the code will give an idea of what I am thinking will work. Thank want&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;set all;&lt;BR /&gt;array nine icd9_1-icd9_3;&lt;BR /&gt;array ten icd10_1-icd10_4;&lt;BR /&gt;array cpt cpt1-cpt7;&lt;BR /&gt;do i = 1 to 3;*3 is the max number of different codes;&lt;BR /&gt;nine{i} = lag(Chg_Pri_Diagnosis_Code);&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;do i = 1 to 4;*4 is the max number of different codes;&lt;BR /&gt;ten{i} = lag(Chg_Pri_ICD10_Diagnosis_Code);&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;do i = 1 to 7;*7 is the max number of different codes;&lt;BR /&gt;cpt{i} = lag(Chg_Procedure_Code);&lt;BR /&gt;end;&lt;BR /&gt;b&lt;/P&gt;</description>
      <pubDate>Tue, 22 Sep 2020 20:48:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685877#M208051</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-09-22T20:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685887#M208053</link>
      <description>&lt;P&gt;Are there &lt;STRONG&gt;any&lt;/STRONG&gt; other variables in your have data set other than the ones shown? If so, do they appear in the result?&lt;/P&gt;</description>
      <pubDate>Tue, 22 Sep 2020 21:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685887#M208053</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-22T21:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685946#M208078</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194348"&gt;@GS2&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's one approach; see code below, followed by the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code allows one to have any number of&amp;nbsp;Chg_Procedure_Code columns in the wide version of the dataset.&amp;nbsp; One simply sets the Nbr_of_Codes macro variable to whatever the maximum number of Chg_Procedure_Code columns you desire.&amp;nbsp; In this example, I've modified the data to fit three&amp;nbsp;Chg_Procedure_Code columns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice also that I've introduced a duplicate for ID 2 on Sept. 22, 6666.&amp;nbsp; In our results we can see that there is no duplicate 6666.&amp;nbsp; The Multidata:'N' in conjunction with how the keys are defined removes duplicates.&amp;nbsp; One could also remove the same duplicates in the Sort step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This wasn't a specified requirement, but the&amp;nbsp;Chg_Procedure_Code columns will have the values in sort order.&amp;nbsp; I.e. for values 1111, 2222, and 3333, the first&amp;nbsp;Chg_Procedure_Code column will have 1111, the second 2222, and the third 3333.&amp;nbsp; This is accomplished by using Ordered:'A' (ascending).&amp;nbsp; If you don't want this behavior, you can just remove this parameter.&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	Have;
	FORMAT	ID;
	FORMAT	Date	YYMMDDD10.;
	INFILE	Datalines;
	input 
		ID								$
		Date							:	ANYDTDTE8.
		Chg_Pri_Diagnosis_Code			$ 
		Chg_Pri_ICD10_Diagnosis_Code 	$
		Chg_Procedure_Code				$
		;
Datalines;
1 09222020 1111 2222 3333
1 09222020 1111 2222 4444
2 09222020 1111 2222 1111
2 09222020 1111 2222 6666
2 09222020 1111 2222 6666
2 08222020 1111 2222 7777
7 07222020 1111 2222 6666
7 07222020 1111 2222 7777
7 07222020 1111 2222 8888
;
RUN;

PROC	SORT	DATA=Have;
	BY	ID Date;
RUN;

DATA	Want;
	DROP	_:;
	DROP	Chg_Procedure_Code;

	SET	Have;
		BY	ID	Date;

	ARRAY	Chg_Procedure_Codes	[*]	$8	Chg_Procedure_Code1 - Chg_Procedure_Code&amp;amp;Nbr_of_Codes;

	IF	_N_					=	1	THEN
		DO;
			DECLARE	HASH	H_Pat_Data(ORDERED: 'A', MULTIDATA:'N');
							H_Pat_Data.DEFINEKEY ('ID', 'Date', 'Chg_Procedure_Code');
							H_Pat_Data.DEFINEDONE();
			DECLARE	HITER	HI_Pat_Data('H_Pat_Data');
		END;

	_RC						=	H_Pat_Data.ADD();

	IF	LAST.Date	THEN
		DO;
			_RC				=	HI_Pat_Data.FIRST();
			DO	_i			=	1	TO	&amp;amp;Nbr_of_Codes;
				Chg_Procedure_Codes[_i]	=	Chg_Procedure_Code;
				_RC			=	HI_Pat_Data.NEXT();
				IF	_RC		&amp;gt;	0	THEN
					DO;
						_i	=	&amp;amp;Nbr_of_Codes;
						_RC	=	H_Pat_Data.CLEAR();
					END;
			END;
		END;
	ELSE
		DO;
			DELETE;
		END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1600836277285.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49698i78AF5194F54A972C/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1600836277285.png" alt="jimbarbour_0-1600836277285.png" /&gt;&lt;/span&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 04:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685946#M208078</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-23T04:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685956#M208082</link>
      <description>&lt;P&gt;Having data in wide format can cause difficulties when processing the data. The first difficulty is already shown in the solution provided by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;: you need to know how many variables for the Chg_Procedure_Codes are required. Later on you will have to write checks talking into account that some of the Chg_Procedure_Code-variable are missing. So: are you sure, that the wide-format serves the whatever you are doing with the data afterwards better than the long-format?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something else: What do you expect from the following data (don't know if the combination of values is possible, at all):&lt;/P&gt;
&lt;PRE&gt;ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code Chg_Procedure_Code
1 09222020 1111 2222 3333
1 09222020 4242 2222 4444&lt;/PRE&gt;
&lt;P&gt;From your description those obs should be combined, forming something like???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 05:15:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685956#M208082</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-09-23T05:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685978#M208094</link>
      <description>&lt;P&gt;If I understand you correctly, you want to do this with all the 3 variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first requirement is to get the number of elements for each variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select 
    max(n1),
    max(n2),
    max(n3)
  into :n1 trimmed,:n2 trimmed,:n3 trimmed
  from(
    select 
      count(distinct Chg_Pri_Diagnosis_Code) as n1,
      count(distinct Chg_Pri_ICD10_Diagnosis_Code) as n2,
      count(distinct Chg_Procedure_Code) as n3
    from have
    group by ID,Date
    )
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then, define a macro to get the data into an array, and use that in a DoW loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro make_array(var,n);
  %if &amp;amp;n&amp;gt;1 %then %do;
    array _&amp;amp;var(*) 8 &amp;amp;var.1-&amp;amp;var.&amp;amp;n;
    if not whichn(&amp;amp;var,of _&amp;amp;var(*)) then
      _&amp;amp;var(n(of _&amp;amp;var(*))+1)=&amp;amp;var;
    drop &amp;amp;var;
    %end;
%mend;
  
options mprint;
data want;
  do until(last.Date);
    set have;  
    by ID Date notsorted;
    %make_array(Chg_Pri_Diagnosis_Code,&amp;amp;n1);
    %make_array(Chg_Pri_ICD10_Diagnosis_Code,&amp;amp;n2);
    %make_array(Chg_Procedure_Code,&amp;amp;n3);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You data was not sorted correctly by DATE, so I used the NOTSORTED option. You may want to replace that with a PROC SORT instead.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 08:57:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/685978#M208094</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-09-23T08:57:21Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686020#M208103</link>
      <description>&lt;P&gt;There are other variables but this is just building a data set for review so they are brought over but not part of what is wanted. Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 12:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686020#M208103</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-09-23T12:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686026#M208105</link>
      <description>&lt;P&gt;So I believe this method will work based on what I am seeing; However, I did make a mistake with the data.&amp;nbsp;Chg_Pri_ICD10_Diagnosis_Code is actually a character variable, there is a letter in a couple observations. Sorry about that. Is it possible to modify the code to incorporate if 1 variable is a character? Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 12:52:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686026#M208105</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-09-23T12:52:33Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686048#M208114</link>
      <description>&lt;P&gt;Yes, wide is how the data needs to be structured.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There may be combinations but this is more gathering how many potential codes are available.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686048#M208114</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-09-23T14:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686062#M208121</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194348"&gt;@GS2&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code I have written will accommodate character.&amp;nbsp; I usually just code everything character unless I intend to do calculations or it's a date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;made a good point that it might be a bit of a pain to have to manually count the number of unique procedure codes.&amp;nbsp; That is easily solved by adding a NODUPKEY parameter to the sort and then inserting a little Data step like the below.&amp;nbsp; The results are I believe what you want and there's no manual step of counting the number of unique procedure codes.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Have;
	FORMAT	ID;
	FORMAT	Date	YYMMDDD10.;
	INFILE	Datalines;
	input 
		ID								$
		Date							:	ANYDTDTE8.
		Chg_Pri_Diagnosis_Code			$ 
		Chg_Pri_ICD10_Diagnosis_Code 	$
		Chg_Procedure_Code				$
		;
Datalines;
1 09222020 1111 2222 3333
1 09222020 1111 2222 4444
2 09222020 1111 2222 1111
2 09222020 1111 2222 6666
2 09222020 1111 2222 6666
2 08222020 1111 2222 7777
7 07222020 1111 2222 6666
7 07222020 1111 2222 7777
7 07222020 1111 2222 8888
;
RUN;

PROC	SORT	DATA=Have	NODUPKEY;
	BY	ID Date	Chg_Procedure_Code;
RUN;

DATA	_NULL_;
	RETAIN	Max_Procedure_Code_Cnt		0;

	IF	_End_of_Data												THEN
		CALL	SYMPUTX('Nbr_of_Codes', Max_Procedure_Code_Cnt, 'G');

	SET	Have	END						=	_End_of_Data;
		BY	ID Date	Chg_Procedure_Code;

	Procedure_Code_Cnt					+	1;

	IF	LAST.Chg_Procedure_Code										THEN
		IF	Procedure_Code_Cnt			&amp;gt;	Max_Procedure_Code_Cnt	THEN
			DO;
				Max_Procedure_Code_Cnt	=	Procedure_Code_Cnt;
				Procedure_Code_Cnt		=	0;
			END;
RUN;

%PUT	&amp;amp;Nte1  &amp;amp;=Nbr_of_Codes;

DATA	Want;
	DROP	_:;
	DROP	Chg_Procedure_Code;

	SET	Have;
		BY	ID	Date;

	ARRAY	Chg_Procedure_Codes	[*]	$8	Chg_Procedure_Code1 - Chg_Procedure_Code&amp;amp;Nbr_of_Codes;

	IF	_N_					=	1	THEN
		DO;
			DECLARE	HASH	H_Pat_Data(ORDERED: 'A', MULTIDATA:'N');
							H_Pat_Data.DEFINEKEY ('ID', 'Date', 'Chg_Procedure_Code');
							H_Pat_Data.DEFINEDONE();
			DECLARE	HITER	HI_Pat_Data('H_Pat_Data');
		END;

	_RC						=	H_Pat_Data.ADD();

	IF	LAST.Date	THEN
		DO;
			_RC				=	HI_Pat_Data.FIRST();
			DO	_i			=	1	TO	&amp;amp;Nbr_of_Codes;
				Chg_Procedure_Codes[_i]	=	Chg_Procedure_Code;
				_RC			=	HI_Pat_Data.NEXT();
				IF	_RC		&amp;gt;	0	THEN
					DO;
						_i	=	&amp;amp;Nbr_of_Codes;
						_RC	=	H_Pat_Data.CLEAR();
					END;
			END;
		END;
	ELSE
		DO;
			DELETE;
		END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:36:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686062#M208121</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-09-23T14:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686241#M208200</link>
      <description>&lt;P&gt;If it helps&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code Chg_Procedure_Code;&lt;BR /&gt;datalines;&lt;BR /&gt;1 09222020 1111 2222 3333&lt;BR /&gt;1 09222020 1111 2222 4444&lt;BR /&gt;2 09222020 1111 2222 3333&lt;BR /&gt;2 08222020 1111 2222 4444&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;Proc sort data=have out=have;&lt;BR /&gt;By ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc transpose data=have out=want (drop=_NAME_) prefix=Chg_Procedure_Code;&lt;BR /&gt;By ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code;&lt;BR /&gt;var Chg_Procedure_Code;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;The output&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Date Chg_Pri_Diagnosis_Code Chg_Pri_ICD10_Diagnosis_Code Chg_Procedure_Code1 Chg_Procedure_Code2&lt;BR /&gt;1 9222020 1111 2222 3333 4444&lt;BR /&gt;2 8222020 1111 2222 4444 .&lt;BR /&gt;2 9222020 1111 2222 3333 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 22:27:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686241#M208200</guid>
      <dc:creator>SK_11</dc:creator>
      <dc:date>2020-09-23T22:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: Adding non-duplicate value of a row to a new column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686324#M208227</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194348"&gt;@GS2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;So I believe this method will work based on what I am seeing; However, I did make a mistake with the data.&amp;nbsp;Chg_Pri_ICD10_Diagnosis_Code is actually a character variable, there is a letter in a couple observations. Sorry about that. Is it possible to modify the code to incorporate if 1 variable is a character? Thank you&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That makes things a bit more complicated. I changed the macro to find the variable type and length of the variable, and define the array (and the WHICH function to call) using that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro make_array(indata,var,n,onerror=abort cancel);
  %if &amp;amp;n&amp;gt;1 %then %do;
    %local dsid varnum vartype vardef whichfunc;
    %let dsid=%sysfunc(open(&amp;amp;indata));
    %if &amp;amp;dsid=0 %then %do;
      %put %qsysfunc(sysmsg());
      &amp;amp;onerror;
      %return;
      %end;
    %let varnum=%sysfunc(varnum(&amp;amp;dsid,&amp;amp;var));
    %if &amp;amp;varnum=0 %then %do;
      %put Variable &amp;amp;var not found on &amp;amp;indata;
      &amp;amp;onerror;
      %return;
      %end;
    %let vartype=%sysfunc(vartype(&amp;amp;dsid,&amp;amp;varnum));
    %if &amp;amp;vartype=N %then %do;
      %let vardef=8;
      %let whichfunc=whichn;
      %end;
    %else %do;
      %let vardef=$%sysfunc(varlen(&amp;amp;dsid,&amp;amp;varnum));
      %let whichfunc=whichc;
      %end;
    %let dsid=%sysfunc(close(&amp;amp;dsid));
    array _&amp;amp;var(*) &amp;amp;vardef &amp;amp;var.1-&amp;amp;var.&amp;amp;n;
    if not &amp;amp;whichfunc(&amp;amp;var,of _&amp;amp;var(*)) then
      _&amp;amp;var(&amp;amp;n-cmiss(of _&amp;amp;var(*))+1)=&amp;amp;var;
    drop &amp;amp;var;
    %end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The macro now has two more parameters: First parameter is the input data set, the last parameter is what to do if there is an error (e.g. the input data does not exist, or the variable is not on the input data set). I set the default to ABORT CANCEL, which will exit the submit block, but not close SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will now have to call the macro with one more parameter, namely the name of the input:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;
data want;
  do until(last.Date);
    set have;  
    by ID Date notsorted;
    %make_array(have,Chg_Pri_Diagnosis_Code,&amp;amp;n1);
    %make_array(have,Chg_Pri_ICD10_Diagnosis_Code,&amp;amp;n2);
    %make_array(have,Chg_Procedure_Code,&amp;amp;n3);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SQL part is the same as my first answer.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 10:12:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-non-duplicate-value-of-a-row-to-a-new-column/m-p/686324#M208227</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-09-24T10:12:32Z</dc:date>
    </item>
  </channel>
</rss>

