<?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: Using LABEL in PROC SQL with UNION removes the FORMAT: is it normal? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/931705#M83524</link>
    <description>&lt;P&gt;PROC SQL uses different logic to consolidate the formats and labels attached to the same variables from different datasets than a DATA step would use.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step the first place where a FORMAT or LABEL exists is the one that "wins" even it is not the first dataset referenced in the DATA step.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a SQL step with UNION CORR then FORMAT and LABEL on the first dataset "wins", even when those they are empty.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you seem to have discovered a wrinkle on how this is done when the first sub-query of the UNION is not a simply a reference to an existing variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that it caused by an order or operations issue in the logic.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Jun 2024 14:02:02 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-06-11T14:02:02Z</dc:date>
    <item>
      <title>Using LABEL in PROC SQL with UNION removes the FORMAT: is it normal?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/931681#M83522</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not sure if that's the right place to ask for this, but I just identified a weird behavior of the proc sql.&lt;BR /&gt;&lt;BR /&gt;Let's review this piece of code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc format;
    value yn 
       0='No' 
       1='Yes'
   ;
run;

data ds1;
    attrib pt format=$2.;
    attrib question format=yn.;
    
    pt='01'; question=0;  output;
    pt='02'; question=1;  output;
run;

proc sql noprint;
    create table test as
        select      pt 'Patient',
                    question label='Yes/no'
        from        ds1
    ;
quit;&lt;/PRE&gt;&lt;P&gt;The output dataset &lt;STRONG&gt;test&lt;/STRONG&gt; is as expected, in particular the variable &lt;STRONG&gt;question&lt;/STRONG&gt; has format &lt;STRONG&gt;yn.&lt;/STRONG&gt; and label "Yes/no"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, let's update the code to add an&amp;nbsp;&lt;STRONG&gt;union&lt;/STRONG&gt;:&lt;/P&gt;&lt;PRE&gt;proc format;
    value yn 
       0='No' 
       1='Yes'
   ;
run;

data ds1;
    attrib pt format=$2.;
    attrib question format=yn.;
    
    pt='01'; question=0;  output;
    pt='02'; question=1;  output;
run;

data ds2;
    attrib pt format=$2.;
    attrib question format=yn.;
    
    pt='02'; question=0;  output;
    pt='03'; question=1;  output;
run;

proc sql noprint;
    create table test as
        select      pt 'Patient',
                    question label='Yes/no' /* la label fait sauter le format */ 
        from        ds1
        
        union corr
        
        select      pt,
                    question 
        from        ds2
    ;
quit;
    &lt;/PRE&gt;&lt;P&gt;The output dataset &lt;STRONG&gt;test&lt;/STRONG&gt; is &lt;STRONG&gt;NOT&lt;/STRONG&gt; as expected, in particular the variable &lt;STRONG&gt;question&lt;/STRONG&gt; has no format anymore, but the expected "Yes/no" label.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If we remove the label modifier, the format is kept&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this behavior expected?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 11:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/931681#M83522</guid>
      <dc:creator>JuSAS</dc:creator>
      <dc:date>2024-06-11T11:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using LABEL in PROC SQL with UNION removes the FORMAT: is it normal?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/931694#M83523</link>
      <description>&lt;P&gt;It's not what I would expect. I suspect you've found a bug. Using this code, the problem is reproducible in SAS9.4 M8 and Viya 2024.04.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
	value yn
	0='No'
	1='Yes';
run;

data ds1;
	format pt $2. question yn.;
	label pt ='Patient' 
		  question ='Yes or No';
	pt='01';
	question=0;
	output;
	pt='02';
	question=1;
	output;
run;

data ds2;
	format pt $2. question yn.;
	label pt ='Patient' 
		  question ='Yes or No';
	pt='03';
	question=1;
	output;
	pt='04';
	question=2;
	output;
run;

proc sql ;
title "DS1";
select * from ds1;

title "DS2";
select * from ds2;

title "DS1 union DS2";
select pt
	 , question 
	from ds1
	union corr
	select *
	from ds2;

title "DS1 union DS2 ";
title2 "Changing the label of the second column removes formatting";
select pt
	 , question label='Y/N'
	from ds1
	union corr
	select *
	from ds2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jun 2024 12:46:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/931694#M83523</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2024-06-11T12:46:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using LABEL in PROC SQL with UNION removes the FORMAT: is it normal?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/931705#M83524</link>
      <description>&lt;P&gt;PROC SQL uses different logic to consolidate the formats and labels attached to the same variables from different datasets than a DATA step would use.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step the first place where a FORMAT or LABEL exists is the one that "wins" even it is not the first dataset referenced in the DATA step.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a SQL step with UNION CORR then FORMAT and LABEL on the first dataset "wins", even when those they are empty.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you seem to have discovered a wrinkle on how this is done when the first sub-query of the UNION is not a simply a reference to an existing variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that it caused by an order or operations issue in the logic.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 14:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/931705#M83524</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-11T14:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using LABEL in PROC SQL with UNION removes the FORMAT: is it normal?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/932529#M83529</link>
      <description>&lt;P&gt;Well,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Despite I agree that data step and proc sql are probably different in the way they process the data, my concern is that for given example, the result is not intuive at all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both data tables used in the proc sql have the exact same columns (and format)&lt;/P&gt;&lt;P&gt;Thus, performing a join should not remove the format.&lt;/P&gt;&lt;P&gt;It could be acceptable if both columns &lt;STRONG&gt;question&amp;nbsp;&lt;/STRONG&gt;had the same type but different formats.&lt;/P&gt;&lt;P&gt;That's not the case here: both variable shave the same type (numeric) and same format (yn.)&lt;/P&gt;&lt;P&gt;Thus, it is expected that column questino in the output data table is numeric with yn. format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is no clear explanation why this is happening, and that's not documented.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jun 2024 13:25:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/932529#M83529</guid>
      <dc:creator>JuSAS</dc:creator>
      <dc:date>2024-06-15T13:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using LABEL in PROC SQL with UNION removes the FORMAT: is it normal?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/932534#M83530</link>
      <description>&lt;P&gt;I was just thinking of reasons how/why they made this mistake.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you try it when the label was applied in a subquery?&amp;nbsp; Does it still have the issue?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select * from (select x label='new label' from have)
union corr
select x from have&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jun 2024 14:59:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/932534#M83530</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-15T14:59:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using LABEL in PROC SQL with UNION removes the FORMAT: is it normal?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/933497#M83532</link>
      <description>&lt;P&gt;This is definitely a bug. The easiest workaround is to re-apply the format when you change the label.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select pt
	 ,question label='Y/N' format=yn.
	from ds1
	union corr
	select *
	from ds2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If this is a concern for&amp;nbsp; you in your day-to-day SAS use, I recommend submitting it to SAS tech support for further evaluation via the &lt;A href="https://service.sas.com/csm" target="_blank"&gt;Customer Service Portal - Customer Support (sas.com)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Jun 2024 15:10:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-LABEL-in-PROC-SQL-with-UNION-removes-the-FORMAT-is-it/m-p/933497#M83532</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2024-06-23T15:10:46Z</dc:date>
    </item>
  </channel>
</rss>

