<?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: Juletip #2 - The Julekalender %Union; in SAS Community Nordic</title>
    <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418114#M130</link>
    <description>&lt;P&gt;Hi Emil&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Great tip - thanks. A small improvement to the macro could be to use the WORK dir to save the intermediate file combined.sas instead of the hardcoded c:\temp. Thus, you would not experience problems with filesystems and access rights (and perhaps multiple users running on the same server). A slightly changed macro becomes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="Courier New" size="3"&gt;%macro union(dsn1=,     /*Name of the first data set    */
             dsn2=,     /*Name of the second data set   */&lt;BR /&gt;             out=       /*Name of combined data set     */);
  
   proc contents data=&amp;amp;dsn1 noprint
      out=out1(keep=name type length where=(type=2));
   proc contents data=&amp;amp;dsn2 noprint
      out=out2(keep=name type length where=(type=2));
   run;
   %let work_path=%sysfunc(pathname(work));
   data _null_;
      file "&amp;amp;work_path\combined.sas";
      merge out1 out2(rename=(length=length2)) end=last;
      by name;
      if _n_ = 1 then put "Data &amp;amp;out;";
      l = max(length,length2);
      put "   length " name " $ " l 5. ";";
      if last then do;
         put "   set &amp;amp;dsn1 &amp;amp;dsn2;";
         put "run;";
      end;
   run;
   %include "&amp;amp;work_path\combined.sas";
%mend union;
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Det bår' dæjli'&lt;/P&gt;</description>
    <pubDate>Tue, 05 Dec 2017 09:44:26 GMT</pubDate>
    <dc:creator>CKjeldsen</dc:creator>
    <dc:date>2017-12-05T09:44:26Z</dc:date>
    <item>
      <title>Juletip #2 - The Julekalender %Union;</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418106#M128</link>
      <description>&lt;P&gt;Do you know the feeling when you want to append a table, and it’s an ad-hoc job which you just don’t want to spend to get done fast, but you end up spending way too much time on it, because you must match the length of the variables?&lt;/P&gt;
&lt;P&gt;Let’s say, you have the following two tables:&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 FamilienSand;
	length navn $13 type $8 jobbeskrivelse $14 interesser $35;
	infile datalines dsd;
	input navn$ type$ jobbeskrivelse$ interesser$;
	datalines;
	Oluf Sand, Menneske, Kartoffelavler, Pibe
	Gertrud Sand, Menneske, Kartoffelavler, 'Telefon, Food-processor og Julepynt'
	Emil Sand, Hund, Hund, Radioavisen
;run;

data Nisser;
	length navn $8 type $5 jobbeskrivelse $52 interesser $23;
	infile datalines dsd;
	input navn$ type$ jobbeskrivelse$ interesser$;
	datalines;
	Fritz, Nisse, Uddeler opgaver, Kontrabas
	Hansi, Nisse, The one with the biggest tænder and the grimmest tøj, The kartoffelaver woman
	Günther, Nisse, Snitternisse, Guitar og Mundharmonika
;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;Both tables contain the same variables, but the lengths are different. Proc Append gives you an error if you don’t add the “Force” option, and even if you do then your variables in one data set will be truncated according to the base-data set.&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;proc append data=FamilienSand base=Nisser;run; *Error;
proc append data=FamilienSand base=Nisser force;run; *Truncates the FamilienSand – data set;&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;You will now have to correct the length-statement in both tables to make sure they match. You might think:” Do I really have to do all this work for this little ad-hoc job?”. All you wanted was, after all to just append the data while keeping all the observations whole.&lt;/P&gt;
&lt;P&gt;Luckily for you there is an alternative. The macro: %Union comes for the rescue. It does exactly what you want it to do: it merges two data sets while always choosing the longest length-definition for the variables in question. It looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro union(dsn1=,     /*Name of the first data set    */
             dsn2=,     /*Name of the second data set   */
             out=       /*Name of combined data set     */);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, your job can be solved as simple as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Union(dsn1=FamilienSand,dsn2=Nisser,out=Heltene);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It doesn’t get much simpler than this. And actually, it can also handle the situation, where one of your tables has a variable which the other one doesn’t. Let’s say you have a third table:&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 KockSokkerOgSko;
	length navn type $13 jobbeskrivelse $38 interesser $11;
	infile datalines dsd;
	input navn $ type $ jobbeskrivelse$ svaghed$ interesser$;
	datalines;
	Benny Jensen, Nåsåer, Rejsende handelsmand i sko og strømper, Snaps, Gamle bøger
;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;This one contains the extra variable: ”Svaghed”, which doesn’t occur in the two other tables. Well, that is no problem, you just use your %Union macro as usual:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Union(dsn1=Heltene,dsn2=KockSokkerOgSko,out=SamletJulekalender);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final result is what you can see here, and it is exactly as you want it to be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/16987i56B29CA536A3B701/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot.png" alt="Screenshot.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code for the macro is shown below, together with the examples in this Juletip.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro union(dsn1=,     /*Name of the first data set    */
             dsn2=,     /*Name of the second data set   */
             out=       /*Name of combined data set     */);
  
   proc contents data=&amp;amp;dsn1 noprint
      out=out1(keep=name type length where=(type=2));
   proc contents data=&amp;amp;dsn2 noprint
      out=out2(keep=name type length where=(type=2));
   run;
   data _null_;
      file "C:\temp\combined.sas";
      merge out1 out2(rename=(length=length2)) end=last;
      by name;
      if _n_ = 1 then put "Data &amp;amp;out;";
      l = max(length,length2);
      put "   length " name " $ " l 5. ";";
      if last then do;
         put "   set &amp;amp;dsn1 &amp;amp;dsn2;";
         put "run;";
      end;
   run;
   %include "C:\temp\combined.sas";
%mend union;


data FamilienSand;
	length navn $13 type $8 jobbeskrivelse $14 interesser $35;
	infile datalines dsd;
	input navn$ type$ jobbeskrivelse$ interesser$;
	datalines;
	Oluf Sand, Menneske, Kartoffelavler, Pibe
	Gertrud Sand, Menneske, Kartoffelavler, 'Telefon, Food-processor og Julepynt'
	Emil Sand, Hund, Hund, Radioavisen
;run;

data Nisser;
	length navn $8 type $5 jobbeskrivelse $52 interesser $23;
	infile datalines dsd;
	input navn$ type$ jobbeskrivelse$ interesser$;
	datalines;
	Fritz, Nisse, Uddeler opgaver, Kontrabas
	Hansi, Nisse, The one with the biggest tænder and the grimmest tøj, The kartoffelaver woman
	Günther, Nisse, Snitternisse, Guitar og Mundharmonika
;run;

data KockSokkerOgSko;
	length navn type $13 jobbeskrivelse $38 interesser $11;
	infile datalines dsd;
	input navn $ type $ jobbeskrivelse$ Svaghed$ interesser$;
	datalines;
	Benny Jensen, Nåsåer, Rejsende handelsmand i sko og strømper, Snaps, Gamle bøger
;run;

proc contents data=FamilienSand;run;
proc contents data=Nisser;run;

data test;
set FamilienSand;output;
set nisser;output;
run;

/*proc append data=FamilienSand base=Nisser;run; *Error;*/
/*proc append data=FamilienSand base=Nisser force;run; *Truncates the FamilienSand – data set;*/

%Union(dsn1=FamilienSand,dsn2=Nisser,out=Heltene);
%Union(dsn1=Heltene,dsn2=KockSokkerOgSko,out=SamletJulekalender);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Dec 2017 12:22:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418106#M128</guid>
      <dc:creator>Emil_Sarauw</dc:creator>
      <dc:date>2017-12-04T12:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: Juletip #2 - The Julekalender %Union;</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418111#M129</link>
      <description>&lt;P&gt;It's hard to be a Nissemand&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2017 11:27:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418111#M129</guid>
      <dc:creator>jmic_nyk</dc:creator>
      <dc:date>2017-12-04T11:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: Juletip #2 - The Julekalender %Union;</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418114#M130</link>
      <description>&lt;P&gt;Hi Emil&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Great tip - thanks. A small improvement to the macro could be to use the WORK dir to save the intermediate file combined.sas instead of the hardcoded c:\temp. Thus, you would not experience problems with filesystems and access rights (and perhaps multiple users running on the same server). A slightly changed macro becomes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="Courier New" size="3"&gt;%macro union(dsn1=,     /*Name of the first data set    */
             dsn2=,     /*Name of the second data set   */&lt;BR /&gt;             out=       /*Name of combined data set     */);
  
   proc contents data=&amp;amp;dsn1 noprint
      out=out1(keep=name type length where=(type=2));
   proc contents data=&amp;amp;dsn2 noprint
      out=out2(keep=name type length where=(type=2));
   run;
   %let work_path=%sysfunc(pathname(work));
   data _null_;
      file "&amp;amp;work_path\combined.sas";
      merge out1 out2(rename=(length=length2)) end=last;
      by name;
      if _n_ = 1 then put "Data &amp;amp;out;";
      l = max(length,length2);
      put "   length " name " $ " l 5. ";";
      if last then do;
         put "   set &amp;amp;dsn1 &amp;amp;dsn2;";
         put "run;";
      end;
   run;
   %include "&amp;amp;work_path\combined.sas";
%mend union;
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Det bår' dæjli'&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 09:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418114#M130</guid>
      <dc:creator>CKjeldsen</dc:creator>
      <dc:date>2017-12-05T09:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: Juletip #2 - The Julekalender %Union;</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418121#M132</link>
      <description>&lt;P&gt;Hej Christian, god pointe! : )&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2017 11:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/418121#M132</guid>
      <dc:creator>Emil_Sarauw</dc:creator>
      <dc:date>2017-12-04T11:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: Juletip #2 - The Julekalender %Union;</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/420031#M142</link>
      <description>&lt;P&gt;Fedt Emil, jeg har allerede brugt det hos en kunde!&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2017 08:02:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-2-The-Julekalender-Union/m-p/420031#M142</guid>
      <dc:creator>astrid_vest</dc:creator>
      <dc:date>2017-12-11T08:02:21Z</dc:date>
    </item>
  </channel>
</rss>

