<?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: An efficient method to merge datasets with multiple length for the character variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768131#M243607</link>
    <description>Stack these dataset by UNION operator.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select * from d1 &lt;BR /&gt;union all corr&lt;BR /&gt;select * from d2&lt;BR /&gt;union all corr&lt;BR /&gt;select * from d3&lt;BR /&gt;union all corr&lt;BR /&gt;select * from d4&lt;BR /&gt;union all corr&lt;BR /&gt;select * from d5&lt;BR /&gt;;&lt;BR /&gt;quit;</description>
    <pubDate>Thu, 16 Sep 2021 13:36:54 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-09-16T13:36:54Z</dc:date>
    <item>
      <title>An efficient method to concatenating/stac datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768022#M243562</link>
      <description>&lt;P&gt;I want to merge multiple datasets with multiple variables but the length of the character variables are different in each dataset. I created an example that works but I want to know how I can improve it to make it easier to apply when the number of datasets is 5-10 with 20 variables to check for length.&lt;/P&gt;
&lt;P&gt;Thanks for your thoughts&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d1;
	length var1 $10. var2 $10.;
	input var1 var2;
	cards;
apple carrots
orange cabbage
;

data d2;
	length var1 $30. var2 $30.;
	input var1 var2;
	datalines;
apple-pie-recipe  carrot-soup
orange-juice-for-breakfast cabbage-soup-for-supper
;

data d1_i (drop= var1_old var2_old);
	set d1(rename= (var1=var1_old var2=var2_old));
	length var1 $30.;
	var1=var1_old;
	length var2 $30.;
	var2=var2_old;
	run;

data want;
	set d1_i d2;
	run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 12:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768022#M243562</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T12:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768023#M243563</link>
      <description>&lt;P&gt;Find the maximum lengths and just add a length statement before the SET.&lt;/P&gt;
&lt;P&gt;So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length var1 $30 var2 $30 ;
  set d1 d2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can generate that by querying the metadata for the source tables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct catx(' ',name,cats('$',length))
  into :lengths separated by ' '
from
  (select upcase(name) as unique_name,min(name) as name,max(length) as length
  from dictionary.columns
  where libname='WORK' 
    and memname in ('D1' 'D2')
    and type='char'
  group by unique_name
  )
;
%let nchar=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then you can add that LENGTH statement to your step that combines the dataset.&lt;/P&gt;
&lt;P&gt;You might also want to remove any formats that are attached to prevent things like a variable with length 200 using a format of only $30. which will cause the values to appear truncated even when they are not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
%if &amp;amp;nchar %then %do;
  length &amp;amp;lengths;
  format _character_ ;
%end;
  set d1 d2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS&amp;nbsp; You don't need to include a period when specifying the length of a variable. Variables can only have integer lengths.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 04:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768023#M243563</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-16T04:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768024#M243564</link>
      <description>&lt;P&gt;It would probably be better to create the original dataset with consistent lengths for the variables.&lt;/P&gt;
&lt;P&gt;What is the source of those dataset and why does the same variable end up having a different length in different datasets?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 04:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768024#M243564</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-16T04:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768033#M243566</link>
      <description>&lt;P&gt;Be careful with your wording. In SAS parlance,&amp;nbsp;&lt;EM&gt;merge&lt;/EM&gt; means putting data side-by-side. What you want is&amp;nbsp;&lt;EM&gt;concatenating&lt;/EM&gt;,&amp;nbsp;&lt;EM&gt;stacking&lt;/EM&gt; or &lt;EM&gt;appending&amp;nbsp;&lt;/EM&gt;datasets.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 07:06:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768033#M243566</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-16T07:06:01Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768100#M243594</link>
      <description>&lt;P&gt;The best way is using PROC SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data d1;
	length var1 $10. var2 $10.;
	input var1 var2;
	cards;
apple carrots
orange cabbage
;

data d2;
	length var1 $30. var2 $30.;
	input var1 var2;
	datalines;
apple-pie-recipe  carrot-soup
orange-juice-for-breakfast cabbage-soup-for-supper
;

proc sql;
create table want as
select * from d1 
union all corr
select * from d2;
quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Sep 2021 12:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768100#M243594</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-16T12:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768102#M243596</link>
      <description>&lt;P&gt;Woe, thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;! I am glad I asked. It is much easier than changing the length one by one!&lt;/P&gt;
&lt;P&gt;Am I right that&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;union all corr&lt;/PRE&gt;
&lt;P&gt;does the job of increasing the length to the longer one automatically? Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 12:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768102#M243596</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T12:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768105#M243598</link>
      <description>Thank you KurtBremser. I changed the title so that is right for the future users.</description>
      <pubDate>Thu, 16 Sep 2021 12:50:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768105#M243598</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T12:50:29Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768107#M243599</link>
      <description>I agree! The datasets were created from different sources and by different individuals. then when I imported them to SAS from SPSS/EXCELL the length is assigned by SAS. Is there any way I can prevent this in the future? Thanks</description>
      <pubDate>Thu, 16 Sep 2021 12:52:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768107#M243599</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T12:52:21Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768112#M243601</link>
      <description>Correct !</description>
      <pubDate>Thu, 16 Sep 2021 12:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768112#M243601</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-16T12:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768113#M243602</link>
      <description>Thanks!</description>
      <pubDate>Thu, 16 Sep 2021 12:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768113#M243602</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T12:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768119#M243604</link>
      <description />
      <pubDate>Thu, 16 Sep 2021 14:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768119#M243604</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T14:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768121#M243606</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;May I please ask one more question. How do I modify the code to stack 5 datasets? I tried adding lines like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select * from d3;
select * from d4;
select * from d5;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but that did not work. Any hint to avoid adding datasets altogether instead of one at a time? Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 13:22:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768121#M243606</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T13:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768131#M243607</link>
      <description>Stack these dataset by UNION operator.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select * from d1 &lt;BR /&gt;union all corr&lt;BR /&gt;select * from d2&lt;BR /&gt;union all corr&lt;BR /&gt;select * from d3&lt;BR /&gt;union all corr&lt;BR /&gt;select * from d4&lt;BR /&gt;union all corr&lt;BR /&gt;select * from d5&lt;BR /&gt;;&lt;BR /&gt;quit;</description>
      <pubDate>Thu, 16 Sep 2021 13:36:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768131#M243607</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-16T13:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768139#M243611</link>
      <description>&lt;P&gt;PROC IMPORT of an SPSS files should replicate the structure in the SPSS file pretty well.&lt;/P&gt;
&lt;P&gt;But PROC IMPORT of an EXCEL file cannot since there is no concept of a variable in EXCEL.&amp;nbsp; Every cell can be totally independent from each other cell.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are getting files from EXCEL you will have more control if they files are delivered as delimited text files.&amp;nbsp; Then you can write your own data step in SAS to read file so that you have full control over the type, length, name and other attributes of the variables.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 13:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768139#M243611</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-16T13:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768145#M243613</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;I hope this is the last question. How can I keep the variables in each dataset that do not match (other than var1 and var2) but I want to keep in the stacked dataset? Thanks!!!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d1;
	length var1 $10. var2 $10. var3 8;
	input var1 var2 var3;
	cards;
apple carrots 99
orange cabbage 88
;

data d2;
	length var1 $30. var2 $30. var4 8 var5 8;
	input var1 var2 var4 var5;
	datalines;
apple-pie-recipe  carrot-soup 100 111
orange-juice-for-breakfast cabbage-soup-for-supper 200 222
;

proc sql;
create table want as
select * from d1 
union all corr
select * from d2;
quit;
&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>Thu, 16 Sep 2021 14:33:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768145#M243613</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T14:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: An efficient method to merge datasets with multiple length for the character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768162#M243618</link>
      <description>&lt;P&gt;I found the solution for the case when we have non-matching variables in each dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select * from d1 
OUTER UNION CORR
select * from d2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks for introducing this PROC SQL procedure. It was very helpful &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 16:53:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/An-efficient-method-to-concatenating-stac-datasets-with-multiple/m-p/768162#M243618</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-09-16T16:53:04Z</dc:date>
    </item>
  </channel>
</rss>

