<?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 Dynamic  process to union all data sets with distinct values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670075#M201106</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;There is a program that I need to run every few weeks.&lt;/P&gt;
&lt;P&gt;This program need to use different source data sets (By business decision).&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;Let's say that If I run the program today then I need to use data sets: t2001,t2002,t2003.&lt;/P&gt;
&lt;P&gt;In the program there is a step that check distinct values for each data source and then stack the values (Union) and delete duplicates values (Union all).&lt;/P&gt;
&lt;P&gt;My question:&lt;/P&gt;
&lt;P&gt;Is there a shorter way to perform the dynamic process of creating data set Newtbl?&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;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t2001;
input ToCustomer FromCustomer Y
cards;
111 999 10
222 888 20
333 888 30
;
Run;

Data t2002;
input ToCustomer FromCustomer Y
cards;
111 999 10
333 888 30
444 767 40
;
Run;

Data t2003;
input ToCustomer FromCustomer Y
cards;
111 999 10
555 345 30
;
Run;
&lt;BR /&gt;/*Non dynamic way to create data set NewTbl*/
PROC SQL;
Create table NewTbl
 SELECT distinct ToCustomer, FromCustomer
 FROM t2001
Union All
 SELECT distinct ToCustomer, FromCustomer
 FROM t2002
Union All
 SELECT distinct ToCustomer, FromCustomer
 FROM t2003
;
QUIT;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;/*dynamic way to create data set NewTbl*/&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%macro RRR1 (YYMM);&lt;BR /&gt;create table distinct_t_&amp;amp;YYMM. as&lt;BR /&gt;select distinct ToCustomer, FromCustomer&lt;BR /&gt; FROM t&amp;amp;YYMM.&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;%mend RRR1;&lt;BR /&gt;&lt;BR /&gt;%let vector=2001+2002+2003;&lt;BR /&gt;%let k = %sysfunc(countw(&amp;amp;vector));&lt;BR /&gt;&lt;BR /&gt;%macro RUN_RRR; &lt;BR /&gt;%do j=1 %to &amp;amp;k.;&lt;BR /&gt;%let YYMM=%scan(&amp;amp;vector.,&amp;amp;j.,+);&lt;BR /&gt;%RRR(&amp;amp;YYMM.);&lt;BR /&gt;%end;&lt;BR /&gt;%mend RUN_RRR;&lt;BR /&gt;%RUN_RRR;&lt;BR /&gt;&lt;BR /&gt;data NewTbl_;&lt;BR /&gt;SET distinct_t_:;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=NewTbl_  nodupkey;&lt;BR /&gt;by ToCustomer FromCustomer;&lt;BR /&gt;Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jul 2020 07:32:49 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2020-07-17T07:32:49Z</dc:date>
    <item>
      <title>Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670075#M201106</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;There is a program that I need to run every few weeks.&lt;/P&gt;
&lt;P&gt;This program need to use different source data sets (By business decision).&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;Let's say that If I run the program today then I need to use data sets: t2001,t2002,t2003.&lt;/P&gt;
&lt;P&gt;In the program there is a step that check distinct values for each data source and then stack the values (Union) and delete duplicates values (Union all).&lt;/P&gt;
&lt;P&gt;My question:&lt;/P&gt;
&lt;P&gt;Is there a shorter way to perform the dynamic process of creating data set Newtbl?&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;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t2001;
input ToCustomer FromCustomer Y
cards;
111 999 10
222 888 20
333 888 30
;
Run;

Data t2002;
input ToCustomer FromCustomer Y
cards;
111 999 10
333 888 30
444 767 40
;
Run;

Data t2003;
input ToCustomer FromCustomer Y
cards;
111 999 10
555 345 30
;
Run;
&lt;BR /&gt;/*Non dynamic way to create data set NewTbl*/
PROC SQL;
Create table NewTbl
 SELECT distinct ToCustomer, FromCustomer
 FROM t2001
Union All
 SELECT distinct ToCustomer, FromCustomer
 FROM t2002
Union All
 SELECT distinct ToCustomer, FromCustomer
 FROM t2003
;
QUIT;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;/*dynamic way to create data set NewTbl*/&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%macro RRR1 (YYMM);&lt;BR /&gt;create table distinct_t_&amp;amp;YYMM. as&lt;BR /&gt;select distinct ToCustomer, FromCustomer&lt;BR /&gt; FROM t&amp;amp;YYMM.&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;%mend RRR1;&lt;BR /&gt;&lt;BR /&gt;%let vector=2001+2002+2003;&lt;BR /&gt;%let k = %sysfunc(countw(&amp;amp;vector));&lt;BR /&gt;&lt;BR /&gt;%macro RUN_RRR; &lt;BR /&gt;%do j=1 %to &amp;amp;k.;&lt;BR /&gt;%let YYMM=%scan(&amp;amp;vector.,&amp;amp;j.,+);&lt;BR /&gt;%RRR(&amp;amp;YYMM.);&lt;BR /&gt;%end;&lt;BR /&gt;%mend RUN_RRR;&lt;BR /&gt;%RUN_RRR;&lt;BR /&gt;&lt;BR /&gt;data NewTbl_;&lt;BR /&gt;SET distinct_t_:;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=NewTbl_  nodupkey;&lt;BR /&gt;by ToCustomer FromCustomer;&lt;BR /&gt;Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jul 2020 07:32:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670075#M201106</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-17T07:32:49Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670080#M201110</link>
      <description>&lt;P&gt;Try next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all_t / view=all_t;
   set t2001 t2003 t2003;
  /* OR set t20: ;  &amp;lt;&amp;lt;&amp;lt; for concatenating all t20xx datasets */
run;
proc sort data=all_t out=final nodupkey;
  by ToCustomer, FromCustomer;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jul 2020 20:13:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670080#M201110</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-17T20:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670093#M201117</link>
      <description>&lt;P&gt;If you want to stick with a SQL Union then below should do the job. Using a SQL Union would be advantageous if your source data resides in a data base; or also if you can't be certain that the variable lengths in your source tables are always the same.&lt;/P&gt;
&lt;P&gt;In regards of the UNION operator: Without the keyword ALL a Union will dedup the result set so for what you want don't use a combination of Distinct/Union All but just use a Union. That should give you the same result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* source data */
Data t2001;
  input ToCustomer FromCustomer Y;
  cards;
111 999 10
222 888 20
333 888 30
;

Data t2002;
  input ToCustomer FromCustomer Y;
  cards;
111 999 10
333 888 30
444 767 40
;

Data t2003;
  input ToCustomer FromCustomer Y;
  cards;
111 999 10
555 345 30
;

/* macro definition */
%macro createWant(sources, target);
  proc sql;
    create table &amp;amp;target as
      select *
        from %scan(&amp;amp;sources,1)

      %do i=2 %to %sysfunc(countw(&amp;amp;sources,%str( )));
        union
        select *
          from %scan(&amp;amp;sources,&amp;amp;i,%str( ))
      %end;
    ;
  quit;
%mend;

/* call macro to create output table using list of defined source tables */
options mprint;
%createWant(t2001 work.t2002 t2003,want);
options nomprint;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jul 2020 10:36:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670093#M201117</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-17T10:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670198#M201148</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;What is the meaning of this code that you wrote please?&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;/ view=akk_t;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jul 2020 19:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670198#M201148</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-17T19:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670199#M201149</link>
      <description>&lt;P&gt;Thanks, you didn't use distinct....&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jul 2020 19:52:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670199#M201149</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-17T19:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670214#M201151</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;What is the meaning of this code that you wrote please?&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;/ view=akk_t;&lt;/LI-CODE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A view is a sas object that tells sas what to do without doing it immediately.&lt;/P&gt;
&lt;P&gt;In this case it tells sas to concatenate the input datasets to create one input stream to the sort procedure just before running the sort. &lt;STRONG&gt;Mainly, it saves run time ans disk space.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Note: Concatenation of datasets means read all observation of the n datasets and write them to one output dataset. By using a view, the observation are read &lt;STRONG&gt;once&lt;/STRONG&gt; directly into the sort procedure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create a view by sas step as shown up or even by a sql by:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ...;
       create VIEW &amp;lt;view_name&amp;gt;  as
        ......
; quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jul 2020 20:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670214#M201151</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-17T20:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670216#M201152</link>
      <description>&lt;P&gt;I have used SORT ... NODUPKEY which is equivalent to SQL .. DISTINCT&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in your case.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jul 2020 20:27:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670216#M201152</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-17T20:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670245#M201163</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, you didn't use distinct....&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's because a Distinct is not required. The UNION operator without the ALL keyword will only return unique rows. May be have a read &lt;A href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n0vo2lglyrnexwn14emi8m0jqvrj.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;A Select DISTINCT * together with UNION ALL would return distinct rows per source table but would not dedup rows from different source tables. Looking at your sample source data I felt that's not what you want.&lt;/P&gt;
&lt;P&gt;The Proc Sort Nodupkey approach will also only return unique rows and though give the same result than the UNION (without Distinct and ALL keywords).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 02:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670245#M201163</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-18T02:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670367#M201221</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;So as I understand this statement&amp;nbsp; &amp;nbsp;create a view instead of data set and advantage is program efficiency&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data all_t / view=all_t;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 19:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670367#M201221</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-18T19:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670368#M201222</link>
      <description>What is the difference between running the code:&lt;BR /&gt;options mprint;&lt;BR /&gt;%createWant(t2001 work.t2002 t2003,want);&lt;BR /&gt;options nomprint;&lt;BR /&gt;&lt;BR /&gt;and running only the code:&lt;BR /&gt;%createWant(t2001 work.t2002 t2003,want);&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 18 Jul 2020 19:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670368#M201222</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-18T19:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic  process to union all data sets with distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670378#M201230</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;What is the difference between running the code:&lt;BR /&gt;options mprint;&lt;BR /&gt;%createWant(t2001 work.t2002 t2003,want);&lt;BR /&gt;options nomprint;&lt;BR /&gt;&lt;BR /&gt;and running only the code:&lt;BR /&gt;%createWant(t2001 work.t2002 t2003,want);&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Best answer you will got if you run:&lt;/P&gt;
&lt;P&gt;First:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;
%createWant(t2001 work.t2002 t2003,want);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then run and compare logs:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options nomprint;
%createWant(t2001 work.t2002 t2003,want);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Jul 2020 20:25:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-process-to-union-all-data-sets-with-distinct-values/m-p/670378#M201230</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-18T20:25:11Z</dc:date>
    </item>
  </channel>
</rss>

