<?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: How to create a list of unique values by group? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826393#M326419</link>
    <description>&lt;P&gt;Can you post an excerpt of the dataset you want to iterate over? I can't see the need to move date into macro-variables. Maybe using a hash object, dow-loops or even a format, but certainly no macro variables.&lt;/P&gt;</description>
    <pubDate>Mon, 01 Aug 2022 05:56:01 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2022-08-01T05:56:01Z</dc:date>
    <item>
      <title>How to create a list of unique values by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826229#M326351</link>
      <description>&lt;P&gt;Hello all&lt;/P&gt;&lt;P&gt;I would like to get a list of unique values by group from a dataset. The number of groups and the number of names per group will change week by week.&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;P&gt;ID Name Group&lt;/P&gt;&lt;P&gt;1 M A&lt;/P&gt;&lt;P&gt;2 N A&lt;/P&gt;&lt;P&gt;3 O B&lt;/P&gt;&lt;P&gt;4 P B&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to end up with the following lists:&lt;/P&gt;&lt;P&gt;a_names&lt;/P&gt;&lt;P&gt;M N&lt;/P&gt;&lt;P&gt;b_names&lt;/P&gt;&lt;P&gt;O P&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I was thinking I could do is do a proc sql to get a list of the unique groups, then iterate over that list to get lists of unique names for each group. I am able to get that first list using:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select unique(name) into :group_names separated by ' ' from dataset;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, now I am stuck as to how to iterate over group_names to get a list of unique names for each. Any help is appreciated!&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 21:19:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826229#M326351</guid>
      <dc:creator>meriS</dc:creator>
      <dc:date>2022-07-29T21:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of unique values by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826231#M326353</link>
      <description>Keep going:&lt;BR /&gt;Select unique(name) into :a_names separated by “ “&lt;BR /&gt;From dataset&lt;BR /&gt;Group by group&lt;BR /&gt;Having group = “A”&lt;BR /&gt;;&lt;BR /&gt;If you want this to be dynamic, then&lt;BR /&gt;1) select unique(group) &lt;BR /&gt;Into &amp;amp;groups separated by “ “&lt;BR /&gt;;&lt;BR /&gt;2) iterate over &amp;amp;groups like so:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-perform-a-do-loop-over-a-list-of-values/" target="_blank"&gt;https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-perform-a-do-loop-over-a-list-of-values/&lt;/A&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 29 Jul 2022 21:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826231#M326353</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-07-29T21:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of unique values by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826234#M326355</link>
      <description>&lt;P&gt;I did want the dynamic iteration. That link was very helpful, thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 22:13:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826234#M326355</guid>
      <dc:creator>meriS</dc:creator>
      <dc:date>2022-07-29T22:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of unique values by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826236#M326356</link>
      <description>&lt;P&gt;Why? What are you planing to do with this list later on? For example, if this will be a parameter to a macro call it's better to leave it in a data set and use CALL EXECUTE instead.&lt;/P&gt;
&lt;P&gt;Fully dynamic, no macros and easy to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table distinct_names as
select group, distinct name 
from have
group by group;
quit;
*assume macro name is my_macro and takes the group and name as parameter;
data execute_macro;
set distinct_names;
str = catt('%my_macro(group=', group, ', name=', name, ');');
*this will execute the macro for each group/name;
call execute(str);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jul 2022 22:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826236#M326356</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-29T22:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of unique values by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826239#M326359</link>
      <description>&lt;P&gt;I ultimately want to check if the name is valid given the group name. So, later I want to iterate over a different dataset and, if the group is A, ensure that the name is in M, N. If the name were Q, it would give an error flag.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 22:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826239#M326359</guid>
      <dc:creator>meriS</dc:creator>
      <dc:date>2022-07-29T22:26:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of unique values by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826242#M326362</link>
      <description>&lt;P&gt;I would still recommend keeping it in a table. Then you can easily merge to do the check more easily than looping through macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a merge, if not in Table B then error.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sort data=table1; by group name;

proc sort data=table2; by group name;

data want;
merge table1 (in=a) table2(in=b);
by group name;
if a and not b then status='In Table1 Only';
else if b and not a then status='In Table 2 Only';
else status='Both Tables';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if it's a more complex lookup, custom formats. Macro would be the last option for something like this.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 22:36:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826242#M326362</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-29T22:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of unique values by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826267#M326382</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (ID Name Group) ($);
cards;
1 M A
2 N A
3 O B
4 P B
;

data temp;
do until(last.group);
set have;
by group;
length want $ 200;
want=catx(' ',want,name);
end;
keep group want;
run;

data want;
 set temp;
 _want=want;
 want=cats(group,'_names');output;
 want=_want;output;
 keep want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Jul 2022 10:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826267#M326382</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-07-30T10:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of unique values by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826393#M326419</link>
      <description>&lt;P&gt;Can you post an excerpt of the dataset you want to iterate over? I can't see the need to move date into macro-variables. Maybe using a hash object, dow-loops or even a format, but certainly no macro variables.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2022 05:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-unique-values-by-group/m-p/826393#M326419</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-08-01T05:56:01Z</dc:date>
    </item>
  </channel>
</rss>

