<?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: Keep only unique rows with unique column entries in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958696#M374145</link>
    <description>&lt;P&gt;Transpose and sort to eliminate dups&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tall;
  set have;
  array icode[10] $8 ;
  do i=1 to dim(icode);
    if not missing(icode[i]) then do;
      code=icode[i];
      output;
    end;
  end;
  keep id dcdate code ;
run;

proc sort data=tall nodupkey;
  by id dcdate code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then transpose back if you are worried you will have more observations for ID than the number of ICODE variables you want to create (perhaps you want to make a dataset just for printing) then here is a method you can use to roll the tall database back into wide, but with limit on how wide.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  do i=1 to 2 until(last.dcdate);
    set tall;
    by id dcdate ;
    array icode[2] $8 ;
    icode[i] = code;
  end;
  drop i code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 Feb 2025 19:50:31 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-02-07T19:50:31Z</dc:date>
    <item>
      <title>Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958684#M374135</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Please help.&lt;/P&gt;
&lt;P&gt;I have this dataset:&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID DCDate Direction $ ICode1 $ ICode2 $ ;&lt;BR /&gt;cards;&lt;BR /&gt;1 2 South Q22.x Q22.y &lt;BR /&gt;1 2 South Q22.x . &lt;BR /&gt;1 2 South Q22.y . &lt;BR /&gt;1 2 South Q23.x . &lt;BR /&gt;2 2 South Q22.x Q22.y &lt;BR /&gt;2 2 South Q22.x . &lt;BR /&gt;2 2 South Q22.y . &lt;BR /&gt;2 2 South Q23.x . &lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;I want this output:&lt;/P&gt;
&lt;TABLE border="1" width="100.00000000000001%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;Date&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;ICode1&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Icode2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Q22.x&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Q22.y&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Q23.x&amp;nbsp;&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Q22.x&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Q22.y&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;Q23.x&amp;nbsp;&lt;/TD&gt;
&lt;TD width="33.333333333333336%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 07 Feb 2025 18:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958684#M374135</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2025-02-07T18:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958686#M374137</link>
      <description>&lt;P&gt;Transpose to long, then sort:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long (rename=(dcdate=date));
set have;
array ic {*} icode:;
do i = 1 to dim(ic);
  if not missing (ic{i}
  then do;
    icode = ic{i};
    output;
  enđ;
end;
keep id dcdate icode;
run;

proc sort data=long nodupkey;
by id date icode;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Feb 2025 18:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958686#M374137</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-02-07T18:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958687#M374138</link>
      <description>&lt;P&gt;Sorry,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like the following output&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="20%"&gt;ID&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%"&gt;DCDate&lt;/TD&gt;
&lt;TD width="20%"&gt;Direction&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%"&gt;ICode1&lt;/TD&gt;
&lt;TD width="20%"&gt;Icode2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%"&gt;1&lt;/TD&gt;
&lt;TD width="20%"&gt;2&lt;/TD&gt;
&lt;TD width="20%"&gt;South&lt;/TD&gt;
&lt;TD width="20%"&gt;Q22.x&lt;/TD&gt;
&lt;TD width="20%"&gt;Q22.y&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%"&gt;1&lt;/TD&gt;
&lt;TD width="20%"&gt;2&lt;/TD&gt;
&lt;TD width="20%"&gt;South&lt;/TD&gt;
&lt;TD width="20%"&gt;Q23.x&lt;/TD&gt;
&lt;TD width="20%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%"&gt;2&lt;/TD&gt;
&lt;TD width="20%"&gt;2&lt;/TD&gt;
&lt;TD width="20%"&gt;South&lt;/TD&gt;
&lt;TD width="20%"&gt;Q22.x&lt;/TD&gt;
&lt;TD width="20%"&gt;Q22.y&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%"&gt;2&lt;/TD&gt;
&lt;TD width="20%"&gt;2&lt;/TD&gt;
&lt;TD width="20%"&gt;South&lt;/TD&gt;
&lt;TD width="20%"&gt;Q23.x&lt;/TD&gt;
&lt;TD width="20%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Keeping only the unique ICODEs for each ID group.&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;
&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>Fri, 07 Feb 2025 19:07:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958687#M374138</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2025-02-07T19:07:09Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958689#M374140</link>
      <description>&lt;P&gt;Why do you want only two icode columns?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 19:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958689#M374140</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-02-07T19:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958690#M374141</link>
      <description>&lt;P&gt;ICode1 and ICode2, represent ICode to the nth in the original dataset.&lt;/P&gt;
&lt;P&gt;I could drop the new ICODE in the new dataset I guess.&lt;/P&gt;
&lt;P&gt;In reality, I have up to ICode10 with not all of them being filled.&lt;/P&gt;
&lt;P&gt;I really just want for the group of IDs to capture only the unique ICode values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 19:24:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958690#M374141</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2025-02-07T19:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958693#M374142</link>
      <description>&lt;P&gt;You need to explain more about what you want. Especially since the examples are so simple.&lt;/P&gt;
&lt;P&gt;What is the role of ID and DCDATE?&amp;nbsp; Do you want unique codes per ID?&amp;nbsp; Or per ID and DCDATE combination?&amp;nbsp; In your example there is no difference since DCDATE is a constant.&lt;/P&gt;
&lt;P&gt;Why was DIRECTION included in the example data? It does not appear to be used at all?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the meaning of ICODE1 and ICODE2?&amp;nbsp; What do you want to do if the same code appears in both variables for the same group?&amp;nbsp; Which column should keep the value?&amp;nbsp; Why are you keeping&amp;nbsp;&lt;SPAN&gt;Q22.y in ICODE2 and not in ICODE1?&amp;nbsp; What is the rule for deciding where it belongs?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 19:36:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958693#M374142</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-07T19:36:51Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958695#M374144</link>
      <description>&lt;P&gt;Sorry,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID is a unique Identifier.&lt;/P&gt;
&lt;P&gt;DC date is the same because all codes happened on the same date&lt;/P&gt;
&lt;P&gt;Think of Direction as a person's last name so it would be the same but unique to that person, as ID is unique to that person. What I really want to make sure of is that even if someone has the same last name they won't have the same ID since ID is unique and not names.&lt;/P&gt;
&lt;P&gt;If in the group of the person's ID's there is a unique ICode then I want to capture it. Otherwise I don't wan it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 19:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958695#M374144</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2025-02-07T19:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958696#M374145</link>
      <description>&lt;P&gt;Transpose and sort to eliminate dups&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tall;
  set have;
  array icode[10] $8 ;
  do i=1 to dim(icode);
    if not missing(icode[i]) then do;
      code=icode[i];
      output;
    end;
  end;
  keep id dcdate code ;
run;

proc sort data=tall nodupkey;
  by id dcdate code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then transpose back if you are worried you will have more observations for ID than the number of ICODE variables you want to create (perhaps you want to make a dataset just for printing) then here is a method you can use to roll the tall database back into wide, but with limit on how wide.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  do i=1 to 2 until(last.dcdate);
    set tall;
    by id dcdate ;
    array icode[2] $8 ;
    icode[i] = code;
  end;
  drop i code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 19:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958696#M374145</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-07T19:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958705#M374148</link>
      <description>&lt;P&gt;You could also do it with hash objects in a single data step (assuming data already sorted by ID).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id dcdate;
  array icode[2] $8 ;
  if _n_=1 then do;
    declare hash h();
    h.definekey('code');
    h.definedata('code');
    h.definedone();
  end;
  do i=1 to dim(icode);
    code = icode[i];
    if not missing(code) then rc=h.add();
  end;
  if last.id then do;
    call missing(of code icode[*]);
    declare hiter iter('h');
    rc = iter.first();
    do until (rc);
      do i=1 to dim(icode) until (rc);
        icode[i]=code;
        rc=iter.next();
      end;
      output;
      call missing(of icode[*]);
    end;
    rc = iter.delete();
    rc = h.clear();
  end;
  drop rc i code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 20:37:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958705#M374148</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-07T20:37:25Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only unique rows with unique column entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958710#M374150</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
	set have; 
	by ID DCDate Direction ICode1 descending icode2;
	retain icode;
	if first.id then icode=icode2;
	if not first.icode1 or icode1=icode then delete; 
	drop icode; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Feb 2025 20:54:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-unique-rows-with-unique-column-entries/m-p/958710#M374150</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2025-02-07T20:54:51Z</dc:date>
    </item>
  </channel>
</rss>

