<?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: Format transformation error in macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980180#M378899</link>
    <description>&lt;P&gt;This is one of the more non-intuitive things about how SAS processes data - I will have to leave this to someone else to explain, but here's a simple example that demonstrates what's going on:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
x=0;
if x=1 then do;
	chk=5/0;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program will still give a division by zero note in the log even though 'chk' would be missing in this case regardless of whether that was a valid mathematical operation (because x is not equal to 1).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a way around your problem - instead using macro logic to write out the relevant section of the DATA step depending on the var type of &amp;lt;v1&amp;gt;.&amp;nbsp; &amp;nbsp;...Well, SAS ODA just ate my example, but essentially, first capture vtype in a separate step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set &amp;amp;dst;
call symputx("&amp;amp;v1._type", vtype(&amp;amp;v1));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...then use macro logic to write out the relevant portion of the DATA step depending on vtype:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
set test;
%if &amp;amp;v1._type=N %then %do;
    ** stuff to do when type is N... ;
%end;
%else %do;
    ** stuff to do when type is C... ;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Dec 2025 02:58:50 GMT</pubDate>
    <dc:creator>quickbluefish</dc:creator>
    <dc:date>2025-12-05T02:58:50Z</dc:date>
    <item>
      <title>Format transformation error in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980178#M378898</link>
      <description>&lt;P&gt;%macro get_num_char(dst,v1,f1) ;&lt;BR /&gt;data nc_&amp;amp;dst.;&lt;BR /&gt;set &amp;amp;dst.;&lt;BR /&gt;length &amp;amp;v1._num 8.;&lt;BR /&gt;length &amp;amp;v1._char $20.;&lt;BR /&gt;&amp;amp;v1._type = vtype(&amp;amp;v1.);&lt;/P&gt;&lt;P&gt;if &amp;amp;v1._type='N' then do;&lt;BR /&gt;&amp;amp;v1._num=&amp;amp;v1.;&lt;BR /&gt;&amp;amp;v1._char=strip(put(&amp;amp;v1.,&amp;amp;f1.));&lt;BR /&gt;end;&lt;BR /&gt;if &amp;amp;v1._type='C' then do;&lt;BR /&gt;&amp;amp;v1._char=&amp;amp;v1.;&lt;BR /&gt;&amp;amp;v1._num=input(&amp;amp;v1.,&amp;amp;f1.);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;%mend get_num_char;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;a1 = '2025-12-05';&lt;BR /&gt;a2 = 45996;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%get_num_char(test, a1, yymmdd10.);&lt;BR /&gt;%get_num_char(test, a2, yymmdd10.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The first code "&lt;STRONG&gt;%get_num_char(test, a1, yymmdd10.);&lt;/STRONG&gt;" log reports an error "The format $YYMMDD was not found or could not be loaded.". Strangely, this error seems to be reported when Macro runs "&lt;STRONG&gt;&amp;amp;v1._char=strip(put(&amp;amp;v1.,&amp;amp;f1.));&lt;/STRONG&gt;" when a1 is a character variable rather than a number. I think "&lt;STRONG&gt;if &amp;amp;v1._type='N' then do;&lt;/STRONG&gt;" has prevented following code block from running so it shouldn't report an error. Can anyone tell me why this happened?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Dec 2025 02:20:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980178#M378898</guid>
      <dc:creator>odahviing</dc:creator>
      <dc:date>2025-12-05T02:20:07Z</dc:date>
    </item>
    <item>
      <title>Re: Format transformation error in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980180#M378899</link>
      <description>&lt;P&gt;This is one of the more non-intuitive things about how SAS processes data - I will have to leave this to someone else to explain, but here's a simple example that demonstrates what's going on:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
x=0;
if x=1 then do;
	chk=5/0;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program will still give a division by zero note in the log even though 'chk' would be missing in this case regardless of whether that was a valid mathematical operation (because x is not equal to 1).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a way around your problem - instead using macro logic to write out the relevant section of the DATA step depending on the var type of &amp;lt;v1&amp;gt;.&amp;nbsp; &amp;nbsp;...Well, SAS ODA just ate my example, but essentially, first capture vtype in a separate step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set &amp;amp;dst;
call symputx("&amp;amp;v1._type", vtype(&amp;amp;v1));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...then use macro logic to write out the relevant portion of the DATA step depending on vtype:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
set test;
%if &amp;amp;v1._type=N %then %do;
    ** stuff to do when type is N... ;
%end;
%else %do;
    ** stuff to do when type is C... ;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Dec 2025 02:58:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980180#M378899</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-12-05T02:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: Format transformation error in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980181#M378900</link>
      <description>&lt;P&gt;If you don't want the data step compiler to complain about your usage of the wrong type of FORMAT in the PUT() function call then switch to using PUTN() instead so that the checking is done at execution time instead.&amp;nbsp; Similarly use INPUTN() in your other branch.&amp;nbsp; This works because PUTN() and INPUTN() (and corresponding character versions) take a character expression as the second argument instead of a format literal.&amp;nbsp; So the compiler cannot check it since the value is not known until the step runs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_num_char(dst,v1,f1) ;
data nc_&amp;amp;dst.;
set &amp;amp;dst.;
length &amp;amp;v1._num 8.;
length &amp;amp;v1._char $20.;
&amp;amp;v1._type = vtype(&amp;amp;v1.);

if &amp;amp;v1._type='N' then do;
&amp;amp;v1._num=&amp;amp;v1.;
&amp;amp;v1._char=strip(putn(&amp;amp;v1.,"&amp;amp;f1."));
end;
if &amp;amp;v1._type='C' then do;
&amp;amp;v1._char=&amp;amp;v1.;
&amp;amp;v1._num=inputn(&amp;amp;v1.,"&amp;amp;f1.");
end;
run;
%mend get_num_char;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note:&amp;nbsp; You should also replace STRIP() with LEFT() since you don't really need to remove the trailing spaces since they will just be added back when the value is written into the fixed length character variable.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Dec 2025 03:44:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980181#M378900</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-12-05T03:44:52Z</dc:date>
    </item>
    <item>
      <title>Re: Format transformation error in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980182#M378901</link>
      <description>&lt;P&gt;The SAS log for your example explains what is happening.&lt;/P&gt;
&lt;PRE&gt; 71         data test;
 72         x=0;
 73         if x=1 then do;
 74           chk=5/0;
 NOTE: Division by zero detected during the compilation phase, detected at line 74 column 8.
 75         end;
 76         run;&lt;/PRE&gt;
&lt;P&gt;Because 5 and 0 are constants the compiler can check if they will cause trouble before the step starts running.&lt;/P&gt;
&lt;P&gt;If you change it to divide by X instead then the compiler will not issue that warning since it cannot know what value X will have.&amp;nbsp; Even in this case where X has a constant value.&lt;/P&gt;
&lt;PRE&gt; 71         data test;
 72          x=0;
 73          if x=1 then do;
 74           chk=5/x;
 75          end;
 76         run;
 
 NOTE: The data set WORK.TEST has 1 observations and 2 variables.&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Dec 2025 03:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980182#M378901</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-12-05T03:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: Format transformation error in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980183#M378902</link>
      <description>PUTC and INPUTN indeed work smoothly. However, this just get a new question to me: why format "YYMMDD10." resolves to "$YYMMDD10." when execute PUT in compile phase? Could you please explain this question ?</description>
      <pubDate>Fri, 05 Dec 2025 03:54:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980183#M378902</guid>
      <dc:creator>odahviing</dc:creator>
      <dc:date>2025-12-05T03:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: Format transformation error in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980184#M378903</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/476155"&gt;@odahviing&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;PUTC and INPUTN indeed work smoothly. However, this just get a new question to me: why format "YYMMDD10." resolves to "$YYMMDD10." when execute PUT in compile phase? Could you please explain this question ?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SAS is trying to be helpful (and also trying to allow old programs to be run with new versions of SAS).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you used a CHARACTER variable with the PUT() function you needed to specify a character format.&amp;nbsp; In the old (very very old) days SAS did not require that character formats start with a $.&amp;nbsp; &amp;nbsp;When SAS could not find find a character format named YYMMDD it decided you must have meant $YYMMDD. But it could not find that one either so it generated an error message use the "fixed" format name.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Dec 2025 03:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980184#M378903</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-12-05T03:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: Format transformation error in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980185#M378904</link>
      <description>&lt;P&gt;Thanks. An intersting SAS correct mechanism&lt;/P&gt;</description>
      <pubDate>Fri, 05 Dec 2025 04:04:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980185#M378904</guid>
      <dc:creator>odahviing</dc:creator>
      <dc:date>2025-12-05T04:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: Format transformation error in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980198#M378908</link>
      <description>Thanks Tom - always learn something from your posts.</description>
      <pubDate>Fri, 05 Dec 2025 11:14:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-transformation-error-in-macro/m-p/980198#M378908</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-12-05T11:14:37Z</dc:date>
    </item>
  </channel>
</rss>

