<?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 union , when same column has different formats in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703342#M215480</link>
    <description>&lt;P&gt;You want to stack data so the formatted value is different based on which table the value came from.&amp;nbsp; However, Proc SQL will assign a column the format that is from the first table in the UNION and a column can have only one format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc FORMAT maps a raw value to a single formatted value. (&lt;EM&gt;And, yes, a multi-label format (MLF) can map a single value to more than one value and some Procedures are MLF 'aware')&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case MLF&amp;nbsp;is not plausible, leaving you with a conundrum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will need some data transformation to get the desired result.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose the column in the union contains the formatted values and the original raw values are kept unformatted in a separate new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SQL Example&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format;
  value $ fmt_one
    'COMPLETED' = 'Completed'
    'SPONSOR' = 'Discontinued'
  ;

  value $ fmt_two
    'COMPLETED' = 'Completed'
  ;

run;

data Table_one;
  id = 1; task = 'COMPLETED'; output;
  id = 2; task = 'SPONSOR';   output;

  format task $fmt_one.;
run;

data Table_two;
  id = 3; task = 'COMPLETED'; output;
  id = 4; task = 'SPONSOR';   output;

  format task $fmt_two.;
run;

proc sql;&lt;BR /&gt;  * task gets format from table_one;
  create table table_union12 as 
  select *, 'table_one' as source from table_one UNION
  select *, 'table_two' as source from table_two
  ;
&lt;BR /&gt;  * task gets formatted value per format and data set;
  create table table_union_wanted (drop=_task)
  as
    select *
      , put(_task,$fmt_one.) as task 
      , _task as raw_task
      , 'table_one' as source
    from table_one (rename=task=_task)
  UNION
    select *
      , put(_task,$fmt_two.) as task 
      , _task as raw_task
      , 'table_two' as source
    from table_two (rename=task=_task)
  ;

ods html file='problem.html';


proc print data=table_one; title "Table_One with task using FMT_ONE";
proc print data=table_two; title "Table_Two with task using FMT_TWO";
proc print data=table_union12; title "Tables unioned, task is FMT_ONE";
proc print data=table_union_wanted; title "Tables unioned, task is not formatted";
run;

ods html close;&lt;/PRE&gt;
&lt;P&gt;Output&lt;/P&gt;
&lt;DIV id="tinyMceEditorRichardADeVenezia_1" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
    <pubDate>Thu, 03 Dec 2020 11:27:45 GMT</pubDate>
    <dc:creator>RichardDeVen</dc:creator>
    <dc:date>2020-12-03T11:27:45Z</dc:date>
    <item>
      <title>How to union , when same column has different formats</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703268#M215473</link>
      <description>&lt;P&gt;Hi Reader,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to union/set , when same column has different formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;1st dataset has - Sponsor records which is converted to Discontinued.&lt;/P&gt;
&lt;P&gt;2nd dataset has - Sponsor which I required as it is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format;&lt;BR /&gt;value $ fmt &lt;BR /&gt;'COMPLETED' = 'Completed'&lt;/P&gt;
&lt;P&gt;"SPONSOR" = 'Discontinued'&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in final output - required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Discontinued&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SPONSOR&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2020 04:59:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703268#M215473</guid>
      <dc:creator>pdhokriya</dc:creator>
      <dc:date>2020-12-03T04:59:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to union , when same column has different formats</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703272#M215474</link>
      <description>&lt;P&gt;Sorry, but i don't understand the problem you have, maybe you could add the data you have in usable form (or at least as table) so that is more obvious what you are taking about.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2020 05:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703272#M215474</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-03T05:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to union , when same column has different formats</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703342#M215480</link>
      <description>&lt;P&gt;You want to stack data so the formatted value is different based on which table the value came from.&amp;nbsp; However, Proc SQL will assign a column the format that is from the first table in the UNION and a column can have only one format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc FORMAT maps a raw value to a single formatted value. (&lt;EM&gt;And, yes, a multi-label format (MLF) can map a single value to more than one value and some Procedures are MLF 'aware')&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case MLF&amp;nbsp;is not plausible, leaving you with a conundrum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will need some data transformation to get the desired result.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose the column in the union contains the formatted values and the original raw values are kept unformatted in a separate new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SQL Example&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format;
  value $ fmt_one
    'COMPLETED' = 'Completed'
    'SPONSOR' = 'Discontinued'
  ;

  value $ fmt_two
    'COMPLETED' = 'Completed'
  ;

run;

data Table_one;
  id = 1; task = 'COMPLETED'; output;
  id = 2; task = 'SPONSOR';   output;

  format task $fmt_one.;
run;

data Table_two;
  id = 3; task = 'COMPLETED'; output;
  id = 4; task = 'SPONSOR';   output;

  format task $fmt_two.;
run;

proc sql;&lt;BR /&gt;  * task gets format from table_one;
  create table table_union12 as 
  select *, 'table_one' as source from table_one UNION
  select *, 'table_two' as source from table_two
  ;
&lt;BR /&gt;  * task gets formatted value per format and data set;
  create table table_union_wanted (drop=_task)
  as
    select *
      , put(_task,$fmt_one.) as task 
      , _task as raw_task
      , 'table_one' as source
    from table_one (rename=task=_task)
  UNION
    select *
      , put(_task,$fmt_two.) as task 
      , _task as raw_task
      , 'table_two' as source
    from table_two (rename=task=_task)
  ;

ods html file='problem.html';


proc print data=table_one; title "Table_One with task using FMT_ONE";
proc print data=table_two; title "Table_Two with task using FMT_TWO";
proc print data=table_union12; title "Tables unioned, task is FMT_ONE";
proc print data=table_union_wanted; title "Tables unioned, task is not formatted";
run;

ods html close;&lt;/PRE&gt;
&lt;P&gt;Output&lt;/P&gt;
&lt;DIV id="tinyMceEditorRichardADeVenezia_1" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Thu, 03 Dec 2020 11:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703342#M215480</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-12-03T11:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to union , when same column has different formats</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703346#M215482</link>
      <description>@RichardADeVenezia : Thank you so much for the quick reply. and help.</description>
      <pubDate>Thu, 03 Dec 2020 11:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-union-when-same-column-has-different-formats/m-p/703346#M215482</guid>
      <dc:creator>pdhokriya</dc:creator>
      <dc:date>2020-12-03T11:53:31Z</dc:date>
    </item>
  </channel>
</rss>

