<?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: Apply the user defined format to the variable within the datastep if variable exists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550572#M152876</link>
    <description>&lt;P&gt;Thanks for replying.&lt;/P&gt;&lt;P&gt;Would you please give me a little example?&lt;/P&gt;</description>
    <pubDate>Fri, 12 Apr 2019 10:54:10 GMT</pubDate>
    <dc:creator>Sami1234</dc:creator>
    <dc:date>2019-04-12T10:54:10Z</dc:date>
    <item>
      <title>Apply the user defined format to the variable within the datastep if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550560#M152872</link>
      <description>&lt;P&gt;Dear Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would there be any way or function to apply the user-defined&amp;nbsp;format to the variable if exists/present in the dataset?&amp;nbsp;for example something like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data test1;&lt;/P&gt;&lt;P&gt;set&amp;nbsp; test;&lt;/P&gt;&lt;P&gt;if var1 exists then do;&lt;/P&gt;&lt;P&gt;format var1 format1;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if var2 exists then do;&lt;/P&gt;&lt;P&gt;format var2 format2;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if var3 exists then do;&lt;/P&gt;&lt;P&gt;format var3 format3;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 10:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550560#M152872</guid>
      <dc:creator>Sami1234</dc:creator>
      <dc:date>2019-04-12T10:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: Apply the user defined format to the variable within the datastep if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550567#M152874</link>
      <description>&lt;P&gt;The FORMAT statement is not an executable statement so you would have to use macro code to write this statement if the variable exists or not write it at all if not.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 10:51:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550567#M152874</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-04-12T10:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: Apply the user defined format to the variable within the datastep if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550572#M152876</link>
      <description>&lt;P&gt;Thanks for replying.&lt;/P&gt;&lt;P&gt;Would you please give me a little example?&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 10:54:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550572#M152876</guid>
      <dc:creator>Sami1234</dc:creator>
      <dc:date>2019-04-12T10:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: Apply the user defined format to the variable within the datastep if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550624#M152892</link>
      <description>Maybe, in some cases.  For example:&lt;BR /&gt;&lt;BR /&gt;format religion: relfmt.;&lt;BR /&gt;&lt;BR /&gt;This statement affects all variable names that begin with "religion".  If there are none, it affects no variables but might issue a note or even a warning.  If there is 1 named "religion" you get the right result.  But if there are multiple such variables you get the wrong result.&lt;BR /&gt;</description>
      <pubDate>Fri, 12 Apr 2019 13:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550624#M152892</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-04-12T13:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: Apply the user defined format to the variable within the datastep if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550632#M152896</link>
      <description>&lt;P&gt;If you only want to assign formats, without other changes to the data, use proc datasets.&lt;/P&gt;
&lt;P&gt;Set up a control table:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data control;
input varname :$32. fmtname :$32.;
datalines;
var1 format1.
var2 format2.
var3 format3.
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Check which variables are present:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table correction as 
select a.varname, a.fmtname
from control a, dictionary.columns b
where upcast(a.varname) = upcast(b.name) and
  b.libname = "WORK" and b.memname = "TEST";
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Dynamically create the code for proc datasets:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set correction end=eof;
if _n_ = 1 then call execute("
  proc datasets library=WORK noprint;
  modify TEST;
  format 
");
call execute(catx(" ",varname,fmtname, ""));
if eof then call execute("
  ;
  quit;
");
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Apr 2019 13:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/550632#M152896</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-04-12T13:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: Apply the user defined format to the variable within the datastep if variable exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/551053#M153067</link>
      <description>&lt;P&gt;Thank you so much, it works perfectly fine.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 14:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Apply-the-user-defined-format-to-the-variable-within-the/m-p/551053#M153067</guid>
      <dc:creator>Sami1234</dc:creator>
      <dc:date>2019-04-15T14:01:57Z</dc:date>
    </item>
  </channel>
</rss>

