<?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: Merging/concatenating multiple datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386135#M92452</link>
    <description>&lt;P&gt;Here's what I would do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. create a data set with the data structure of each of your three data sets&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro save_data_structure(libname=library&lt;/P&gt;&lt;P&gt;,memname=&lt;/P&gt;&lt;P&gt;,n=%substr(&amp;amp;memname,%length(&amp;amp;memname)-1,1);&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table data_structure_&amp;amp;n as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select name, type, length as length_&amp;amp;n&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname = "%upcase(&amp;amp;libname)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and memname ="%upcase(&amp;amp;memname)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and memtype eq 'DATA';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; quit;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%save_data_structure(data1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%save_data_structure(data3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%save_data_structure(data3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. rename the length to the data set N: i.e. length = length1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. merge the three data sets on variable Name&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4. calculate the max length&lt;/P&gt;&lt;P&gt;array _length (*) length_1 length_2 length_3;&lt;/P&gt;&lt;P&gt;length = max(_length(*));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4. choose only variables that are in all three data sets&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;merge&lt;/P&gt;&lt;P&gt;data_structure_1(in = have1)&lt;/P&gt;&lt;P&gt;data_structure_2(in = have2)&lt;/P&gt;&lt;P&gt;data_structure_3(in = have3);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if have1 and have2 and have3 then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;5. use this&amp;nbsp; data set to write either a length or attribute statement&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hth&lt;/P&gt;&lt;P&gt;Ron Fehd&amp;nbsp; data structure maven&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Aug 2017 20:22:53 GMT</pubDate>
    <dc:creator>Ron_MacroMaven</dc:creator>
    <dc:date>2017-08-07T20:22:53Z</dc:date>
    <item>
      <title>Merging/concatenating multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386121#M92449</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to combine three SAS datasets. Each dataset has a different number of variables (e.g. dataset A has 209 vars, B has 97 vars and C has 102.) When I tried combining them using the data step I got a bunch of warning messages saying "multiple lengths were specified for..". &amp;nbsp;I expected this because the datasets were created by different people. So my question is what would be the best way to go about this? How do I redefine the length and the type of the variables so that those variables I would like to see included in my master dataset are formatted consistently (e.g. character, length etc.) Also, should that happen before I combine them or after? I am only going to keep the variables that exist in all three datasets once I combine them.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2017 19:47:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386121#M92449</guid>
      <dc:creator>Kiko</dc:creator>
      <dc:date>2017-08-07T19:47:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merging/concatenating multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386127#M92451</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When handed multiple data sets with different lengths for variables all you have to do is set a definition in the data step combining them before the set statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose you have a variable named text that is in all three sets and has lengths of 11, 25 and 56. This will get rid of the message and prevent any truncation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; length text $ 56; &amp;lt;= note that this is the longest of the lengths for the variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set data1 data2 data3;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest using keep (or drop statements) to keep only the variables you want. &amp;nbsp;If your final data set only wants these variables text var1 var4 var27 then code as (add lengths as needed)&lt;/P&gt;
&lt;P&gt;data want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; length text $ 56; &amp;lt;= note that this is the longest of the lengths for the variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;data1 (keep=text var1 var4 var27)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data2 (keep=text&amp;nbsp;var1 var4 var27 )&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data3 (keep=text&amp;nbsp;var1 var4 var27 )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2017 20:00:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386127#M92451</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-07T20:00:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merging/concatenating multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386135#M92452</link>
      <description>&lt;P&gt;Here's what I would do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. create a data set with the data structure of each of your three data sets&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro save_data_structure(libname=library&lt;/P&gt;&lt;P&gt;,memname=&lt;/P&gt;&lt;P&gt;,n=%substr(&amp;amp;memname,%length(&amp;amp;memname)-1,1);&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table data_structure_&amp;amp;n as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select name, type, length as length_&amp;amp;n&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname = "%upcase(&amp;amp;libname)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and memname ="%upcase(&amp;amp;memname)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and memtype eq 'DATA';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; quit;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%save_data_structure(data1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%save_data_structure(data3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%save_data_structure(data3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. rename the length to the data set N: i.e. length = length1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. merge the three data sets on variable Name&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4. calculate the max length&lt;/P&gt;&lt;P&gt;array _length (*) length_1 length_2 length_3;&lt;/P&gt;&lt;P&gt;length = max(_length(*));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4. choose only variables that are in all three data sets&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;merge&lt;/P&gt;&lt;P&gt;data_structure_1(in = have1)&lt;/P&gt;&lt;P&gt;data_structure_2(in = have2)&lt;/P&gt;&lt;P&gt;data_structure_3(in = have3);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if have1 and have2 and have3 then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;5. use this&amp;nbsp; data set to write either a length or attribute statement&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hth&lt;/P&gt;&lt;P&gt;Ron Fehd&amp;nbsp; data structure maven&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2017 20:22:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386135#M92452</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2017-08-07T20:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: Merging/concatenating multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386341#M92502</link>
      <description>&lt;P&gt;Thank you for your reply.&amp;nbsp;I just tried what you suggested and it&amp;nbsp;worked great!&amp;nbsp;I&amp;nbsp;have another question though.&amp;nbsp;So&amp;nbsp; your code&amp;nbsp;above does specify the length of variable as well as the type, but I still got an error message saying "Variable xx has been defined both char and numeric"&amp;nbsp; so I am guessing I should add something to make sure that the type of var is consistent across the datasets. How do I do that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2017 17:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386341#M92502</guid>
      <dc:creator>Kiko</dc:creator>
      <dc:date>2017-08-08T17:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Merging/concatenating multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386367#M92518</link>
      <description>&lt;P&gt;I was wondering if you had mismatched types;&lt;/P&gt;&lt;P&gt;your problem is probably an order of magnitude more difficult for this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;rename variables which have different types&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data1 (rename = (var1 = var1_c))&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data2 (rename = (var2 = var2_n))&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;set up an array with the list of vars with mis-matched types&lt;/P&gt;&lt;P&gt;and do a loop to convert to your desired type&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;array _char(*) &amp;lt;list to convert&amp;gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;array _num(*) &amp;lt;list to convert&amp;gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;do i = 1 to dim(_char);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; _num(i) = input(_char(i),best.);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; *or:;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; _char(i) = put(_num(i),best.); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hth&lt;/P&gt;&lt;P&gt;Ron Fehd&amp;nbsp; which type maven&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2017 18:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-concatenating-multiple-datasets/m-p/386367#M92518</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2017-08-08T18:54:18Z</dc:date>
    </item>
  </channel>
</rss>

